[Math] How to check if two rectangles intersect? Rectangles can be rotated

geometryrectangles

How to check if two rectangles intersect? Each rectangle is defined by three points in 2d space. The rectangles can be rotated around any point as on the image below.

enter image description here

Best Answer

Hint:

  • You can use 2D cross product of vectors to decide whether a point is on the left- or right-hand side of some given vector.
  • You can use the above to check if a point is inside or outside a convex polygon by checking each edge.
  • You can use the first bullet point to check if two vectors intersect.
  • Two rectangles $A$ and $B$ intersect if and only if at least one of the following is satisfied
    1. a corner of $A$ is inside $B$;
    2. a corner of $B$ is inside $A$;
    3. some edge of $A$ intersect with some edge of $B$.

I hope this helps $\ddot\smile$