在boost :: geometry中初始化多边形

问题描述 投票:4回答:3

我是通用几何库的新手,建议包含在boost中:

http://geometrylibrary.geodan.nl/

我有两个向量vector<int> Xb, Yb,我试图创建一个多边形。我试图获得以下代码片段的内容:

 polygon_2d P;

 vector<double>::const_iterator xi;
 vector<double>::const_iterator yi;

 for (xi=Xb.begin(), yi=Yb.begin(); xi!=Xb.end(); ++xi, ++yi)
  P.push_back (make<point_2d>(*xi, *yi));

上面的代码不起作用,抱怨P没有push_back成员函数。如何从具有坐标vector<int> Xb,vector<int> Yb的点初始化多边形?

c++ boost polygon boost-geometry
3个回答
6
投票
append(P, make<point_2d>(*xi, *yi));

12
投票

以下是您在Kirill的答案下面作为评论提出的原始问题的扩展示例:多边形之间的交叉点是否可能?

是的,Boost.Geometry (aka GGL)支持多边形 - 多边形交叉点

#include <iostream>
#include <vector>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/cartesian2d.hpp>
#include <boost/geometry/geometries/adapted/c_array_cartesian.hpp>

using namespace boost::geometry;

int main(void)
{
    // Define a polygons and fill the outer rings.
    polygon_2d a;
    {
        const double c[][2] = {
            {160, 330}, {60, 260}, {20, 150}, {60, 40}, {190, 20}, {270, 130}, {260, 250}, {160, 330}
        };
        assign(a, c);
    }
    correct(a);
    std::cout << "A: " << dsv(a) << std::endl;

    polygon_2d b;
    {
        const double c[][3] = {
            {300, 330}, {190, 270}, {150, 170}, {150, 110}, {250, 30}, {380, 50}, {380, 250}, {300, 330}
        };
        assign(b, c);
    }
    correct(b);
    std::cout << "B: " << dsv(b) << std::endl;

    // Calculate interesection
    typedef std::vector<polygon_2d > polygon_list;
    polygon_list v;

    intersection_inserter<polygon_2d>(a, b, std::back_inserter(v));
    std::cout << "Intersection of polygons A and B" << std::endl;
    for (polygon_list::const_iterator it = v.begin(); it != v.end(); ++it)
    {
        std::cout << dsv(*it) << std::endl;
    }

    return 0;
}

Here是结果(多边形交叉向南移动以获得更好的可见性):

我希望它对你有用。


0
投票

您还可以使用元组初始化多边形

#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

boost::geometry::assign_points(
    polygon, boost::assign::tuple_list_of
        (300, 330) (190, 270) (150, 170) (150, 110) (250, 30) (380, 50)
        (380, 250) (300, 330)
);
© www.soinside.com 2019 - 2024. All rights reserved.