2

I have 2 rectangles they are placed arbitrarily and I have rects all four corner point like

struct Rect
{
 NSPoint topLeft; 
 NSPoint topRight; 
 NSPoint bottomLeft; 
 NSPoint bottomRight; 
}

I want to check whether 2 rectangles intersects. I am looking a method similar to NSIntersectsRect . But NSIntersectsRectwon't respect the rotation of rectangle. The points in structure are the points obtained after rotation.

Sample code in Objecitve-C ,C++ or C will be a great help.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Raviprakash
  • 2,410
  • 6
  • 34
  • 56
  • if the rectangles are rotated, it is extremely difficult. the full answer is explained here: http://stackoverflow.com/questions/8011267/area-of-rectangle-rectangle-intersection – Fattie Sep 12 '12 at 10:04

2 Answers2

5

One simple way is to check whether every vertex of one rectangle is on the same and exterior side of an edge of the one, and vice-versa. For faster and more general methods, see http://gpwiki.org/index.php/Polygon_Collision and http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Plante/CompGeomProject-EPlante/algorithm.html

lhf
  • 70,581
  • 9
  • 108
  • 149
2

See e.g.g 2D Rotated Rectangle Collision on gamedev.net

Veger
  • 37,240
  • 11
  • 105
  • 116
second
  • 28,029
  • 7
  • 75
  • 76