CGAL 6.0 - 2D Regularized Boolean Set-Operations
Loading...
Searching...
No Matches
Boolean_set_operations_2/set_union.cpp
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Gps_circle_segment_traits_2.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <list>
#include <cstdlib>
#include <cmath>
#include <cassert>
typedef Kernel::Point_2 Point_2;
typedef Kernel::Circle_2 Circle_2;
typedef Traits_2::Polygon_2 Polygon_2;
typedef Traits_2::Polygon_with_holes_2 Polygon_with_holes_2;
typedef Traits_2::Curve_2 Curve_2;
typedef Traits_2::X_monotone_curve_2 X_monotone_curve_2;
// Construct a polygon from a circle.
Polygon_2 construct_polygon (const Circle_2& circle)
{
// Subdivide the circle into two x-monotone arcs and construct the polygon
Traits_2 traits;
Curve_2 curve (circle);
Polygon_2 pgn;
traits.make_x_monotone_2_object() (curve,
CGAL::dispatch_or_drop_output<X_monotone_curve_2>(std::back_inserter(pgn)));
assert(pgn.size() == 2);
return pgn;
}
// The main program:
int main (int argc, char* argv[])
{
// Read the number of circles from the command line.
unsigned int n_circles = 8;
if (argc > 1) n_circles = std::atoi(argv[1]);
// Create the circles, equally spaced of the circle x^2 + y^2 = 1.
const double pi = std::atan(1.0) * 4;
const double n_circles_reciep = 1.0 / n_circles;
const double radius = 1;
const double frac = 2 * pi * n_circles_reciep;
std::list<Polygon_2> circles;
unsigned int k;
for (k = 0; k < n_circles; k++) {
const double angle = frac * k;
const double x = radius * std::sin(angle);
const double y = radius * std::cos(angle);
Point_2 center = Point_2(x, y);
Circle_2 circle(center, radius);
circles.push_back (construct_polygon (circle));
}
// Compute the union aggregately.
std::list<Polygon_with_holes_2> res;
CGAL::join (circles.begin(), circles.end(), std::back_inserter (res));
// Print the result.
std::copy (res.begin(), res.end(),
std::ostream_iterator<Polygon_with_holes_2>(std::cout, "\n"));
std::cout << std::endl;
return 0;
}
The traits class Gps_circle_segment_traits_2 models the GeneralPolygonSetTraits_2 concept.
Definition: Gps_circle_segment_traits_2.h:18
bool join(const Polygon_2< Kernel, Container > &pgn1, const Polygon_2< Kernel, Container > &pgn2, General_polygon_with_holes_2< Polygon_2< Kernel, Container > > &res)
computes the union of two polygons.