gpt4 book ai didi

c++ - 使用 boost 几何旋转多边形

转载 作者:太空狗 更新时间:2023-10-29 21:40:09 25 4
gpt4 key购买 nike

我正在尝试使用 boost 几何体旋转多边形。可能我做错了什么。我有一个不以原点为中心的多边形,声明如下:

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);

然后,我执行轮换:

  boost::geometry::strategy::transform::rotate_transformer<boost::geometry::degree, double, 2, 2> rotate(45.0);

但是得到的多边形坐标不正确:

多边形的坐标: 4 3 4 5 6 5 6 3

旋转坐标: 4 0 6 0 7 0 6 -2

我必须做什么?

最佳答案

您的多边形无效(请参阅文档)。这很容易用 is_valid 来检查。

如果您不知道输入源,您可以随时尝试使用 boost::geometry::correct 进行更正:

Live On Coliru

#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";
}

输出:

false
true

在这种情况下,您显然忘记了添加CLOSE

关于c++ - 使用 boost 几何旋转多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31098496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com