我有两个分别具有4个值的矩形:
左侧位置X,顶部位置Y,宽度W和高度H:
X
Y
W
H
X1, Y1, H1, W1 X2, Y2, H2, W2
矩形不旋转,如下所示:
+--------------------> X axis | | (X,Y) (X+W, Y) | +--------------+ | | | | | | | | | | +--------------+ v (X, Y+H) (X+W,Y+H) Y axis
确定两个矩形的交点是否为空的最佳解决方案是什么?
if (X1+W1<X2 or X2+W2<X1 or Y1+H1<Y2 or Y2+H2<Y1): Intersection = Empty else: Intersection = Not Empty
如果您有四个坐标– ((X,Y),(A,B))和((X1,Y1),(A1,B1))–而不是两个坐标加上宽度和高度,则看起来像这样:
((X,Y),(A,B))
((X1,Y1),(A1,B1))
if (A<X1 or A1<X or B<Y1 or B1<Y): Intersection = Empty else: Intersection = Not Empty