This program performs operations on a collection of polygons. It supports the following operations:
- Union: Combine multiple polygons into a single polygon.
- Intersection: Find the common area among multiple polygons.
- Difference: Subtract one set of polygons from another.
To achieve this task, C++ library Computational Geometry Algorithms Library (CGAL) has been used. The library provides robust and exact geometric computations where precision matters, especially involving complex polygon operations. It supports different polygon types, simple polygons, polygons with holes and more compelx polygon structures. It uses advanced algorithms and data structures to achieve good time and space complexity. A brief explanation of included header files and type definition from the code:
#include <CGAL/Polygon_2.h>: Polygon_2 is a class template which represents a simple polygon in the plane. It is a part of 2D polygon package in CGAL which provides algorithms and data structures to work with planar polygons.
auto typedef Polygon = CGAL::Polygon_2: The ‘Polygon_2’ class template is parameterized by the kernel type (here ‘Kernel’) . ‘Kernel’ is an instance of the ‘Exact_predicates_exact_constructions_kernel’ which is used as a kernel for the ‘Polyon_2’ class. This means that the Polygon will be represented by precise arithmetic operations (from ‘Kernel’) using both predicates (e.g., orientation test showing if a point is located at right or left of a 2D line) and constructions (e.g., intersection point of two lines or calculating common area among multiple polygons)
Using the Exact_predicates_exact_constructions_kernel ensures that geometric operations, such as, union or intersection are performed with high precision.
To compile and run the code using CGAL, make sure that CGAL is installed:
sudo apt-get update
sudo apt-get install libcgal-dev
For visualizations, matplotlib is required:
sudo apt-get install python-matplotlib python-numpy python2.7-dev
Then run:
g++ -std=c++11 -o polygon_operations polygon.cpp -lCGAL -lgmp -lmpfr -I/usr/include/python2.7 –lpython2.7
The input is provided from a text file. Each line in a text file represents vertices (x,y) of a polygon. Each vertex’s x and y coordinates are separated by a space. For example:
1.0 1.0 2.0 1.0 2.0 2.0 1.0 2.0 3.0 3.0 4.0 3.0 4.0 4.0 3.0 4.0
In this example, two polygons are represented. The first polygon has vertices (1.0, 1.0), (2.0, 1.0), (2.0, 2.0), and (1.0, 2.0). The second polygon has vertices (3.0, 3.0), (4.0, 3.0), (4.0, 4.0), and (3.0, 4.0).
The program then prompts the user to choose an operation (Union, Intersection, or Difference) by entering a corresponding number (1, 2, or 3). Each operation is summarized in the next steps.
Union Operation: The union operation combines multiple polygons into a single polygon set. In CGAL, this can be achieved using the join method of Polygon_set_2. For each polygon in the input set, the join operation adds it to the result set. The final result is a set of polygons representing the union of all input polygon.
- The
polygonUnion
function takes a vector ofPolygon_2
objects as input and returns aPolygon_with_holes_2
object as the result of the union operation. - The function first checks if the input vector is empty. If it is, an
empty
Polygon_with_holes_2
object is returned. - The code creates a new
Polygon_with_holes_2
object calledresult
and initializes it with the vertices of the first polygon in the input vector. - It then iterates over the remaining polygons in the input vector, converts
each polygon to a
Polygon_with_holes_2
object calledcurrent_polygon
, and performs the union operation using theCGAL::join
function. - The result of the union operation is updated in the
result
object. - Finally, the
result
object is returned as the output of thepolygonUnion
function.
Intersection and Difference Operations: The intersection operation finds the common area shared by multiple polygons. In CGAL, this can be achieved using the intersection method of Polygon_set_2.
The difference operation finds the difference between two polygons which can be computed using the difference method of Polygon_set_2.
- The
performOperation
function takes a vector ofPolygon_2
objects and a stringoperationName
specifying which operation (intersection or difference) to be performed as an input. It returns a vector ofPolygon_with_holes_2
object with intersection operation result at each instance. - The function uses nested loops to iterate over all pairs of input polygons (excluding self-intersections).
- For each pair, it calculates the result using either the
CGAL::intersection
orCGAL::difference
method based on the specified operation. - The result is stored in a
std::list<Polygon_with_holes_2>
calledresult
. - The results from each pair are inserted into the final results vector
results
. - The function returns the vector of
Polygon_with_holes_2
results
.
Visualization: To provide information about the results obtained from the intersection or difference operation, processResults
function is used.
- The function takes a vector of
Polygon_with_holes_2
object and a string specifying the type of operation to be performed. - The function generates a filepath to save the resulting image for each polygon pair.
- The function also prints the resulting polygon points.
- If no error, it calls a separate function
printPolygon
to output the resulting polygon and saves an image representation in a local folder. - Note: In case of union operation,
printPolygon
is called directly because union operation outputs a single image showing union of all input polygons.
- At least 2 polygons are required to perform union, intersection, or difference operation.
- Check if each polygon must have at least three vertices.
- Check if the three vertices of each polygon are not collinear. For example, given the polygon points ((0,0), (2,2), (0,0)), the program performs error handling for such situations.
- Check if each x,y point of a polygon is valid.
- Check if polygon is not self-intersecting.