Rotated 2d rectangle intersection detection

2019-07-16 09:46发布

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.

2条回答
Deceive 欺骗
2楼-- · 2019-07-16 10:00

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

查看更多
Juvenile、少年°
3楼-- · 2019-07-16 10:25
登录 后发表回答