Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

model::multi_polygon

multi_polygon, a collection of polygons

Description

Multi-polygon can be used to group polygons belonging to each other, e.g. Hawaii

Model of

MultiPolygon Concept

Synopsis

template<typename Polygon, template< typename, typename > class Container, template< typename > class Allocator>
class model::multi_polygon
      : public std::vector< Polygon, std::allocator< Polygon > >
{
  // ...
};

Template parameter(s)

Parameter

Default

Description

typename Polygon

template< typename, typename > class Container

std::vector

template< typename > class Allocator

std::allocator

Constructor(s)

Function

Description

Parameters

multi_polygon()

Default constructor, creating an empty multi_polygon.

multi_polygon(std::initializer_list< Polygon > l)

Constructor taking std::initializer_list, filling the multi_polygon.

std::initializer_list< Polygon >: l:

Header

Either

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

Or

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

Examples

Declaration and use of the Boost.Geometry model::multi_polygon, modelling the MultiPolygon Concept

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

namespace bg = boost::geometry;

int main()
{
    typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
    typedef bg::model::polygon<point_t> polygon_t; 1
    typedef bg::model::multi_polygon<polygon_t> mpolygon_t; 2

    mpolygon_t mpoly1; 3
    mpolygon_t mpoly2{{{{0.0, 0.0}, {0.0, 5.0}, {5.0, 5.0}, {5.0, 0.0}, {0.0, 0.0}},
                       {{1.0, 1.0}, {4.0, 1.0}, {4.0, 4.0}, {1.0, 4.0}, {1.0, 1.0}}},
                      {{{5.0, 5.0}, {5.0, 6.0}, {6.0, 6.0}, {6.0, 5.0}, {5.0, 5.0}}}}; 4

    mpoly1.resize(2); 5

    bg::append(mpoly1[0].outer(), point_t(0.0, 0.0)); 6
    bg::append(mpoly1[0].outer(), point_t(0.0, 5.0));
    bg::append(mpoly1[0].outer(), point_t(5.0, 5.0));
    bg::append(mpoly1[0].outer(), point_t(5.0, 0.0));
    bg::append(mpoly1[0].outer(), point_t(0.0, 0.0));

    mpoly1[0].inners().resize(1); 7
    bg::append(mpoly1[0].inners()[0], point_t(1.0, 1.0)); 8
    bg::append(mpoly1[0].inners()[0], point_t(4.0, 1.0));
    bg::append(mpoly1[0].inners()[0], point_t(4.0, 4.0));
    bg::append(mpoly1[0].inners()[0], point_t(1.0, 4.0));
    bg::append(mpoly1[0].inners()[0], point_t(1.0, 1.0));

    bg::append(mpoly1[1].outer(), point_t(5.0, 5.0)); 9
    bg::append(mpoly1[1].outer(), point_t(5.0, 6.0));
    bg::append(mpoly1[1].outer(), point_t(6.0, 6.0));
    bg::append(mpoly1[1].outer(), point_t(6.0, 5.0));
    bg::append(mpoly1[1].outer(), point_t(5.0, 5.0));

    double a = bg::area(mpoly1);

    std::cout << a << std::endl;

    return 0;
}

1

Default parameters, clockwise, closed polygon.

2

Clockwise, closed multi_polygon.

3

Default-construct a multi_polygon.

4

Construct a multi_polygon containing two polygons, using C++11 unified initialization syntax.

5

Resize a multi_polygon, store two polygons.

6

Append point to the exterior ring of the first polygon.

7

Resize a container of interior rings of the first polygon.

8

Append point to the interior ring of the first polygon.

9

Append point to the exterior ring of the second polygon.

Output:

17

PrevUpHomeNext