两个矩形相交 [英] Two Rectangles intersection
本文介绍了两个矩形相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个矩形,每个矩形有 4 个值:
I have two rectangles characterized by 4 values each :
左侧位置X
,顶部位置Y
,宽度W
和高度H
:
Left position X
, top position Y
, width W
and height H
:
X1, Y1, H1, W1
X2, Y2, H2, W2
矩形不会旋转,就像这样:
Rectangles are not rotated, like so:
+--------------------> X axis
|
| (X,Y) (X+W, Y)
| +--------------+
| | |
| | |
| | |
| +--------------+
v (X, Y+H) (X+W,Y+H)
Y axis
判断两个矩形的交集是否为空的最佳解决方案是什么?
What is the best solution to determine whether the intersection of the two rectangles is empty or not?
推荐答案
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))
– 而不是两个加上宽度和高度,它看起来像这样:
If you have four coordinates – ((X,Y),(A,B))
and ((X1,Y1),(A1,B1))
– rather than two plus width and height, it would look like this:
if (A<X1 or A1<X or B<Y1 or B1<Y):
Intersection = Empty
else:
Intersection = Not Empty
这篇关于两个矩形相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文