Check if xy point is within a polygon Console App
-
I have create a Win form app and create Polygons as follows: private List> Polygons = new List>(); I store this polygons with Xml.Serialization, into a MySQL database as string and Deserialize it with my console app into a list of polygons again. In my console app, which eventually is going to run from Raspberry Pi, I'm getting data from USB, which consist of floating X and floating Y coordinates. Now I want to know how do I check if this point is within one of the polygons. I've tried it first with WinForms app and it works great. public static bool IsPointInPolygon(IList polygon, PointF testPoint) { try { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y) { if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X) { result = !result; } } j = i; } return result; } catch (Exception ex) { MessageBox.Show("error - " + ex.ToString()); return false; } I don't want to draw something, I know it's a console app, just getting a bool or int value back from the Test. Any suggestions or help please?
-
I have create a Win form app and create Polygons as follows: private List> Polygons = new List>(); I store this polygons with Xml.Serialization, into a MySQL database as string and Deserialize it with my console app into a list of polygons again. In my console app, which eventually is going to run from Raspberry Pi, I'm getting data from USB, which consist of floating X and floating Y coordinates. Now I want to know how do I check if this point is within one of the polygons. I've tried it first with WinForms app and it works great. public static bool IsPointInPolygon(IList polygon, PointF testPoint) { try { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y) { if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X) { result = !result; } } j = i; } return result; } catch (Exception ex) { MessageBox.Show("error - " + ex.ToString()); return false; } I don't want to draw something, I know it's a console app, just getting a bool or int value back from the Test. Any suggestions or help please?
-
I have create a Win form app and create Polygons as follows: private List> Polygons = new List>(); I store this polygons with Xml.Serialization, into a MySQL database as string and Deserialize it with my console app into a list of polygons again. In my console app, which eventually is going to run from Raspberry Pi, I'm getting data from USB, which consist of floating X and floating Y coordinates. Now I want to know how do I check if this point is within one of the polygons. I've tried it first with WinForms app and it works great. public static bool IsPointInPolygon(IList polygon, PointF testPoint) { try { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y) { if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X) { result = !result; } } j = i; } return result; } catch (Exception ex) { MessageBox.Show("error - " + ex.ToString()); return false; } I don't want to draw something, I know it's a console app, just getting a bool or int value back from the Test. Any suggestions or help please?
If you create a GraphicsPath object for the points, you can use that to check:
System.Drawing.Drawing2D.GraphicsPath poly = new System.Drawing.Drawing2D.GraphicsPath(); poly.AddLine(10, 10, 90, 10); poly.AddLine(90, 10, 90, 90); poly.AddLine(90, 90, 10, 90); poly.AddLine(10, 90, 10, 10); Point inPoly = new Point(50, 50); Point outPoly = new Point(0, 0); Console.WriteLine(poly.IsVisible(inPoly) ? "IN" : "OUT"); Console.WriteLine(poly.IsVisible(outPoly) ? "IN" : "OUT");
The beauty of that is that the poly can be extremely complex and the framework will sort it out. For example, an hourglass:
System.Drawing.Drawing2D.GraphicsPath poly = new System.Drawing.Drawing2D.GraphicsPath(); poly.AddLine(10, 10, 90, 90); poly.AddLine(90, 90, 10, 90); poly.AddLine(10, 90, 90, 10); poly.AddLine(90, 10, 10, 10); Point inPoly = new Point(50, 40); Point outPoly = new Point(40, 50); Console.WriteLine(poly.IsVisible(inPoly) ? "IN" : "OUT"); Console.WriteLine(poly.IsVisible(outPoly) ? "IN" : "OUT");
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
What exactly is the problem? If the calculations work in a Windows Form app, then they will work in any other app.
I didn't add the drawing reference. Not my day. Thank you Richard. It works
-
I didn't add the drawing reference. Not my day. Thank you Richard. It works
-
I didn't add the drawing reference. Not my day. Thank you Richard. It works
Says who? Of course you can! See the code I gave you in my reply - that code works just fine in a Console app!
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!