【问题标题】:Rectangles Intersection (Vertical line)矩形交点(垂直线)
【发布时间】:2014-06-09 11:42:08
【问题描述】:

对于给定的矩形R1,我试图找出可以与之相交的其他矩形IF我画了一条垂直线段。 p>

与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;
}

示例

  • 左 = 4;
  • 右 = 10;
  • 结构 {1,2,"rc1"};

intervals.push_back(Interval(4,10,{1,2,"r1"}));

【问题讨论】:

  • 您有什么问题?你试过什么?
  • 我正在尝试找出这个问题是否存在以及是否有特定的算法来解决它@JoachimPileborg
  • 我正在研究扫描线算法的想法。但对我来说听起来太复杂了。我想找到一种更简单的方法来做到这一点
  • 你的意思是“找到所有与任何可以通过 R1 绘制的垂直线相交的矩形”?我问是因为这完全是重叠线段的一维问题。
  • @Agent_L 是的,这就是我想要的。与可通过 R1 绘制的任何垂直线相交的所有矩形"

标签: c++ computational-geometry intersection rectangles


【解决方案1】:

您需要一个collision detection 算法。在 C++ 中,boost.geometry 用于执行此类操作。

【讨论】:

    【解决方案2】:

    您不关心矩形的垂直位置。您可以将所有内容投影到 x 轴上,然后解决相应的一维问题:您有一组区间,并且您想知道哪些与给定区间重叠。这正是区间树的作用:

    https://en.wikipedia.org/wiki/Interval_tree

    【讨论】:

    • 棉花你能解释一下投影吗?间隔是多少?
    • 将一个点 (x, y) 投影到 x 轴上就是 x。将一个矩形(上、左)、(下、右)投影到x轴上就是区间[左、右]。
    • 对不起@Cotton 最后的评论 只是想看看我是否做对了。每个矩形都将投影到 X 轴上。所以我将有以下列表:[[left1,right1], [left2,right2],....,[leftn,rightn]] 我将构建一个 inverval Tree 并查看它们是否重叠?操作:1)添加区间 2)给定区间 x,判断 x 是否与任何现有区间重叠。
    • 是的!我认为应该这样做。祝你好运!
    【解决方案3】:

    地点:

    • 矩形是所有矩形的集合。
    • R 是要测试的矩形
    • x1 是一个 x 坐标
    • x2 是其他 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
    

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多