Rotate a polygon using forced geometry
I am trying to rotate a polygon using blowing geometry. I am probably doing something wrong. I have a polygon, not centered in 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 rotate with:
boost::geometry::strategy::transform::rotate_transformer<boost::geometry::degree, double, 2, 2> rotate(45.0);
But the resulting polygon coordinates are not correct:
poly coordinates: 4 3 4 5 6 5 6 3
rotated coordinates: 4 0 6 0 7 0 6 -2
What do I need to do?
+3
source to share
1 answer
Polygon is invalid (see documentation). This can be easily verified with is_valid
.
If you don't know the source of the input, you can always try to fix it with 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 a dot CLOSE
+2
source to share