Get the points of intersection from 2 rectangles(从2个矩形中获取交点)
问题描述
假设我们有两个矩形,分别定义了它们的左下角和右上角.例如: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.
任何想法、算法、伪代码,将不胜感激.
Any ideas, algorithm, pseudo code, would be greatly appreciated.
附言我发现了类似的问题,但他们只检查 2 个矩形是否相交.
p.s. I found similar questions but they check only if 2 rectangle intersect.
推荐答案
如果输入矩形是标准化的,即你已经知道 x2x1
, 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)-(x6, y6)
.如果原始矩形不相交,则结果将是一个退化"矩形(具有 x5 >= x6
和/或 y5 >= 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.
附:像往常一样,小细节将取决于您是否必须将 touching 矩形视为相交.
P.S. As usual, small details will depend on whether you have to consider touching rectangles as intersecting.
这篇关于从2个矩形中获取交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从2个矩形中获取交点
- 静态初始化顺序失败 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01