Polygon initialization in boost :: geometry

I am new to the general geometry library, which is suggested for inclusion with boost:

http://geometrylibrary.geodan.nl/ 

I have two vectors vector<int> Xb, Yb from which I am trying to create a polygon. I am trying to get something according to the following code snippet:

  polygon_2d P; vector<double>::const_iterator xi; vector<double>::const_iterator yi; for (xi=Xb.begin(), yi=Yb.begin(); xi!=Xb.end(); ++xi, ++yi) P.push_back (make<point_2d>(*xi, *yi)); 

The above code does not work, complaining that P does not have a push_back member function. How to initialize a polygon from points with coordinates vector<int> Xb,vector<int> Yb ?

+4
source share
3 answers
 append(P, make<point_2d>(*xi, *yi)); 
+6
source

Here is an example of expanding your original question that you asked as a comment below Cyril's answer: Are intersections between polygons possible?

Yes, intersections of polygons and polygons are supported by Boost.Geometry (aka GGL)

 #include <iostream> #include <vector> #include <boost/geometry/geometry.hpp> #include <boost/geometry/geometries/cartesian2d.hpp> #include <boost/geometry/geometries/adapted/c_array_cartesian.hpp> using namespace boost::geometry; int main(void) { // Define a polygons and fill the outer rings. polygon_2d a; { const double c[][2] = { {160, 330}, {60, 260}, {20, 150}, {60, 40}, {190, 20}, {270, 130}, {260, 250}, {160, 330} }; assign(a, c); } correct(a); std::cout << "A: " << dsv(a) << std::endl; polygon_2d b; { const double c[][3] = { {300, 330}, {190, 270}, {150, 170}, {150, 110}, {250, 30}, {380, 50}, {380, 250}, {300, 330} }; assign(b, c); } correct(b); std::cout << "B: " << dsv(b) << std::endl; // Calculate interesection typedef std::vector<polygon_2d > polygon_list; polygon_list v; intersection_inserter<polygon_2d>(a, b, std::back_inserter(v)); std::cout << "Intersection of polygons A and B" << std::endl; for (polygon_list::const_iterator it = v.begin(); it != v.end(); ++it) { std::cout << dsv(*it) << std::endl; } return 0; } 

Here is the result (the intersection of the polygon moves south for better visibility):

alt text

I hope this works for you.

+12
source

You can also use a tuple to initialize the polygon.

 #include <boost/geometry/geometries/adapted/boost_tuple.hpp> 

and

 boost::geometry::assign_points( polygon, boost::assign::tuple_list_of (300, 330) (190, 270) (150, 170) (150, 110) (250, 30) (380, 50) (380, 250) (300, 330) ); 
0
source

All Articles