我在使用 Boost.Polygon 中的 get_rectangles(output_container_type& output, const T& Polygon_set)
方法将曼哈顿(直线)多边形切成矩形时遇到问题。它似乎不适用于自相交的多边形,例如下面的多边形。
这是我的尝试:
#include <iostream>
#include <boost/polygon/polygon.hpp>
#include <vector>
namespace gtl = boost::polygon;
using namespace boost::polygon::operators;
int main() {
typedef gtl::polygon_90_with_holes_data<int> Polygon;
typedef gtl::polygon_traits<Polygon>::point_type Point;
Point pts[] = {gtl::construct<Point>(0, 0), // See the image
gtl::construct<Point>(0, 10),
gtl::construct<Point>(30, 10),
gtl::construct<Point>(30, 20),
gtl::construct<Point>(10, 20),
gtl::construct<Point>(10, 0)};
Polygon poly;
gtl::set_points(poly, pts, pts+6);
std::vector< gtl::rectangle_data<int> > rects;
get_rectangles(rects, poly);
std::cout << rects.size() << " rectangle: \n";
for(std::vector<gtl::rectangle_data<int> >::iterator it = rects.begin(); it !=
rects.end(); ++it) {
// Print out the corner coordinates
std::cout << "x1: "<< gtl::xl(*it) << ", x2: " << gtl::xh(*it)
<< ", y1: "<< gtl::yl(*it) << ", y2: " << gtl::yh(*it) << std::endl;
}
return 0;
}
这是输出:
1 rectangle:
x1: 10, x2: 30, y1: 10, y2: 20
此方法适用于不相交的多边形,但此处仅生成一个矩形,而不是两个。 This似乎表明我想做的事情应该是可能的。我做错了什么,或者这可能吗?
最佳答案
您的代码正在对多边形代码进行“压力测试”。严格来说,您的点列表没有定义多边形。理论上,平面区域(特别是多边形)只能由形成周长的所谓乔丹(简单)曲线来很好地定义。用最简单的话来说,乔丹(简单)曲线不得与自身相交。
当使用具有以下 8 个点列表的乔丹(简单)曲线重新定义相同的形状时,
Point pts[] = {gtl::construct<Point>(0, 0),
gtl::construct<Point>(0, 10),
gtl::construct<Point>(10, 10),
gtl::construct<Point>(10, 20),
gtl::construct<Point>(30, 20),
gtl::construct<Point>(30, 10),
gtl::construct<Point>(10, 10),
gtl::construct<Point>(10, 0)};
您的代码产生正确的结果:
2个矩形: x1:0,x2:10,y1:0,y2:10 x1:10,x2:30,y1:10,y2:20
关于c++ - 使用 Boost.Polygon 对曼哈顿多边形进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24064483/