Boost 多边形仅支持硬编码点数据
我正在尝试将多边形分割成一堆矩形。为此,我正在使用 boost 多边形库。 当我尝试从用户那里获取输入(例如此处的向量)时,程序会给出意想不到的结果,但当硬编码(在代码中注释)给出正确结果时,会给出相同的值。
#include <iostream>
#include <vector>
#include <boost/polygon/polygon.hpp>
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[7];
std::vector <double> vec {0.0,0.0, 0.0, 235.0, 170.0, 235.0, 170.0, 305.0, 310.0, 305.0, 310.0, 0.0, 0.0, 0.0};
for (int i = 0 ; i< 7 ; i++) {
pts[i] = gtl::construct<Point>(vec[i],vec[i+1]);
}
/*
pts[0] = gtl::construct<Point>(0.0 , 0.0) ;
pts[1] = gtl::construct<Point>(0.0 , 235.0) ;
pts[2] = gtl::construct<Point>(170.0 , 235.0) ;
pts[3] = gtl::construct<Point>(170.0 , 305.0) ;
pts[4] = gtl::construct<Point>(310.0 , 305.0) ;
pts[5] = gtl::construct<Point>(310.0 , 0.0) ;
pts[6] = gtl::construct<Point>(0.0 , 0.0) ;
*/
Polygon poly;
gtl::set_points(poly, pts, pts+7);
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;
}
输出:
以向量作为
0 rectangle:
带有硬编码值的输入
2 rectangle:
1: 0, x2: 170, y1: 0, y2: 235
x1: 170, x2: 310, y1: 0, y2: 305
I am trying to split polygons into bunch of rectangles. For that I am using boost polygon library.
When i try to take input from user (in vector for e.g. here) the program gives unintended result but same values when hard-coded (commented in the code) gives right result.
#include <iostream>
#include <vector>
#include <boost/polygon/polygon.hpp>
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[7];
std::vector <double> vec {0.0,0.0, 0.0, 235.0, 170.0, 235.0, 170.0, 305.0, 310.0, 305.0, 310.0, 0.0, 0.0, 0.0};
for (int i = 0 ; i< 7 ; i++) {
pts[i] = gtl::construct<Point>(vec[i],vec[i+1]);
}
/*
pts[0] = gtl::construct<Point>(0.0 , 0.0) ;
pts[1] = gtl::construct<Point>(0.0 , 235.0) ;
pts[2] = gtl::construct<Point>(170.0 , 235.0) ;
pts[3] = gtl::construct<Point>(170.0 , 305.0) ;
pts[4] = gtl::construct<Point>(310.0 , 305.0) ;
pts[5] = gtl::construct<Point>(310.0 , 0.0) ;
pts[6] = gtl::construct<Point>(0.0 , 0.0) ;
*/
Polygon poly;
gtl::set_points(poly, pts, pts+7);
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;
}
output:
with vector as input
0 rectangle:
with hardcode value
2 rectangle:
1: 0, x2: 170, y1: 0, y2: 235
x1: 170, x2: 310, y1: 0, y2: 305
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议避免出现问题并使代码更安全:
Live On Coliru
打印
I'd suggest avoiding problems and making the code much safer:
Live On Coliru
Prints