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::polygon

The polygon contains an outer ring and zero or more inner rings.

Model of

Polygon Concept

Synopsis

template<typename Point, bool ClockWise, bool Closed, template< typename, typename > class PointList,
         template< typename, typename > class RingList, template< typename > class PointAlloc, template< typename > class RingAlloc>
class model::polygon
{
  // ...
};

Template parameter(s)

Parameter

Default

Description

typename Point

point type

bool ClockWise

true

true for clockwise direction, false for CounterClockWise direction

bool Closed

true

true for closed polygons (last point == first point), false open points

template< typename, typename > class PointList

std::vector

container type for points, for example std::vector, std::list, std::deque

template< typename, typename > class RingList

std::vector

container type for inner rings, for example std::vector, std::list, std::deque

template< typename > class PointAlloc

std::allocator

container-allocator-type, for the points

template< typename > class RingAlloc

std::allocator

container-allocator-type, for the rings

Constructor(s)

Function

Description

Parameters

polygon()

Default constructor, creating an empty polygon.

polygon(std::initializer_list< ring_type > l)

Constructor taking std::initializer_list, filling the polygon.

std::initializer_list< ring_type >: l:

Member Function(s)

Function

Description

Parameters

Returns

ring_type const & outer()

inner_container_type const & inners()

ring_type & outer()

inner_container_type & inners()

void clear()

Utility method, clears outer and inner rings.

Header

Either

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

Or

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

Examples

Declaration and use of the Boost.Geometry model::polygon, modelling the Polygon 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

    polygon_t poly1; 2

#if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) \
 && !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)

    polygon_t polygon2{{{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}}}; 3

#endif

    bg::append(poly1.outer(), point_t(0.0, 0.0)); 4
    bg::append(poly1.outer(), point_t(0.0, 5.0));
    bg::append(poly1.outer(), point_t(5.0, 5.0));
    bg::append(poly1.outer(), point_t(5.0, 0.0));
    bg::append(poly1.outer(), point_t(0.0, 0.0));

    poly1.inners().resize(1); 5
    bg::append(poly1.inners()[0], point_t(1.0, 1.0)); 6
    bg::append(poly1.inners()[0], point_t(4.0, 1.0));
    bg::append(poly1.inners()[0], point_t(4.0, 4.0));
    bg::append(poly1.inners()[0], point_t(1.0, 4.0));
    bg::append(poly1.inners()[0], point_t(1.0, 1.0));

    double a = bg::area(poly1);

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

    return 0;
}

1

Default parameters, clockwise, closed polygon.

2

Default-construct a polygon.

3

Construct a polygon containing an exterior and interior ring, using C++11 unified initialization syntax.

4

Append point to the exterior ring.

5

Resize a container of interior rings.

6

Append point to the interior ring.

Output:

16

PrevUpHomeNext