A test program for implementing polygons using boost::geometry::model::multi_polygon
In geometry applications, we often need to deal with multiple polygons. The Boost.Geometry library provides a template called multi_polygon to handle multiple polygons. This article describes how to use multi_polygon.
First, we need to define a spatial type. Here, we have used a two-dimensional plane (2D Cartesian). Therefore, we can define a spatial type, called point_2d:
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
namespace bg = boost::geometry;
typedef bg::model::d2::point_xy<double> point_2d;
Then, we define a simple polygon consisting of three points:
using namespace boost::geometry::model;
polygon<point_2d> poly;
ring<point_2d> ring;
ring.push_back(point_2d(0.0, 0.0));
ring.push_back(point_2d(1.0, 0.0));
ring.push_back(point_2d(1.0, 1.0));
ring.push_back(point_2d(0.0, 1.0));
ring.push_back(point_2d(0.0, 0.0));
bg::append(poly.outer(), ring);
Next, we define another polygon, consisting of four points:
polygon<point_2d> poly2;
ring