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

前端 未结 30 2666
醉梦人生
醉梦人生 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条回答
  •  萌比男神i
    2020-11-21 05:47

    Compute the oriented sum of angles between the point p and each of the polygon apices. If the total oriented angle is 360 degrees, the point is inside. If the total is 0, the point is outside.

    I like this method better because it is more robust and less dependent on numerical precision.

    Methods that compute evenness of number of intersections are limited because you can 'hit' an apex during the computation of the number of intersections.

    EDIT: By The Way, this method works with concave and convex polygons.

    EDIT: I recently found a whole Wikipedia article on the topic.

提交回复
热议问题