Advertisement
Guest User

boost::geometry::difference

a guest
Feb 27th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <boost/geometry.hpp>
  4. #include <boost/geometry/geometries/point_xy.hpp>
  5.  
  6. #include <boost/foreach.hpp>
  7.  
  8. int main()
  9. {
  10.     typedef boost::geometry::model::d2::point_xy<double> POINT;
  11.     typedef boost::geometry::model::linestring<POINT> LINE;
  12.     typedef boost::geometry::model::polygon<POINT> POLYGON;
  13.  
  14.     LINE line;
  15.     POLYGON polygon;
  16.  
  17.     //                     Y
  18.     //                     |
  19.     //               -------------
  20.     //               |     |     |
  21.     //               |     |     |
  22.     //   ------A>>>>>C>>>>>>>>>>>D>>>>>B-----> X
  23.     //       start   |     |     |    end
  24.     //       point   |     |     |   point
  25.     //               ------|------
  26.     //                     |
  27.     //
  28.  
  29.     // Create line from A to B.
  30.     boost::geometry::read_wkt("linestring(-2 0, 2 0)", line);
  31.     boost::geometry::correct(line);
  32.  
  33.     boost::geometry::read_wkt("POLYGON((1 1, -1 1, -1 -1, 1 -1))", polygon);
  34.     boost::geometry::correct(polygon);
  35.  
  36.     std::vector<LINE> output;
  37.     boost::geometry::difference(line, polygon, output);
  38.  
  39.     // Result will be 2 lines.
  40.     // A to C : LINESTRING(-2 0,-1 0)
  41.     // D to B : LINESTRING(1 0,2 0)
  42.     BOOST_FOREACH(LINE const& l, output)
  43.     {
  44.         std::cout << boost::geometry::wkt(l) << std::endl;
  45.     }
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement