我需要创建一组预定大小的矩形,这些矩形在不规则(可能不是凸面)多边形内部形成网格。 (我意识到边缘上的位将不适合矩形。那些可以被丢弃。)一切都是二维的,我的点类型是双精度。我正在使用UTM数据,所以我的多边形远不是原点。我必须使用c ++。我有两个问题:
这可以通过提升来完成吗?我查看了Voronoi图构建器,但是我在多边形内生成一个矩形点阵点时遇到了麻烦。
我可以使用的另一个几何库更适合在多边形内生成一组矩形吗?
答案 0 :(得分:0)
我写了自己的功能来做到这一点。它可能不漂亮,但它的工作原理。我首先确定了最高和最低的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();
}
}
这当然不是最好的解决方案,但我欢迎提高代码效率的方法。