Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rectangle/circle intersection points using boost library

Is it possible to get rectangle x circle intersection points with boost? As far as I can see boost has an intersection function:

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

But I can't find how can I even construct a circular geometry. I mean, I can create a polygon with lots of vertices, but it's not the kind of representation I'm looking for

like image 510
user1496491 Avatar asked Oct 18 '25 15:10

user1496491


2 Answers

If you want to get circle using boost you can use boost::geometry::buffer algorithm. Details here.

You need to pass point as input geometry (center of your circle) and radius as distance_strategy. Full test code below

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";
}

Prints the intersection as

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))

And you can see the result of the buffer is:

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)))

I added the picture as output, the dark semicircle is intersection geometry: enter image description here

like image 135
rafix07 Avatar answered Oct 20 '25 04:10

rafix07


A polygon approximation is the usual approach.

The easy way to construct one is to use buffer with the point_circle strategy:

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

like image 36
sehe Avatar answered Oct 20 '25 06:10

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!