# Point in Polygon (C# Implementation)

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

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

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633654
• Total Posts
3013170
×