使用Boost库获取矩形/圆形相交点

时间:2018-07-16 14:48:30

标签: c++ boost boost-geometry

是否可以使用boost获得矩形x圆的交点?据我所知boost具有交集功能:

template<typename Geometry1, typename Geometry2, typename GeometryOut>
bool intersection(Geometry1 const & geometry1, Geometry2 const & geometry2, GeometryOut & geometry_out)

但是我找不到如何构造圆形几何体的方法。我的意思是,我可以创建一个具有很多顶点的多边形,但这不是我想要的表示形式

2 个答案:

答案 0 :(得分:4)

如果您想使用增强来绕圈,可以使用boost::geometry::buffer算法。详细信息here

您需要将点作为输入几何(圆心),将半径作为 distance_strategy 。完整的测试代码如下

Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <iostream>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point;
    typedef boost::geometry::model::polygon<point> polygon;

    const double buffer_distance = 1.0; // radius of circle
    const int points_per_circle = 36;
    boost::geometry::strategy::buffer::distance_symmetric<double> distance_strategy(buffer_distance);
    boost::geometry::strategy::buffer::join_round join_strategy(points_per_circle);
    boost::geometry::strategy::buffer::end_round end_strategy(points_per_circle);
    boost::geometry::strategy::buffer::point_circle circle_strategy(points_per_circle);
    boost::geometry::strategy::buffer::side_straight side_strategy;

    boost::geometry::model::multi_polygon<polygon> result;

    point pt;
    boost::geometry::read_wkt("POINT(5 5)", pt); // center of circle

    boost::geometry::buffer(pt, result,
                distance_strategy, side_strategy,
                join_strategy, end_strategy, circle_strategy);
    // first item of result is circle with 1 radius and (5,5) point as center
    // result should have 1 polygon
    polygon rect; // your rectangle
    boost::geometry::read_wkt("POLYGON((3 3,3 7,5 7,5 3,3 3))",rect);

    std::deque<polygon> intersectionGeometry;
    boost::geometry::intersection(rect,result.front(),intersectionGeometry);
    if (intersectionGeometry.size() == 1)
        std::cout << boost::geometry::wkt(intersectionGeometry.front()) << std::endl; // intersection

    std::cout << boost::geometry::wkt(result) << "\n";
}

将相交打印为

POLYGON((5 4,4.82635 4.01519,4.65798 4.06031,4.5 4.13397,4.35721 4.23396,4.23396 4.35721,4.13397 4.5,4.06031 4.65798,4.01519 4.82635,4 5,4.01519 5.17365,4.06031 5.34202,4.13397 5.5,4.23396 5.64279,4.35721 5.76604,4.5 5.86603,4.65798 5.93969,4.82635 5.98481,5 6,5 4))

您会看到缓冲区的result是:

MULTIPOLYGON(((6 5,5.98481 4.82635,5.93969 4.65798,5.86603 4.5,5.76604 4.35721,5.64279 4.23396,5.5 4.13397,5.34202 4.06031,5.17365 4.01519,5 4,4.82635 4.01519,4.65798 4.06031,4.5 4.13397,4.35721 4.23396,4.23396 4.35721,4.13397 4.5,4.06031 4.65798,4.01519 4.82635,4 5,4.01519 5.17365,4.06031 5.34202,4.13397 5.5,4.23396 5.64279,4.35721 5.76604,4.5 5.86603,4.65798 5.93969,4.82635 5.98481,5 6,5.17365 5.98481,5.34202 5.93969,5.5 5.86603,5.64279 5.76604,5.76604 5.64279,5.86603 5.5,5.93969 5.34202,5.98481 5.17365,6 5)))

我添加了图片作为输出,暗半圆是交点几何: enter image description here

答案 1 :(得分:1)

多边形逼近是通常的方法。

构造一个简单的方法是将<div *ngFor="obj of details"> <p>{{obj.key}}</p> <p>{{obj.value}}</p> <p></p> </div> buffer策略结合使用:

https://www.boost.org/doc/libs/master/libs/geometry/doc/html/geometry/reference/strategies/strategy_buffer_point_circle.html