/Src/Dependencies/Boost/libs/geometry/doc/src/examples/algorithms/sym_difference.cpp

http://hadesmem.googlecode.com/ · C++ · 80 lines · 30 code · 18 blank · 32 comment · 0 complexity · 87946364f7de4247c38707bb02f7120d MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // QuickBook Example
  3. // Copyright (c) 2011 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //[sym_difference
  8. //` Shows how to calculate the symmetric difference (XOR) of two polygons
  9. #include <iostream>
  10. #include <boost/geometry.hpp>
  11. #include <boost/geometry/geometries/polygon.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. #include <boost/geometry/multi/geometries/multi_polygon.hpp>
  14. #include <boost/geometry/domains/gis/io/wkt/wkt.hpp>
  15. #include <boost/foreach.hpp>
  16. /*<-*/ #include "create_svg_overlay.hpp" /*->*/
  17. int main()
  18. {
  19. typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;
  20. polygon green, blue;
  21. boost::geometry::read_wkt(
  22. "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
  23. "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);
  24. boost::geometry::read_wkt(
  25. "POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);
  26. boost::geometry::model::multi_polygon<polygon> multi;
  27. boost::geometry::sym_difference(green, blue, multi);
  28. std::cout
  29. << "green XOR blue:" << std::endl
  30. << "total: " << boost::geometry::area(multi) << std::endl;
  31. int i = 0;
  32. BOOST_FOREACH(polygon const& p, multi)
  33. {
  34. std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
  35. }
  36. /*<-*/ create_svg("sym_difference.svg", green, blue, multi); /*->*/
  37. return 0;
  38. }
  39. //]
  40. //[sym_difference_output
  41. /*`
  42. Output:
  43. [pre
  44. green XOR blue:
  45. total: 3.1459
  46. 0: 0.02375
  47. 1: 0.542951
  48. 2: 0.0149697
  49. 3: 0.226855
  50. 4: 0.839424
  51. 5: 0.525154
  52. 6: 0.015
  53. 7: 0.181136
  54. 8: 0.128798
  55. 9: 0.340083
  56. 10: 0.307778
  57. [$img/algorithms/sym_difference.png]
  58. ]
  59. */
  60. //]