• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry
2 // QuickBook Example
3 
4 // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2015 Adam Wulkiewicz, Lodz, Poland.
6 
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 //[multi_polygon
12 //` Declaration and use of the Boost.Geometry model::multi_polygon, modelling the MultiPolygon Concept
13 
14 #include <iostream>
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/geometries/geometries.hpp>
17 
18 namespace bg = boost::geometry;
19 
main()20 int main()
21 {
22     typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
23     typedef bg::model::polygon<point_t> polygon_t; /*< Default parameters, clockwise, closed polygon. >*/
24     typedef bg::model::multi_polygon<polygon_t> mpolygon_t; /*< Clockwise, closed multi_polygon. >*/
25 
26     mpolygon_t mpoly1; /*< Default-construct a multi_polygon. >*/
27 
28 #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) \
29  && !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
30 
31     mpolygon_t mpoly2{{{{0.0, 0.0}, {0.0, 5.0}, {5.0, 5.0}, {5.0, 0.0}, {0.0, 0.0}},
32                        {{1.0, 1.0}, {4.0, 1.0}, {4.0, 4.0}, {1.0, 4.0}, {1.0, 1.0}}},
33                       {{{5.0, 5.0}, {5.0, 6.0}, {6.0, 6.0}, {6.0, 5.0}, {5.0, 5.0}}}}; /*< Construct a multi_polygon containing two polygons, using C++11 unified initialization syntax. >*/
34 
35 #endif
36 
37     mpoly1.resize(2); /*< Resize a multi_polygon, store two polygons. >*/
38 
39     bg::append(mpoly1[0].outer(), point_t(0.0, 0.0)); /*< Append point to the exterior ring of the first polygon. >*/
40     bg::append(mpoly1[0].outer(), point_t(0.0, 5.0));
41     bg::append(mpoly1[0].outer(), point_t(5.0, 5.0));
42     bg::append(mpoly1[0].outer(), point_t(5.0, 0.0));
43     bg::append(mpoly1[0].outer(), point_t(0.0, 0.0));
44 
45     mpoly1[0].inners().resize(1); /*< Resize a container of interior rings of the first polygon. >*/
46     bg::append(mpoly1[0].inners()[0], point_t(1.0, 1.0)); /*< Append point to the interior ring of the first polygon. >*/
47     bg::append(mpoly1[0].inners()[0], point_t(4.0, 1.0));
48     bg::append(mpoly1[0].inners()[0], point_t(4.0, 4.0));
49     bg::append(mpoly1[0].inners()[0], point_t(1.0, 4.0));
50     bg::append(mpoly1[0].inners()[0], point_t(1.0, 1.0));
51 
52     bg::append(mpoly1[1].outer(), point_t(5.0, 5.0)); /*< Append point to the exterior ring of the second polygon. >*/
53     bg::append(mpoly1[1].outer(), point_t(5.0, 6.0));
54     bg::append(mpoly1[1].outer(), point_t(6.0, 6.0));
55     bg::append(mpoly1[1].outer(), point_t(6.0, 5.0));
56     bg::append(mpoly1[1].outer(), point_t(5.0, 5.0));
57 
58     double a = bg::area(mpoly1);
59 
60     std::cout << a << std::endl;
61 
62     return 0;
63 }
64 
65 //]
66 
67 
68 //[multi_polygon_output
69 /*`
70 Output:
71 [pre
72 17
73 ]
74 */
75 //]
76