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

Path between two points inside a Boost Geometry polygon

我试图找到连接 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++ 是否有工具可以帮助处理这种情况。

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

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: