Method to detect intersection between a rectangle and a polygon?

Hich picture Hich · Aug 21, 2011 · Viewed 7.7k times · Source

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

red rectangle and black polygon

Answer

CHP picture CHP · May 8, 2014

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.