Method to detect intersection between a rectangle and a polygon?

前端 未结 5 882
名媛妹妹
名媛妹妹 2021-01-04 19:46

What is the best method to detect whether the red rectangle overlaps the black polygon? Please refer to this image:

5条回答
  •  半阙折子戏
    2021-01-04 20:28

    There are four cases.

    1. Rect is outside of Poly
    2. Rect intersects Poly
    3. Rect is inside of Poly
    4. Poly is inside of Rect

    First: check an arbitrary point in your Rect against the Poly (see Point in Polygon). If it's inside you are done, because it's either case 3 or 2. If it's outside case 3 is ruled out.

    Second: check an arbitrary point of your Poly against the Rect to validate/rule out case 4.

    Third: check the lines of your Rect against the Poly for intersection to validate/rule out case 2.

    This should also work for Polygon vs. Polygon (convex and concave) but this way it's more readable.

提交回复
热议问题