Sign in to follow this  

Point in Polygon (C# Implementation)

This topic is 3151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There was a C# source code posted on the MSDN forums about Point in Polygon test: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/95055cdc-60f8-4c22-8270-ab5f9870270a/ For some reason, I can't seem to get this to work. I went through the code, and it *makes* sense to me, but I'm not getting the results I'm expecting. This is how I am using the code:
Point[] poly = new Point[4];
poly[0] = new Point(0, 0);
poly[1] = new Point(4, 0);
poly[2] = new Point(0, 4);
poly[3] = new Point(4, 4);

Point p = new Point(1, 1);

if (PointInPolygon(p, poly)) 
{ Console.WriteLine("Yes"); }
else
{ Console.WriteLine("No"); }



As you can see above, I passed in an array of points that define a square. Point (1,1) should reside in that square, but the function keeps returning false. Please point out my stupid error. I'm losing too many brain cells right now :( Thank you.

Share this post


Link to post
Share on other sites
That's not a square but an hourglass shaped polygon. Try swapping the order of the points. I haven't checked, but it's also likely that the algorithm expects the polygon's vertices to be defined in either clockwise or counter-clockwise order.

Share this post


Link to post
Share on other sites
C0D1F1ED, THANK YOU.

I knew that it had to be counter-clockwise or clockwise!!! But for some reason, I *thought* what I put down was clockwise...

This is what happens when you code for 16 hours straight with no food :(

All I had to do was change it to:


Point[] poly = new Point[4];
poly[0] = new Point(0, 0);
poly[1] = new Point(0, 4);
poly[2] = new Point(4, 4);
poly[3] = new Point(4, 0);

Share this post


Link to post
Share on other sites

This topic is 3151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this