How to check whether point was inside or outside of the polygon

[ATTACH=CONFIG]1887[/ATTACH]I want to uses the angle to check point inside or outside, if point is inside all the angle will add up to 360, and outside was less than 360.
[ATTACH=CONFIG]1888[/ATTACH]I want to know the formula for 2 point P0P1 turn into vector. so I can using [ATTACH=CONFIG]1889[/ATTACH]to find angle

It’s more efficient (and robust) to use plane equations. For each adjacent pair of vertices [x1,y1] and [x2,y2], check the sign of
(y1-y2)x + (x2-x1)y + (x1y2-x2y1)
where [x,y] is the test point.

If the point is inside the polygon, all of the values will have the same sign (whether it’s positive or negative depends upon whether the vertices are in clockwise or anticlockwise order).

This is likely to be somewhat faster than calculating lengths (each of which requires a square root) and using acos(), as well as being more numerically stable. acos() is ill-conditioned for arguments close to ±1. Additionally, if the point lies very close to the edge; rounding errors can result in (a·b)/(|a||b|) evaluating to something like -1.00001, which results in acos() returning NaN.

Just checking for same signs only works on convex polygons. If the polygon is concave you might need something like ‘counting quadrants’. See: https://www.youtube.com/watch?v=f-DIsdvwJ7E

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.