我寫了我自己的功能,這樣做。它可能不漂亮,但它的工作原理。我首先確定了最高和最低的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();
}
}
這當然不是最好的解決方案,但我會歡迎提高我的代碼效率的方法。