[英]Rectangles Intersection (Vertical line)
对于给定的矩形R1
, 如果我绘制一条垂直线段,我试图找出哪些其他矩形可以与其相交。
与R1
相交的矩形标记为红色。
每个矩形都有其(top, left)
和(bottom, right)
坐标。
R1 = [top, left, bottom, right],...,Rn = [top, left, bottom, right]
通过使用坐标和垂直线。 我想找到与R1相交的矩形
我发现以下库的功能与icl boost库相同,但必须更简单:下载站点:[ https://github.com/ekg/intervaltree] [2 ]
#include <iostream>
#include <fstream>
#include "IntervalTree.h"
using namespace std;
struct Position
{
int x;
int y;
string id;
};
int main()
{
vector<Interval<Position>> intervals;
intervals.push_back(Interval<Position>(4,10,{1,2,"r1"}));
intervals.push_back(Interval<Position>(6,10,{-6,-3,"r2"}));
intervals.push_back(Interval<Position>(8,10,{5,6,"r3"}));
vector<Interval<Position> > results;
vector<string> value;
int start = 4;
int stop = 10;
IntervalTree<Position> tree(intervals);
// tree.findContained(start, stop, results);
tree.findOverlapping(start, stop, results);
cout << "found " << results.size() << " overlapping intervals" << endl;
}
interval.push_back(Interval(4,10,{1,2,“ r1”})));
您需要一种碰撞检测算法。 在C ++中, boost.geometry可用于执行许多其他操作。
您不在乎矩形的垂直位置。 您可以将所有内容投影到x轴上,然后解决相应的一维问题:您有一组间隔,并且想知道给定间隔中哪些重叠。 这正是间隔树的作用:
哪里:
伪码
// make sure x1 is on the left of x2
if (R.x1 > R.x2)
tmp = R.x2
R.x2 = R.x1
R.x1 = tmp
end if
for each Rect as r
// don't test itself
if (R != r)
// make sure x1 is on the left of x2
if (r.x1 > r.x2)
tmp = r.x2
r.x2 = r.x1
r.x1 = tmp
end if
if ((r.x2 < R.x1) // if r rect to left of R rect
|| (r.x1 > R.x2)) // if r rect to right of R rect
// r rect does not intersect R rect
else
// r rect does intersect R rect
end if
end if
end for
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.