分离轴测试,检测旋转的矩形是否与另一个扁平矩形重叠

时间:2009-03-07 23:04:56

标签: algorithm math graphics geometry

我读到了关于交叉矩形的信息:

Algorithm to detect intersection of two rectangles?

但是我很难实现它。

如果R1(A,B,C,D)是我的旋转矩形而R2(A',B',C',D')是另一个没有旋转的矩形。

从上面的链接中提取的公式是:

  edge = v(n) - v(n-1)

旋转90°可以使其垂直。在2D中,这很简单:

  rotated.x = -unrotated.y
  rotated.y =  unrotated.x

  // rotated: your rotated edge
  // v(n-1) any point from the edge.
  // testpoint: the point you want to find out which side it's on.

  side = sign (rotated.x * (testpoint.x - v(n-1).x) + 
               rotated.y * (testpoint.y - v(n-1).y);

我的旋转边缘将来自R1

AB(xB-xA,yB-yA)所以旋转x是xB-xA? BC(xC-xB,yC-y1) CD ...... AD ......

测试点将来自R2的A',B',C',D' 因此,我必须检查R2的所有点与R1的4个边缘的结果的符号。 如果相交则进行16次比较。我怎么知道我是否找到了分离边缘?

由于

1 个答案:

答案 0 :(得分:2)

如果对于任何给定的边缘,任何针对该边缘测试的点积的符号都不匹配,那么您有一个交点。点积的符号对于线的一侧的所有点都是相同的。

相关问题