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

前端 未结 30 2162
醉梦人生
醉梦人生 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:06

    You can do this by checking if the area formed by connecting the desired point to the vertices of your polygon matches the area of the polygon itself.

    Or you could check if the sum of the inner angles from your point to each pair of two consecutive polygon vertices to your check point sums to 360, but I have the feeling that the first option is quicker because it doesn't involve divisions nor calculations of inverse of trigonometric functions.

    I don't know what happens if your polygon has a hole inside it but it seems to me that the main idea can be adapted to this situation

    You can as well post the question in a math community. I bet they have one million ways of doing that

提交回复
热议问题