【问题标题】:How fill the polygon in boost?如何在boost中填充多边形?
【发布时间】:2014-09-28 04:54:05
【问题描述】:

我有这样的代码:

typedef boost::geometry::model::polygon Polygon;

多边形边界;

我需要填充边界。我认为这一定很容易,但我从未使用过 boost,而且我还没有找到这方面的说明。我查看了许多示例,但它们没有包含所需的操作。 我尝试使用项目 umeshu https://github.com/vladimir-ch/umeshu/ 创建具有良好三角剖分的网格。我只需要了解如何填充初始数据。

【问题讨论】:

    标签: c++ boost polygon triangulation boost-geometry


    【解决方案1】:

    你想要的界面是:

    //! This refers to the exterior ring of the polygon.
    inline ring_type& outer() { return m_outer; }
    
    //! This refers to a collection of rings which are holes inside the polygon.
    inline inner_container_type & inners() { return m_inners; }
    

    默认情况下 ring_type 是 std::vector ,其中 Point 是您指定的模板参数(在您的情况下为 Point2。)

    试试:

    boundary.outer().push_back(Point2(x, y)); //This fills the exterior boundary with one point whose coordinates are x and y.
    

    这是一个完整的示例:

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    
    #include <iostream>
    
    namespace bg = boost::geometry;
    
    int main(void)
    {
        typedef bg::model::point<double, 2, bg::cs::cartesian> point;
        typedef bg::model::polygon<point> polygon;
    
        //! create a polygon
        polygon p;
        p.outer().push_back(point(0., 0.));
        p.outer().push_back(point(1., 0.));
        p.outer().push_back(point(1., 2.));
        p.outer().push_back(point(2., 3.));
        p.outer().push_back(point(0., 4.));
    
        //! display it
        std::cout << "generated polygon:" << std::endl;
        std::cout << bg::wkt<polygon>(p) << std::endl;
    
        return 0;
    }
    

    输出:

    generated polygon:
    POLYGON((0 0,1 0,1 2,2 3,0 4))
    Press any key to continue . . .
    

    【讨论】:

    • 感谢您的回答,但是我通过这种方式遇到了运行时错误((
    • boundary.outer().resize(100); - 此代码也会引发运行时错误。
    • 谢谢,它可以工作......我不知道如何......但我的代码也是。我今天刚来上班并运行我的应用程序。我认为问题的原因在于我的图书馆。昨天我安装了很多不同的应用程序,其中包含许多库和 boost。可能会出现一些问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多