473,836 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Point in polygon

25 New Member
I am trying to find out whether a point is inside a polygon.I am using vector std c++.

My program till now:
Expand|Select|Wrap|Line Numbers
  1. Polygon.h
  2. class Polygon {
  3. public:
  4. Polygon();
  5. virtual ~Polygon();
  6. virtual bool Inside(Point p);
  7. private:
  8. std::vector<Point> pts;
  9.  
  10. Polygon.cc
  11. Polygon::Polygon()
  12. {
  13. }
  14. Polygon::~Polygon()
  15. {
  16. }
  17. bool Polygon Inside(Point p)
  18. {
  19. int nvert=pts.size;
  20. }
--------
I liked the ray casting algorithm but I dont know how to do in C++. Could anyone help out?
Jul 17 '12 #1
28 33768
weaknessforcats
9,208 Recognized Expert Moderator Expert
Here again, polygon is a general word. Use it as a base class. Then derive your polygons: Square, Triangle, Rectangle, Pentagon, etc. Try to not start out to solve all polygons in one class.

Then in each derived class write a convert method to build the database. Each derived class can have a different algorithm and database. All that's in the base class is the function prototype that you override in the derived class.

Your square is just rectangle so maybe you derive square from rectangle to allow the square to use the same database as the rectangle. Maybe there is a parallogram class deriving from polygon and then square and rectangle derive from that.

The idea is that you implement a shape at a time. Figuring out if a point is inside or outside a rectangle or triangle is pretty straightforward so solve those first. You can just calculate these. No database required.

later when you have a polygon that's a rectangle with a triangle punched out of it you can use the already written rectangle and triangle code to find out of your point is inside our outside the shape. Like if it is inside the rectangle and also inside the triangle, then it is outside the shape. In your class are member variables that are Rectangle and Triangle objects.

C++ expects you to write a little and get that working. Then write a little more and get that working. By repeated improvement cycles you have this humongous capability and all along the way the program has been working. Any errors are probably in just the last improvement cycle.
Jul 17 '12 #2
gayathri81
25 New Member
Thanks for your suggestion. I did as you told. First I created a base class called Shape and a derived class called Polygon. In the base class, I used a function calculateDistan ce to calculate distance between 2 points.
Shape.cc
Expand|Select|Wrap|Line Numbers
  1. inline double Shape::calculateDistance(Point &a,Point &b)
  2. {
  3. //formula to calculate distance between two points
  4. }
  5.  
When I compile it, I get error as:
Extra qualification Shape on member calculateDistan ce.

I wrote the point in polygon algorithm in derived class and when I compile it, it refers to the same error as above.

Please help me out.
----------
@weaknessforcats
Jul 18 '12 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
There is no error in the code you posted.

Post the exact error your compiler gave you.
Jul 20 '12 #4
gayathri81
25 New Member
The error got solved when I removed the keyword inline. There were 4 lines inside the braces. So I removed it and now I have solved it.
Jul 23 '12 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
That may be but the code word inline was not your problem.

An inline function is not called. Rather the compiler makes a copy of the code and pastes the copy where you make your call. To do that the compiler must have seen the inline function before you call it so it can make a copy.

I suspect you made a call using a function protototype and not the function itself.
Jul 23 '12 #6
gayathri81
25 New Member
As you told, I made polygon as a sub class and used the algorithm below to find whether a point is inside or not.
Polygon.h
Expand|Select|Wrap|Line Numbers
  1. class Polygon: public Shape
  2. {
  3. public:
  4. virtual bool inside(Point& p)
  5. private:
  6. std::vector<Point&> points;
  7. };
Polygon.cc

Expand|Select|Wrap|Line Numbers
  1. bool Polygon::inside(Point& p) 
  2. {   
  3. int i, j, c = 0; 
  4.   int nvert = poly.size();   
  5.  for (i = 0, j = nvert-1; i < nvert; j = i++)
  6.  { 
  7.      if ( ((poly[i].y> p.y) != (poly[j].y> p.y)) && (p.x < (poly[j].x-poly[i].x) * (p.y-poly[i].y) / (poly[j].y-poly[i].y) + poly[i].x) )       
  8.  c = !c;   
  9.  } 
  10.   return c;
  11.  } 
This is the ray casting algorithm. But on compiling I am gettting plenty of errors like:
error: forming pointer to referenece type 'Point&'

Where am I going wrong? Can you help me.
Jul 23 '12 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:
Expand|Select|Wrap|Line Numbers
  1. std::vector<Point&> points;
won't fly. A vector is an array. An array contains objects or pointers.

A reference is not an object. Rather it is an alias for an already existing object.

What your vector needs is a type and not a reference to a type.

Just do this:

Expand|Select|Wrap|Line Numbers
  1. std::vector<Point> points;
and your error will go away.
Jul 23 '12 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
OOPS.

The codeword inline was a problem after all.

Everything I said about inline functions is true but in addition since inline functions are not called they have no address. They have just been pasted around as needed.

Virtual functions, on the other hand, need an address so they can be overriden wherever they may be called.

Hence, inline functions cannot be virtual.

Sorry I didn't pick up on this sooner.
Jul 23 '12 #9
gayathri81
25 New Member
Thank you very much . I removed the & and the error went away.
Aug 2 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1640
by: paul | last post by:
library for querying virtual polygon over raster spatial data in my c++ application, after calculating my polygon i wish to define it (simple 4 sided polygon with floats or line parameters) and use this to query a b/w image in memory, i want to get the pixel counts, full, hopefully partial, perhaps specific to a side, data to calculate area ect, can someone help point me to a library paul
1
1170
by: Jaime | last post by:
When I send a graph (polygon) to printer, I receive the next message: Object referente not set to an instante of an object.--- at System.Drawing.SafeNativeMethods.GdipFillPolygonI(HandleRef graphics, HandleRef brush, HandleRef points, Int32 count, Int32 brushMode) at System.Drawing.Graphics.FillPolygon(Brush brush, Point points, FillMode fillMode) at WinAppSeguimiento.frmImpresion.PaintRadar(Graphics& graphToRender, Double paramRadio,...
6
3658
BSOB
by: BSOB | last post by:
im gone for a long weekend so plenty of time for anyone to answer. if i have 4 points representing a polygon and each point is represented by an x and a y coordinate, is there an easy (or slightly less painfull) way to decide whether or not a point (a,b) lays within that polygon? please note that this polygon DOES NOT take the shape of a square and has NO right angles and all side lengths are somewhat random. the application: say i...
1
8438
by: renu | last post by:
Hello, I have drawn polygon on window. And I want to check wheather given point is in that polygon region or not? How shold I find that? I have created object of class region GraphicsPath path = new GraphicsPath(); path.AddPolygon(pts); //////// Some points I have passed Region rgn = new Region(path); RegionData rgnDta = rgn.GetRegionData();
3
6168
by: jojo41300000 | last post by:
Hi, Is anyone know that how to get the x and y points inside the polygon using C++ program? I have the given polygon data to draw the polygon, but i don't know how to get all the points inside the polygon. Eg. Let's say we have 5 points to draw a polygon. Polygon ---------------------
3
2242
by: friendkitty | last post by:
Hi All, I m a new member here.I am now writing a program that render Vertex Normals.I m now trying to implement a data structure that will best suit computing vertex normals.For that computation , my data structure should have accessed to the faces adjacent to each vertices. first i read all vertices ,and faces from .m file into a structure.And stored in vectors. vector<Vertex> vertices; vector<Face> faces; Then i render that model.No...
41
4004
AccessIdiot
by: AccessIdiot | last post by:
I have a puzzle for all you sql fans. Imagine a polygon with 12 sides (SideA, SideB, SideC, etc) Within this polygon are 96 points (Pt1, Pt2, Pt3, etc). Now let's say I have a table that has the distances from each point to each side Point Side Distance Pt1 SideA 25.6683 Pt1 SideB 37.6778
6
10843
by: moondaddy | last post by:
I need to be able to make polygons with rounded corners. This will be to draw group outlines around shapes in a diagramming tool in wpf. all angles in the polygon will be 90 degrees, but somehow I wanted to make the corners a bit rounded (otherwise it will look like an old legacy app). Is this possible in wpf? The reason I wanted to use a polygon was because I will bind an adorner to each point so the user can drag the corners around to...
5
3589
by: Dave Bootsma | last post by:
I have an application where I want to redraw a polygon from points I retrieve from a file. The file is read with streamreader line by line each line contains the points for each polygon. Below is a typical line from my file. 500,748 500,678 552,678 552,696 584,696 584,714 612,714 612,748 Each set of x,y coordinates is separated by a space. How can I read this into my points collection. If I currently try I get
1
2492
by: JennySmith | last post by:
Hello friend. I want to write a program whether a given point in the plane lies inside, outside, or on the boundary of a polygon. I have found this interesting website which shows 2 ways of doing that. i.e InsidePolygon() and pnpoly() http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/ But these Algo/code don't detect if the input pt is on the boundary of a polygon? Can you guys help me in that part of code/algo? Your help is...
0
9810
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10527
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9358
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.