得到的交叉点的2的矩形 [英] Get the points of intersection from 2 rectangles
问题描述
让说,我们有两个矩形,用自己的左下和右上边角定义。例如: Rect1的(X1,Y1)(X2,Y2)和 RECT2(X3,Y3)(X4,Y4)。 我试图找到相交矩形的坐标(左下和右上)。
Let say that we have two rectangles, defined with their bottom-left and top-right corners. For example: rect1 (x1, y1)(x2, y2) and rect2 (x3, y3)(x4, y4). I'm trying to find the coordinates(bottom-left and top-right) of the intersected rectangle.
任何想法,算法,伪code,将大大AP preciated。
Any ideas, algorithm, pseudo code, would be greatly appreciated.
P.S。我发现了类似的问题,但他们只检查是否2矩形相交。
p.s. I found similar questions but they check only if 2 rectangle intersect.
推荐答案
如果输入的矩形进行归一化,即你已经知道, X1< X2
, Y1< Y2
(与同为第二个矩形),那么所有你需要做的就是计算
If the input rectangles are normalized, i.e. you already know that x1 < x2
, y1 < y2
(and the same for the second rectangle), then all you need to do is calculate
int x5 = max(x1, x3);
int y5 = max(y1, y3);
int x6 = min(x2, x4);
int y6 = min(y2, y4);
和它会给你的交集为矩形(X5,Y5) - (5233,Y6)
。如果原来的矩形不相交,其结果将是一个堕落的矩形(用 X5&GT; = 5233
和/或 Y5&GT; = Y6
),您可以轻松地检查。
and it will give you your intersection as rectangle (x5, y5)-(x6, y6)
. If the original rectangles do not intersect, the result will be a "degenerate" rectangle (with x5 >= x6
and/or y5 >= y6
), which you can easily check for.
P.S。像往常一样,小的细节将取决于你是否有考虑的接触的矩形,如图相交。
P.S. As usual, small details will depend on whether you have to consider touching rectangles as intersecting.
这篇关于得到的交叉点的2的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!