• Create Account

## Point in Polygon (C# Implementation)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1AltecZZ  Members

133
Like
0Likes
Like

Posted 30 April 2009 - 01:25 PM

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.

### #2C0D1F1ED  Members

456
Like
0Likes
Like

Posted 30 April 2009 - 01:40 PM

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.

### #3AltecZZ  Members

133
Like
0Likes
Like

Posted 30 April 2009 - 01:46 PM

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);

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.