在多边形旋转后,boost :: geometry polygon的outer()具有错误的坐标

时间:2015-06-17 03:18:03

标签: c++ boost boost-geometry

我正在使用boost 1.56-rhel5版本。旋转多边形后,当我使用outer()获取多边形的所有点,然后获取它们的坐标时,会出现错误的坐标结果。

这是我的代码:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <iostream>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double>    point_type;
    typedef boost::geometry::model::polygon<point_type>     polygon_type;

    polygon_type poly;
    polygon_type poly2;

    boost::geometry::read_wkt( "POLYGON((0 0 0 2 1 2 1 0))", poly);

    // Rotate poly to get poly2
    boost::geometry::strategy::transform::rotate_transformer<boost::geometry::degree, double, 2, 2> rotate (180);
    boost::geometry::transform(poly, poly2, rotate);

    // Print the coordinates of poly2
    std::cout << "poly2's coordinates:" << std::endl;
    for (unsigned i = 0; i != poly2.outer().size(); ++i) {
        int x = boost::geometry::get<0>(poly2.outer()[i]);
        int y = boost::geometry::get<1>(poly2.outer()[i]);
        std::cout << " " << x;
        std::cout << " " << y;
    }
    std::cout << std::endl;

    // Verify whether poly2 is correctly rotated
    point_type p2(-2,-2);
    std::cout << "Distance between p2 and poly2: " << boost::geometry::distance(p2, poly2) << std::endl;
}

这是打印结果:

poly2的坐标:

0 0 0 -2 0 -2 -1 0

p2和poly2之间的距离: 1

预期结果是:

poly2的坐标:

0 0 0 -2 -1 -2 -1 0

p2和poly2之间的距离: 1

“p2和poly2:1之间的距离”是正确的,这意味着旋转效果很好。但是poly2的坐标是错误的。第三点应为-1 -2,而不是0 -2。

这是boost :: geometry的错误吗?或者我有任何误解或以错误的方式使用它?

1 个答案:

答案 0 :(得分:-1)

这是由于使用整数来存储坐标。将double值赋给整数时,会进行强制转换。例如,-0.9999999变为0。 应该使用双重类型来存储。

// Print the coordinates of poly2
std::cout << "poly2's coordinates:" << std::endl;
for (unsigned i = 0; i != poly2.outer().size(); ++i) {
    int x = boost::geometry::get<0>(poly2.outer()[i]);
    int y = boost::geometry::get<1>(poly2.outer()[i]);
    std::cout << " " << x;
    std::cout << " " << y;
}

应改为:

// Print the coordinates of poly2
std::cout << "poly2's coordinates:" << std::endl;
for (unsigned i = 0; i != poly2.outer().size(); ++i) {
    double x = boost::geometry::get<0>(poly2.outer()[i]);
    double y = boost::geometry::get<1>(poly2.outer()[i]);
    std::cout << " " << x;
    std::cout << " " << y;
}