The polygon example shows some examples of what can be done with polygons in the Generic Geometry Library: the outer ring and the inner rings how to calculate the area of a polygon how to get the centroid, and how to get an often more interesting label point how to correct the polygon such that it is clockwise and closed within: the well-known point in polygon algorithm how to use polygons which use another container, or which use different containers for points and for inner rings how polygons can be intersected, or clipped, using a clipping box
The illustrations below show the usage of the within algorithm and the intersection algorithm.
The within algorithm results in true if a point lies completly within a polygon. If it lies exactly on a border it is not considered as within and if it is inside a hole it is also not within the polygon. This is illustrated below, where only the point in the middle is within the polygon.
The clipping algorithm, called intersection, is illustrated below:
The yellow polygon, containing a hole, is clipped with the blue rectangle, resulting in a multi_polygon of three polygons, drawn in red. The hole is vanished.
include polygon_example.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <boost/geometry/geometry.hpp>
std::string boolstr(bool v)
{
return v ? "true" : "false";
}
int main(void)
{
using namespace boost::geometry;
polygon_2d poly;
{
const double coor[][2] = {
{2.0, 1.3}, {2.4, 1.7}, {2.8, 1.8}, {3.4, 1.2}, {3.7, 1.6},
{3.4, 2.0}, {4.1, 3.0}, {5.3, 2.6}, {5.4, 1.2}, {4.9, 0.8}, {2.9, 0.7},
{2.0, 1.3}
};
}
std::cout << dsv(poly) << std::endl;
box_2d b;
std::cout << dsv(b) << std::endl;
std::cout <<
"area: " <<
area(poly) << std::endl;
point_2d cent;
std::cout << "centroid: " << dsv(cent) << std::endl;
std::cout << "number of points in outer ring: " << poly.outer().size() << std::endl;
{
poly.inners().resize(1);
const double coor[][2] = { {4.0, 2.0}, {4.2, 1.4}, {4.8, 1.9}, {4.4, 2.2}, {4.0, 2.0} };
}
std::cout << "with inner ring:" << dsv(poly) << std::endl;
std::cout <<
"new area of polygon: " <<
area(poly) << std::endl;
std::cout << "new centroid: " << dsv(cent) << std::endl;
std::cout << "point in polygon:"
<<
" p1: " << boolstr(
within(make<point_2d>(3.0, 2.0), poly))
<<
" p2: " << boolstr(
within(make<point_2d>(3.7, 2.0), poly))
<<
" p3: " << boolstr(
within(make<point_2d>(4.4, 2.0), poly))
<< std::endl;
box_2d cb(make<point_2d>(1.5, 1.5), make<point_2d>(4.5, 2.5));
typedef std::vector<polygon_2d> polygon_list;
polygon_list v;
std::cout << "Clipped output polygons" << std::endl;
for (polygon_list::const_iterator it = v.begin(); it != v.end(); ++it)
{
std::cout << dsv(*it) << std::endl;
}
polygon_set ps;
polygon_2d hull;
std::cout << "Convex hull:" << dsv(hull) << std::endl;
deque_polygon poly2;
append(ring, make<point_2d>(2.8, 1.9));
append(ring, make<point_2d>(2.9, 2.4));
append(ring, make<point_2d>(3.3, 2.2));
append(ring, make<point_2d>(3.2, 1.8));
append(ring, make<point_2d>(2.8, 1.9));
std::cout << dsv(poly2) << std::endl;
return 0;
}