[英]Rotate a polygon using boost geometry
I am trying to rotate a polygon using boost geometry. 我正在尝试使用boost几何旋转多边形。 Probably I am doing something wrong.
可能我做错了什么。 I have a polygon, not centered in the origin, declared like this:
我有一个多边形,不在原点居中,声明如下:
Polygon _poly;
Polygon _poly2;
Point2D A(4,3);
Point2D B(4,5);
Point2D C(6,5);
Point2D D(6,3);
Point2D CLOSE(4,3);
_poly.outer().push_back(A);
_poly.outer().push_back(B);
_poly.outer().push_back(C);
_poly.outer().push_back(D);
Then, I perform a rotation with: 然后,我执行旋转:
boost::geometry::strategy::transform::rotate_transformer<boost::geometry::degree, double, 2, 2> rotate(45.0);
But the resulting coordinates of the polygon are not the correct ones: 但是多边形的结果坐标不正确:
poly's coordinates: 4 3 4 5 6 5 6 3 poly的坐标:4 3 4 5 6 5 6 3
rotated coordinates: 4 0 6 0 7 0 6 -2 旋转坐标:4 0 6 0 7 0 6 -2
What I have to do? 我该怎么办?
You polygon is invalid (see the documentation). 多边形无效(请参阅文档)。 This is easy to check with
is_valid
. 使用
is_valid
很容易检查。
If you don't know the input source, you can always try to correct with boost::geometry::correct
: 如果您不知道输入源,可以尝试使用
boost::geometry::correct
:
#include <iostream>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/algorithms/is_valid.hpp>
#include <boost/geometry/algorithms/transform.hpp>
namespace bg = boost::geometry;
typedef bg::model::point<double, 2, bg::cs::cartesian> Point2D;
typedef bg::model::polygon<Point2D> Polygon;
//typedef bg::model::box<Point2D> box;
int main() {
Polygon _poly;
Polygon _poly2;
Point2D A(4,3);
Point2D B(4,5);
Point2D C(6,5);
Point2D D(6,3);
Point2D CLOSE(4,3);
_poly.outer().push_back(A);
_poly.outer().push_back(B);
_poly.outer().push_back(C);
_poly.outer().push_back(D);
std::cout << std::boolalpha << bg::is_valid(_poly) << "\n";
bg::correct(_poly);
std::cout << std::boolalpha << bg::is_valid(_poly) << "\n";
}
Output: 输出:
false
true
In this case you clearly forgot to add the CLOSE
point 在这种情况下,您显然忘记添加
CLOSE
点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.