473,889 Members | 1,656 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
28 33771
gayathri81
25 New Member
Yes. It is also included. But I have found out where the mistake is. Thanks a lot.
Aug 14 '12 #21
gayathri81
25 New Member
I also found that I have to convert into degrees because of latitude and logitude. So I used this:
Expand|Select|Wrap|Line Numbers
  1.  double Dataprocessor::calculateDistance(const Point& a, const Point& b) 
  2. {
  3.     double lat = (a.latitude + b.latitude) / 2 * 0.01745;
  4.     double dLat = 111.3 * cos(lat) * (a.longitude -b.longitude);
  5.     double dLng = 111.3 * (a.latitude - b.latitude);
  6.  
  7.     return sqrt(pow(dLat, 2) + pow(dLng, 2));
  8. }
Now I want to find out whether a point is inside the polygon or not. So I used the above function and the polygon.cc and wrote:
Expand|Select|Wrap|Line Numbers
  1. if(calculateDistance(position,.....))
  2. {
  3. // push back to geofence
  4. }
In the above part, position is the position of test point and the other parameter would be the vertices of polygon. But it is not clear what should be written there? I want to compare the latitude and longitude of test point with each of the vertices of polygon using the ray casting algorithm.

I wrote points but showed me an error , failing to recognise it. What should be written?
Aug 14 '12 #22
Banfa
9,065 Recognized Expert Moderator Expert
points is a vector of Point, but calculateDistan ce is expecting a reference to a single Point.

You need to iterate through your vector of points and individually call the function for each one

Expand|Select|Wrap|Line Numbers
  1. for(std::vector<Point>::iterator iter = points.begin(); iter != points.end(); iter++)
  2. {
  3.   double distance = calculateDistance(position, *iter);
  4.  
  5.   // Do something with distance
  6. }
  7.  
Aug 15 '12 #23
gayathri81
25 New Member
Hello
I perfectly agree with you. I have done what you have told. Now I want to call the polygon algorithm. I tried this out but did not work.
Expand|Select|Wrap|Line Numbers
  1. if(isInside(curPosition.position))
  2. {
  3. //push geofences inside.
  4. }
  5. // Function to check whether the point is inside or outside the 
  6. //polygon : concept of ray casting algorithm
  7. bool DataProcessor::isInside(Point& p)
  8.  {
  9.  
  10.     int i,j= this->points.size()-1;
  11.     bool oddnodes=false;
  12.     for(i=0;i<this->points.size();i++)
  13.     {
  14.         if((this->points[i].longitude < p.longitude && this->points[j].longitude >= p.longitude || this->points[j].longitude< p.longitude && this->points[i].longitude >= p.longitude)&&
  15.                 (this->points[i].latitude <= p.latitude|| this->points[j].latitude <= p.latitude))
  16.         {
  17.             if(this->points[i].latitude + (p.longitude-this->points[i].longitude) / (this->points[j].longitude-this->points[i].longitude) * (this->points[j].latitude-this->points[i].latitude) < p.latitude)
  18.             {
  19.                 oddnodes!=oddnodes;
  20.  
  21.             }
  22.         }
  23.         j=i;
  24.     }
  25.        return oddnodes;
  26. }
//All the code is in DataProcessor.c c.
//curPosition.pos ition is the current position of GPS device.

Where am I going wrong? There are no syntax errors. But I am no getting sms.
Aug 16 '12 #24
Banfa
9,065 Recognized Expert Moderator Expert
As I already pointed out in post #20 you have some errors that are causing your code to go wrong, for example oddnodes!=oddno des; does nothing.
Aug 16 '12 #25
gayathri81
25 New Member
Hallo

Yes you are right. This statement always returns me 0 even if the point is outside or inside. Or how should I modify the code above to get even for outside and odd for inside ( ray casting method)? Please help me out.
Aug 16 '12 #26
Banfa
9,065 Recognized Expert Moderator Expert
As I already said I gave the solution to this in post #20
Aug 16 '12 #27
gayathri81
25 New Member
Hello

Thank you for the solution. But I could not understand this :
double distance = calculateDistan ce(position, *iter);

I am a starter with vector in C++ and I am trying to develop myself.
Aug 17 '12 #28
Banfa
9,065 Recognized Expert Moderator Expert
This is just a call to a function
Expand|Select|Wrap|Line Numbers
  1. double distance = calculateDistance(position, *iter);
double distance = - we are declaring a variable to hold the result called distance and we will initialise it with something.

calculateDistan ce(position, *iter); the thing we are going to initialise it, the return value of the function calculateDistan ce, 2 parameters are being passed to this function, position (which came from your own post) and *iter. Sinceiter is an iterator into your vector by dereferencing iter (*iter) we get the value that the iterator is currently pointing at.

When using a standard container, such as std::vector iterators are used to address each entry in the container in turn, a little like using a pointer you access members of an array. Iterators can be incremented to access the next member and there are a variety of stand-library methods which can help, for example std::distance the distance between 2 iterators into the same container. All containers have methods begin and end to get the iterator to the first entry and the iterator just after the last entry and rbegin and rend which gets elements in reverse order.

Look at this example (run and compile and understand)

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5.  
  6. int main()
  7. {
  8.     // Declare my vector
  9.     std::vector<int> numbers;
  10.  
  11.     // Populate it
  12.     for(int ix = 0; ix < 10; ix++)
  13.     {
  14.         numbers.push_back(ix);
  15.     }
  16.  
  17.     // Iterate through the vector and print out its values
  18.     for(std::vector<int>::iterator iter = numbers.begin(); iter != numbers.end(); iter++)
  19.     {
  20.         std::cout << "Forward Iterate: " << *iter << std:: endl;
  21.     }
  22.  
  23.     // Iterate in reverse order through the vector and print out its values
  24.     for(std::vector<int>::reverse_iterator riter = numbers.rbegin(); riter != numbers.rend(); riter++)
  25.     {
  26.         std::cout << "Reverse Iterate: " << *riter << std:: endl;
  27.     }
  28.  
  29.     // Check size
  30.     std::cout << "Count: " << numbers.size() << " = " << std::distance(numbers.begin(), numbers.end()) << std::endl;
  31.  
  32.     return 0;
  33. }
  34.  
Aug 17 '12 #29

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

Similar topics

1
1644
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
1172
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
3663
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
8447
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
6173
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
2245
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
4019
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
10845
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
3593
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
9969
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
9810
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11203
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10896
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9612
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
7151
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();...
1
4650
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
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.