首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::几何学::model::linestring与boost::几何学::model::polygon的交集

boost::几何学::model::linestring与boost::几何学::model::polygon的交集
EN

Stack Overflow用户
提问于 2016-08-07 01:16:18
回答 1查看 1.6K关注 0票数 1

我试图找到一个线字符串的一部分,是在一个多边形内。我尝试了intersection函数,但它似乎只是找到实际的交点,而不是线串中重叠多边形的部分。有什么办法能得到这个东西吗?

下面是一个演示情况:

代码语言:javascript
复制
#include <iostream>
#include <fstream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/svg/svg_mapper.hpp>
#include <boost/geometry/geometries/linestring.hpp>

using point_type = boost::geometry::model::d2::point_xy<double>;
using polygon_type = boost::geometry::model::polygon<point_type>;
using linestring_type = boost::geometry::model::linestring<point_type>;

int main()
{
    polygon_type polygon;

    polygon.outer().push_back(point_type{10,10});
    polygon.outer().push_back(point_type{12,10});
    polygon.outer().push_back(point_type{12,12});
    polygon.outer().push_back(point_type{10,12});
    polygon.outer().push_back(point_type{10,10});

    linestring_type linestring;
    linestring.push_back(point_type{11,9});
    linestring.push_back(point_type{11,11});
    linestring.push_back(point_type{13,11});

    // Expected intersections at (11, 10) and (12, 11)

    std::ofstream svg("both.svg");

    linestring_type output;
    boost::geometry::intersection(polygon, linestring, output);

    for(auto iter = output.begin(); iter != output.end(); ++iter) {
        std::cout << boost::geometry::get<0>(*iter) << " " << boost::geometry::get<1>(*iter) << std::endl;
    }
// The output is:
//    11 10
//    12 11

// But I want it to be:
//    11 10
//    11 11
//    12 11
    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-07 01:38:11

似乎必须使用multi_linestring作为输出类型:

代码语言:javascript
复制
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>

using point_type = boost::geometry::model::d2::point_xy<double>;
using polygon_type = boost::geometry::model::polygon<point_type>;
using linestring_type = boost::geometry::model::linestring<point_type>;
using multi_linestring_type = boost::geometry::model::multi_linestring<linestring_type>;

int main()
{
    polygon_type polygon;

    polygon.outer().push_back(point_type{10,10});
    polygon.outer().push_back(point_type{10,12});
    polygon.outer().push_back(point_type{12,12});
    polygon.outer().push_back(point_type{12,10});
    polygon.outer().push_back(point_type{10,10});

    linestring_type linestring;
    linestring.push_back(point_type{11,9});
    linestring.push_back(point_type{11,11});
    linestring.push_back(point_type{13,11});

    // Expected intersections at (11, 10) and (12, 11)

    multi_linestring_type intersection;
    boost::geometry::intersection(polygon, linestring, intersection);

    for(auto intersectionIter = intersection.begin(); intersectionIter != intersection.end(); ++intersectionIter) {
        linestring_type intersectionPiece = *intersectionIter;
        std::cout << "Piece:" << std::endl;
        for(auto intersectionPieceIter = intersectionPiece.begin(); intersectionPieceIter != intersectionPiece.end(); ++intersectionPieceIter) {
            std::cout << boost::geometry::get<0>(*intersectionPieceIter) << " " << boost::geometry::get<1>(*intersectionPieceIter) << std::endl;
        }

    }

    return 0;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38810048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档