使用 boost 库获取 rectangle/circle 个交点

Getting rectangle/circle intersection points using boost library

是否可以得到矩形x圆与boost的交点?据我所知 boost 有一个交集函数:

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

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

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

构造一个的简单方法是使用 bufferpoint_circle 策略:

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

如果你想使用 boost 获得圆,你可以使用 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)))

我添加了图片作为输出,深色半圆是相交几何: