• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
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 // Polygon Overlay Example
12 
13 #include <fstream>
14 #include <iostream>
15 #include <string>
16 #include <vector>
17 
18 #include <boost/foreach.hpp>
19 
20 
21 #include <boost/geometry/geometry.hpp>
22 #include <boost/geometry/geometries/point_xy.hpp>
23 #include <boost/geometry/geometries/polygon.hpp>
24 #include <boost/geometry/geometries/adapted/c_array.hpp>
25 
26 #if defined(HAVE_SVG)
27 #  include <boost/geometry/io/svg/svg_mapper.hpp>
28 #endif
29 
BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)30 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
31 
32 
33 int main(void)
34 {
35     namespace bg = boost::geometry;
36 
37     typedef bg::model::d2::point_xy<double> point_2d;
38     typedef bg::model::polygon<point_2d> polygon_2d;
39 
40 
41 #if defined(HAVE_SVG)
42     std::ofstream stream("05_a_intersection_polygon_example.svg");
43     bg::svg_mapper<point_2d> svg(stream, 500, 500);
44 #endif
45 
46     // Define a polygons and fill the outer rings.
47     polygon_2d a;
48     {
49         const double c[][2] = {
50             {160, 330}, {60, 260}, {20, 150}, {60, 40}, {190, 20}, {270, 130}, {260, 250}, {160, 330}
51         };
52         bg::assign_points(a, c);
53     }
54     bg::correct(a);
55     std::cout << "A: " << bg::dsv(a) << std::endl;
56 
57     polygon_2d b;
58     {
59         const double c[][2] = {
60             {300, 330}, {190, 270}, {150, 170}, {150, 110}, {250, 30}, {380, 50}, {380, 250}, {300, 330}
61         };
62         bg::assign_points(b, c);
63     }
64     bg::correct(b);
65     std::cout << "B: " << bg::dsv(b) << std::endl;
66 #if defined(HAVE_SVG)
67     svg.add(a);
68     svg.add(b);
69 
70     svg.map(a, "opacity:0.6;fill:rgb(0,255,0);");
71     svg.map(b, "opacity:0.6;fill:rgb(0,0,255);");
72 #endif
73 
74 
75     // Calculate interesection(s)
76     std::vector<polygon_2d> intersection;
77     bg::intersection(a, b, intersection);
78 
79     std::cout << "Intersection of polygons A and B" << std::endl;
80     BOOST_FOREACH(polygon_2d const& polygon, intersection)
81     {
82         std::cout << bg::dsv(polygon) << std::endl;
83 #if defined(HAVE_SVG)
84         svg.map(polygon, "opacity:0.5;fill:none;stroke:rgb(255,0,0);stroke-width:6");
85 #endif
86     }
87 
88     return 0;
89 }
90