I'm trying to determine if a point is inside a polygon. the Polygon is defined by an array of Point objects. I can easily figure out if the point is inside the bounded box of the polygon, but I'm not sure how to tell if it's inside the actual polygon or not. If possible, I'd like to only use C# and WinForms. I'd rather not call on OpenGL or something to do this simple task.
Here's the code I have so far:
private void CalculateOuterBounds() { //m_aptVertices is a Point[] which holds the vertices of the polygon. // and X/Y min/max are just ints Xmin = Xmax = m_aptVertices[0].X; Ymin = Ymax = m_aptVertices[0].Y; foreach(Point pt in m_aptVertices) { if(Xmin > pt.X) Xmin = pt.X; if(Xmax < pt.X) Xmax = pt.X; if(Ymin > pt.Y) Ymin = pt.Y; if(Ymax < pt.Y) Ymax = pt.Y; } } public bool Contains(Point pt) { bool bContains = true; //obviously wrong at the moment :) if(pt.X < Xmin || pt.X > Xmax || pt.Y < Ymin || pt.Y > Ymax) bContains = false; else { //figure out if the point is in the polygon } return bContains; }
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
I've checked codes here and all have problems.
The best method is:
/// <summary> /// Determines if the given point is inside the polygon /// </summary> /// <param name="polygon">the vertices of polygon</param> /// <param name="testPoint">the given point</param> /// <returns>true if the point is inside the polygon; otherwise, false</returns> public static bool IsPointInPolygon4(PointF[] polygon, PointF testPoint) { 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; }
The accepted answer did not work for me in my project. I ended up using the code found here.
public static bool IsInPolygon(Point[] poly, Point p) { Point p1, p2; bool inside = false; if (poly.Length < 3) { return inside; } var oldPoint = new Point( poly[poly.Length - 1].X, poly[poly.Length - 1].Y); for (int i = 0; i < poly.Length; i++) { var newPoint = new Point(poly[i].X, poly[i].Y); if (newPoint.X > oldPoint.X) { p1 = oldPoint; p2 = newPoint; } else { p1 = newPoint; p2 = oldPoint; } if ((newPoint.X < p.X) == (p.X <= oldPoint.X) && (p.Y - (long) p1.Y)*(p2.X - p1.X) < (p2.Y - (long) p1.Y)*(p.X - p1.X)) { inside = !inside; } oldPoint = newPoint; } return inside; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With