Is the Point Inside the Polygon?. “In computational geometry, the... | by Anirudh Topiwala | Towards Data Science

“In computational geometry, the point-in-polygon (PIP) problem asks whether a given point in the plane lies inside, outside, or on the boundary of a polygon.” Wikipedia.
A quick and simple algorithm to find whether a point lies inside, on or outside a polygon is very useful in various applications like computer graphics, geographical information systems (GIS), motion planning, CAD, computer vision, etc.
Being a computer vision engineer, some direct application I see are:
Lane Detection: as a lane can be represented as trapezoid, PIP can be used to determine if a pixel lies inside the trapezoid (lane).Calculate area of Object using Edge Detection: any object’s area can be calculated by performing edge detection on it and the area can then be calculated by checking if the pixels lie inside the polygon formed by the edges of the object.
Lane Detection: as a lane can be represented as trapezoid, PIP can be used to determine if a pixel lies inside the trapezoid (lane).
Calculate area of Object using Edge Detection: any object’s area can be calculated by performing edge detection on it and the area can then be calculated by checking if the pixels lie inside the polygon formed by the edges of the object.
Some methods to solve the PIP problem is by using Ray Casting algorithm and Winding Number algorithm. A point to note is that the winding number algorithm is more accurate then ray casting for points really close to the polygon. Also with the newer implementations it is also faster than ray casting algorithm. The PIP problem is further simplified for convex polygons and we will discuss one such method to solve it.
In this article I am going to explain the Winding Number Algorithm which solves PIP for any polygon. I will then cover a simplified method for solving PIP for convex polygons. C++ code for both these methods can be found here.
Winding number is defined by the number of times a curve travels counter clockwise around a point.
The algorithm states that for any point inside the polygon the winding number would be non zero. Therefore it also know as the nonzero-rule algorithm.
One way to calculate the winding number would be to calculate the angle subtended by each side of the polygon with the query point. This is indicated by angles θ1, θ2, θ3 and θ4 for sides AB, BC , CD and DA respectively. If the summation of these angle add up to 2 π the point lies inside the polygon and if the sum is 0, the point lies outside.
sum_of_angles = θ1 + θ2 + θ3 + θ4 = 2 π -> Point is insidesum_of_angles = θ1 + θ2 + θ3 + θ4 = 0 -> Point is outside.
The time complexity of this algorithm would be O(n) similar to ray casting algorithm, but it would involve repeated calculation of inverse trigonometric functions like atan2, to get the angle subtended by the sides of the polygon with the query point.
One way to reduce the complexity as described by W. Randolph Franklin is by simply observing the edges that actually wind around the query point whilst all the other edges can be ignored.
Algorithm: For any polygon, find all the edges of the polygon that cut through the line passing through the query point and is parallel to y_axis. For these edges check if the query point is on the left or right side of the edge when looking at all the edges in anticlockwise direction. Increase the value of winding number (wn) by one if query point is on the left side of an upward crossing and decrease the wn by one if query point is on the right side of an downward crossing. If the final winding number is non zero then the point lies inside the polygon.
Example:For part a) in the figure below:Initially wn = 0Only edges CD and AB cut the line passing through P1 and parallel to y axis.1) wn++ for CD as it is an upward crossing for which P1 is left of CD2) wn wont be changed as P1 is on left of AB (downward crossing).As final winding number wn = 1 which is not equal to zero point P1 lies inside the polygon.A similar case can be made for b) part of the figure. Although it should be noted that we are avoiding the added complexity of the polygon and hence make the algorithm more efficient.For c) part in the figure below:Initially wn = 0Only edges DE and BC cut the line passing through P1 and parallel to y axis.1) wn++ for DE as it is upward crossing for which P1 is left of CD2) wn-- for BC as it is downward crossing for which P1 is right of BCAs final winding number wn = 0 point P1 lies outside the polygon.
Cpp code: (GitHub Handle)
substitute_point_in_line(): This function calculates on which side of the line the point lies.is_point_inside_polygon(): This is the complete algorithm and calculates if a point is inside, outside or on any polygon given the vertices of the polygon in anticlockwise direction. Fixing the direction of the edges by listing the vertices in anticlockwise directions helps locking in the left and the right side of the line segment (polygon edges).
substitute_point_in_line(): This function calculates on which side of the line the point lies.
is_point_inside_polygon(): This is the complete algorithm and calculates if a point is inside, outside or on any polygon given the vertices of the polygon in anticlockwise direction. Fixing the direction of the edges by listing the vertices in anticlockwise directions helps locking in the left and the right side of the line segment (polygon edges).
A convex polygon is a polygon with all its interior angles less than 180°, which means all the vertices point away from the interior of the polygon.
We discuss this separately as the most common types of polygons encountered in computer vision are convex polygons. Theses includes all triangles, squares, parallelograms, trapezoids, etc.
Algorithm: For a convex polygon, if the sides of the polygon can be considered as a path from any one of the vertex. Then, a query point is said to be inside the polygon if it lies on the same side of all the line segments making up the path. This can be seen in the diagram below.
To find on which side of the line segment does the point lie, we can simply substitute the point in the equation of the line segment. For example for the line formed by (x1, y1) and (x2, y2), the query point (xp,yp) can be substituted like:
result = (yp - y1) * (x2 -x1) - (xp - x1) * (y2 - y1)
When looking at segment in anticlockwise direction if the result is :
result > 0: Query point lies on left of the line.result = 0: Query point lies on the line.result < 0: Query point lies on right of the line.
result > 0: Query point lies on left of the line.
result = 0: Query point lies on the line.
result < 0: Query point lies on right of the line.
Cpp code: (GitHub Handle)
substitute_point_in_line(): This function calculates on which side of the line the point lies.is_point_inside_convex_polygon(): This is the complete algorithm and calculates if a point is inside, outside or on a convex polygon given the vertices of the polygon in either clockwise or anticlockwise direction.
substitute_point_in_line(): This function calculates on which side of the line the point lies.
is_point_inside_convex_polygon(): This is the complete algorithm and calculates if a point is inside, outside or on a convex polygon given the vertices of the polygon in either clockwise or anticlockwise direction.
As discussed previously point in convex polygon would also cover cases like
Point in TrapezoidPoint in ParallelogramPoint in Rectangle
Point in Trapezoid
Point in Parallelogram
Point in Rectangle
To sum it up we covered how to find the winding number for a polygon and use it to find if the point is Inside, On or Outside the polygon. We also saw a more simpler solution which can be applied for convex polygons to solve PIP.
Please reach out to me if you have any questions and hope you enjoyed the math.
https://en.wikipedia.org/wiki/Point_in_polygon#cite_note-5http://www.eecs.umich.edu/courses/eecs380/HANDOUTS/PROJ2/InsidePoly.htmlhttp://geomalgorithms.com/a03-_inclusion.htmlWm. Randolph Franklin, “PNPOLY — Point Inclusion in Polygon Test”
https://en.wikipedia.org/wiki/Point_in_polygon#cite_note-5
http://www.eecs.umich.edu/courses/eecs380/HANDOUTS/PROJ2/InsidePoly.html
http://geomalgorithms.com/a03-_inclusion.html
Wm. Randolph Franklin, “PNPOLY — Point Inclusion in Polygon Test”