05_a_overlay_polygon_example.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Polygon Overlay Example
  10. #include <fstream>
  11. #include <iostream>
  12. #include <string>
  13. #include <vector>
  14. #include <boost/foreach.hpp>
  15. #include <boost/geometry/geometry.hpp>
  16. #include <boost/geometry/geometries/point_xy.hpp>
  17. #include <boost/geometry/geometries/polygon.hpp>
  18. #include <boost/geometry/geometries/adapted/c_array.hpp>
  19. #if defined(HAVE_SVG)
  20. # include <boost/geometry/io/svg/svg_mapper.hpp>
  21. #endif
  22. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  23. int main(void)
  24. {
  25. namespace bg = boost::geometry;
  26. typedef bg::model::d2::point_xy<double> point_2d;
  27. typedef bg::model::polygon<point_2d> polygon_2d;
  28. #if defined(HAVE_SVG)
  29. std::ofstream stream("05_a_intersection_polygon_example.svg");
  30. bg::svg_mapper<point_2d> svg(stream, 500, 500);
  31. #endif
  32. // Define a polygons and fill the outer rings.
  33. polygon_2d a;
  34. {
  35. const double c[][2] = {
  36. {160, 330}, {60, 260}, {20, 150}, {60, 40}, {190, 20}, {270, 130}, {260, 250}, {160, 330}
  37. };
  38. bg::assign_points(a, c);
  39. }
  40. bg::correct(a);
  41. std::cout << "A: " << bg::dsv(a) << std::endl;
  42. polygon_2d b;
  43. {
  44. const double c[][2] = {
  45. {300, 330}, {190, 270}, {150, 170}, {150, 110}, {250, 30}, {380, 50}, {380, 250}, {300, 330}
  46. };
  47. bg::assign_points(b, c);
  48. }
  49. bg::correct(b);
  50. std::cout << "B: " << bg::dsv(b) << std::endl;
  51. #if defined(HAVE_SVG)
  52. svg.add(a);
  53. svg.add(b);
  54. svg.map(a, "opacity:0.6;fill:rgb(0,255,0);");
  55. svg.map(b, "opacity:0.6;fill:rgb(0,0,255);");
  56. #endif
  57. // Calculate interesection(s)
  58. std::vector<polygon_2d> intersection;
  59. bg::intersection(a, b, intersection);
  60. std::cout << "Intersection of polygons A and B" << std::endl;
  61. BOOST_FOREACH(polygon_2d const& polygon, intersection)
  62. {
  63. std::cout << bg::dsv(polygon) << std::endl;
  64. #if defined(HAVE_SVG)
  65. svg.map(polygon, "opacity:0.5;fill:none;stroke:rgb(255,0,0);stroke-width:6");
  66. #endif
  67. }
  68. return 0;
  69. }