【问题标题】:Generate set of rectangles to form grid in irregular polygon using boost使用 boost 生成一组矩形以在不规则多边形中形成网格
【发布时间】:2016-01-22 18:25:51
【问题描述】:

我需要创建一组预定大小的矩形,这些矩形在不规则(可能不是凸面)多边形内部形成一个网格。 (我意识到边缘上会有一些不适合矩形的位。那些可以丢弃。)一切都是二维的,我的点类型是双精度的。我正在使用 UTM 数据,所以我的多边形离原点很远。我必须使用 C++。我有两个问题:

这可以通过 boost 完成吗?我查看了 Voronoi 图表生成器,但无法在多边形内生成矩形点阵。

我可以使用另一个更适合在多边形内生成一组矩形的几何库吗?

【问题讨论】:

    标签: c++ boost geometry boost-polygon


    【解决方案1】:

    我编写了自己的函数来执行此操作。它可能不漂亮,但它有效。我首先确定了最高和最低的 x 和 y 值。然后我将它们传递给这个函数并根据常数值定义我的单元格的边界。

    using namespace std;
    
    typedef boost::geometry::model::d2::point_xy<double> point_xy;
    typedef boost::geometry::model::polygon<point_xy> polygon_type;
    
    vector<polygon_type> createNScells(double nex, double ney, double swx, double swy) {
        vector<polygon_type> cells;
    
        double x1 = swx;
        double x2 = swx;
    
        point_xy first;
        point_xy second;
        point_xy third;
        point_xy fourth;
    
        while (x2 > nex) {//move x's
            x2 -= const1;
            double y1 = ney;
            double y2 = ney;
            while (y2 > swy) {//move y's
                y2 -= const2;
                //assign x's and y's to points
                first.x(x1);
                first.y(y1);
                second.x(x2);
                second.y(y2);
                third.x(x1);
                third.y(y2);
                fourth.x(x2);
                fourth.y(y1);
                polygon_type r;
                //assign points to polygon
                boost::geometry::append(r, first);
                boost::geometry::append(r, third);
                boost::geometry::append(r, second);
                boost::geometry::append(r, fourth);
                boost::geometry::append(r, first);
                cells.push_back(r);
    
                y1 = y2;
            }
            x1 = x2;
        }
        return cells;
    }
    

    const1 和 const2 定义了我的单元格的大小。最后,我编写了一个函数来删除不在我的多边形边界内的单元格。

    for (int i = 0; i < cells.size(); ++i) {
        if (!boost::geometry::within(cells.at(i), polygons)) {
            swap(cells.at(i), cells.back());
            cells.pop_back();
        }
    }
    

    这当然不是最简洁的解决方案,但我欢迎提高代码效率的方法。

    【讨论】:

      猜你喜欢
      • 2014-05-12
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      相关资源
      最近更新 更多