Elmi
Elmi

Reputation: 6193

Expand polygons with boost::geometry?

I know it is possible to add/subtract different polygons to/from each other via boost::geometry, examples can be found at different places in web. Now what I want to do is something different:

I have a 2D polygon which I want to expand/shrink by a given size. So I'm not talking about a simple scaling-operation but about a function, which calculates a new polygon out of the input data:

My question: is such an operation possible with boost::geometry? If yes, how can this be done?

Thanks!

Upvotes: 2

Views: 2379

Answers (1)

sehe
sehe

Reputation: 393064

This feature is called "Buffering" in the OGC Simple Feature Specification.

Boost Geometry supports it for most 2D cartesian geometries (and you can easily do the rest by conversion), and points only in other coordinate systems.

Documentation

The available strategies: enter image description here

Here's the sample code

Live On Coliru

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


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

    // Declare strategies
    const double buffer_distance = 1.0;
    const int points_per_circle = 36;
    boost::geometry::strategy::buffer::distance_symmetric<coordinate_type> 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;

    // Declare output
    boost::geometry::model::multi_polygon<polygon> result;

    // Declare/fill a linestring
    boost::geometry::model::linestring<point> ls;
    boost::geometry::read_wkt("LINESTRING(0 0,4 5,7 4,10 6)", ls);

    // Create the buffer of a linestring
    boost::geometry::buffer(ls, result,
                distance_strategy, side_strategy,
                join_strategy, end_strategy, circle_strategy);


    // Declare/fill a multi point
    boost::geometry::model::multi_point<point> mp;
    boost::geometry::read_wkt("MULTIPOINT((3 3),(4 4),(6 2))", mp);

    // Create the buffer of a multi point
    boost::geometry::buffer(mp, result,
                distance_strategy, side_strategy,
                join_strategy, end_strategy, circle_strategy);


    // Declare/fill a multi_polygon
    boost::geometry::model::multi_polygon<polygon> mpol;
    boost::geometry::read_wkt("MULTIPOLYGON(((0 1,2 5,5 3,0 1)),((1 1,5 2,5 0,1 1)))", mpol);

    // Create the buffer of a multi polygon
    boost::geometry::buffer(mpol, result,
                distance_strategy, side_strategy,
                join_strategy, end_strategy, circle_strategy);


    return 0;
}

Points can "grow together" like this enter image description here

The linestring example with round corners: enter image description here

Upvotes: 3

Related Questions