【问题标题】:Path between two points inside a Boost Geometry polygonBoost Geometry 多边形内两点之间的路径
【发布时间】:2021-08-07 07:32:13
【问题描述】:

我正在尝试找到连接 c++ boost 多边形中两个点的“路径”。

我有一个这样的多边形

namespace bgm = bg::model;
using Point = bgm::d2::point_xy<double>;
bgm::polygon<Point> poly;

bg::read_wkt("POLYGON((" + points_poly + "))", poly);

我在多边形中有随机点。

我想知道 boost c++ 是否有工具可以在这种情况下提供帮助。

【问题讨论】:

    标签: c++ boost boost-geometry


    【解决方案1】:

    目前还不清楚实际的问题是什么,所以这里直接回答您的问题:

    Poly poly{{
        {-4, 14}, {17.75, 13.99}, {17.75, 7.95},  {10, 8},         {10, 2},
        {16, 2},  {16, -8},       {22, -8},       {13.97, -13.17}, {6, -8},
        {12, -8}, {12, -2},       {-0.99, -2.06}, {-0.9, -7.95},   {-18, -8},
        {-18, 2}, {-10, 2},       {-10, 6},       {-6, 6},         {-6, 2},
        {-2, 8},  {-7.05, 8},     {-7.15, 14},    {-4, 14},
    }};
    
    Point A{-7.08, 10.07};
    Point B{10, 4};
    
    LineString path{A, {4, 10}, {4, 4}, B};
    

    这些的 WKT 是:

    poly: POLYGON((-4 14,17.75 13.99,17.75 7.95,10 8,10 2,16 2,16 -8,22 -8,13.97
    -13.17,6 -8,12 -8,12 -2,-0.99 -2.06,-0.9 -7.95,-18 -8,-18 2,-10 2,-10 6,-6 6,
    -6 2,-2 8,-7.05 8,-7.15 14,-4 14))
    A: POINT(-7.08 10.07)
    B: POINT(10 4)
    path: LINESTRING(-7.08 10.07,4 10,4 4,10 4)
    

    将其转换为 SVG 图像:

    Live On Coliru

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    #include <boost/geometry/geometries/linestring.hpp>
    #include <iostream>
    #include <fstream>
    
    namespace bg  = boost::geometry;
    namespace bgm = bg::model;
    
    using Point      = bgm::d2::point_xy<double>;
    using Poly       = bgm::polygon<Point>;
    using LineString = bgm::linestring<Point>;
    
    int main() {
        Poly poly{{
            {-4, 14}, {17.75, 13.99}, {17.75, 7.95},  {10, 8},         {10, 2},
            {16, 2},  {16, -8},       {22, -8},       {13.97, -13.17}, {6, -8},
            {12, -8}, {12, -2},       {-0.99, -2.06}, {-0.9, -7.95},   {-18, -8},
            {-18, 2}, {-10, 2},       {-10, 6},       {-6, 6},         {-6, 2},
            {-2, 8},  {-7.05, 8},     {-7.15, 14},    {-4, 14},
        }};
    
        Point A{-7.08, 10.07};
        Point B{10, 4};
    
        LineString path{A, {4, 10}, {4, 4}, B};
    
        std::cout << "poly: " << bg::wkt(poly) << "\n";
        std::cout << "A: " << bg::wkt(A) << "\n";
        std::cout << "B: " << bg::wkt(B) << "\n";
        std::cout << "path: " << bg::wkt(path) << "\n";
    
        {
            std::ofstream svg("output.svg");
            boost::geometry::svg_mapper<Point> mapper(svg, 400, 400);
            mapper.add(poly);
            mapper.add(path);
            mapper.add(A);
            mapper.add(B);
    
            mapper.map(poly, "fill-opacity:0.3;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
            mapper.map(path, "fill-opacity:0.05;fill:rgb(255,0,0);stroke:rgb(255,0,0);stroke-width:2");
            mapper.map(A, "fill:red;stroke-width:20");
            mapper.map(B, "fill:red;stroke-width:20");
    
            mapper.text(A, "A", "");
            mapper.text(B, "B", "");
        }
    }
    

    打印 WKT(见上文)以及写 output.svg

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      相关资源
      最近更新 更多