Algorithm 查找两个矩形是否重叠
我试图找出两个矩形是否相互重叠。我将以下矩形表示为Algorithm 查找两个矩形是否重叠,algorithm,math,rectangles,overlapping,Algorithm,Math,Rectangles,Overlapping,我试图找出两个矩形是否相互重叠。我将以下矩形表示为[x1,x2]x[y1,y2] Rect 1 = [0.0, 1.0] x [0.0, 1.0] Rect 2 = [0.7, 1.2] x [0.9, 1.5] 我只需要一个可以实现的伪代码来查找矩形是否相互重叠。if((Rect1.topLeft.x>Rect2.bottomRight.x)|(Rect1.bottomRight.xRect2.bottomRight.y)|(Rect1.bottomRight.yb.x2 | | b.x1>
[x1,x2]x[y1,y2]
Rect 1 = [0.0, 1.0] x [0.0, 1.0]
Rect 2 = [0.7, 1.2] x [0.9, 1.5]
我只需要一个可以实现的伪代码来查找矩形是否相互重叠。if((Rect1.topLeft.x>Rect2.bottomRight.x)|(Rect1.bottomRight.xRect2.bottomRight.y)|(Rect1.bottomRight.y if ((Rect1.topLeft.x > Rect2.bottomRight.x) || (Rect1.bottomRight.x < Rect2.topLeft.x) || (Rect1.topLeft.y > Rect2.bottomRight.y) || ( Rect1.bottomRight.y < Rect2.topLeft.y)):
Then NOT OVERLAPPING
else:
OVERLAPPING
那就不重叠了
其他:
重叠
如果矩形表示为
[x1,x2]x[y1,y2]
:
if(a.x1>b.x2 | | b.x1>a.x2)返回false;//检查x轴
如果(a.y1>b.y2 | b.y1>a.y2)返回false;//检查y轴
返回true;
条件
a.x1>b.x2
检查a
的左边框是否位于b
的右边框的右侧,如果是这样,则矩形不重叠。其他三个条件是相似的。我认为答案是这样的。@sasmith提供的答案非常有效。谢谢虽然此代码可能会回答该问题,但提供有关此代码为什么和/或如何回答该问题的附加上下文可提高其长期价值。