How can I determine whether a 2D Point is within a Polygon?

前端 未结 30 2160
醉梦人生
醉梦人生 2020-11-21 05:06

I\'m trying to create a fast 2D point inside polygon algorithm, for use in hit-testing (e.g. Polygon.contains(p:Point)). Suggestions for effective tech

30条回答
  •  爱一瞬间的悲伤
    2020-11-21 06:00

    The answer depends on if you have the simple or complex polygons. Simple polygons must not have any line segment intersections. So they can have the holes but lines can't cross each other. Complex regions can have the line intersections - so they can have the overlapping regions, or regions that touch each other just by a single point.

    For simple polygons the best algorithm is Ray casting (Crossing number) algorithm. For complex polygons, this algorithm doesn't detect points that are inside the overlapping regions. So for complex polygons you have to use Winding number algorithm.

    Here is an excellent article with C implementation of both algorithms. I tried them and they work well.

    http://geomalgorithms.com/a03-_inclusion.html

提交回复
热议问题