Boost Geometry 多边形内两点之间的路径

Posted

技术标签:

【中文标题】Boost Geometry 多边形内两点之间的路径【英文标题】:Path between two points inside a Boost Geometry polygon 【发布时间】: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++ 是否有工具可以在这种情况下提供帮助。

【问题讨论】:

【参考方案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 B10, 4;

LineString pathA, 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 B10, 4;

    LineString pathA, 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

【讨论】:

以上是关于Boost Geometry 多边形内两点之间的路径的主要内容,如果未能解决你的问题,请参考以下文章

提高内点的几何多边形距离

使用具有线段属性的Boost :: Geometry Polygon布尔值/交叉点

在 boost 几何中创建实心多边形

用 boost::geometry 扩展多边形?

奇怪的行为将 boost::geometry::within() 用于多边形和环

Boost Geometry:联合多个多边形 C++