对于给定的矩形R1
我试图找出哪些是可以与之相交的其他矩形 IF 我绘制了一个vectical线段。
与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;
}
intervals.push_back(时间间隔(4,10,{1,2, “R1”}));
答案 0 :(得分:3)
您需要一个collision detection算法。在C ++中,有boost.geometry用于在许多其他人中做这些事情。
答案 1 :(得分:3)
您不关心矩形垂直的位置。您可以将所有内容投影到x轴上,然后解决相应的一维问题:您有一组间隔,并且您想知道哪个重叠与给定的间隔。这正是区间树的作用:
答案 2 :(得分:1)
其中:
伪代码
// 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