The following example creates a polygon and illustrates the usage of some global functions that operate on sequences of points.
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2_algorithms.h>
#include <iostream>
typedef
K::Point_2 Point;
using
std::cout;
using
std::endl;
void
check_inside(Point pt, Point *pgn_begin, Point *pgn_end, K traits)
cout <<
"The point "
<< pt;
cout <<
" is inside the polygon.\n"
;
break
;
cout <<
" is on the polygon boundary.\n"
;
break
;
cout <<
" is outside the polygon.\n"
;
break
;
int
main()
Point points[] = { Point(0,0), Point(5.1,0), Point(1,1), Point(0.5,6)};
cout <<
"The polygon is "
<<
"simple."
<< endl;
check_inside(Point(0.5, 0.5), points, points+4, K());
check_inside(Point(1.5, 2.5), points, points+4, K());
check_inside(Point(2.5, 0), points, points+4, K());
return
0;
Bounded_side bounded_side_2(ForwardIterator first, ForwardIterator last, const Point &point, const PolygonTraits &traits)
Computes if a point lies inside a polygon.
bool is_simple_2(ForwardIterator first, ForwardIterator last, const PolygonTraits &traits)
Checks if the polygon defined by the iterator range [first,last) is simple, that is,...
Polygons in 3D Space
Sometimes it is useful to run a 2D algorithm on 3D data. Polygons may be contours of a 3D object, where the contours are organized in parallel slices, generated by segmentation of image data from a scanner.
In order to avoid an explicit projection on the
xy
plane, one can use the traits class
Projection_traits_xy_3
which is part of the 2D and 3D Linear Geometric Kernel.
File
Polygon/projected_polygon.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Projection_traits_yz_3.h>
#include <CGAL/Polygon_2_algorithms.h>
#include <iostream>
int
main()
Point_3
points[4] = {
Point_3
(0,1,1),
Point_3
(0,2,1),
Point_3
(0,2,2),
Point_3
(0,1,2) };
points+4,
if
(!b){
std::cerr <<
"Error polygon is not simple"
<< std::endl;
return
1;
return
0;
Iterating over Vertices and Edges
The polygon class provides member functions such as
Polygon_2::vertices_begin()
and
Polygon_2::vertices_end()
to iterate over the vertices. It additionally provides a member function
Polygon_2::vertices()
that returns a range, mainly to be used with modern
for
loops. The same holds for edges and for the holes of the class
Polygon_with_holes_2
.
File
Polygon/ranges.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
int
main()
Polygon_2 p;
for
(
const
Point_2
& p : p.vertices()){
std::cout << p << std::endl;
for
(
auto
it = range.begin(); it!= range.end(); ++it){
std::cout << *it << std::endl;
std::cout << e << std::endl;
return
EXIT_SUCCESS;
Container Vertices
a range type to iterate over the vertices
Definition:
Polygon_2.h:124
Draw a Polygon
A polygon can be visualized by calling the
CGAL::draw<P>()
function as shown in the following example. This function opens a new window showing the given polygon. A call to this function is blocking, that is the program continues as soon as the user closes the window. Versions for polygons with holes and multipolygons with holes also exist, cf.
CGAL::draw<PH>()
and
CGAL::draw<MPH>()
.
File
Polygon/draw_polygon.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/draw_polygon_2.h>
int
main()
Polygon_2 p;
p.push_back(Point(0,0));
p.push_back(Point(4,0));
p.push_back(Point(4,4));
p.push_back(Point(2,2));
p.push_back(Point(0,4));
return
EXIT_SUCCESS;
void draw(const MPH &aph)
opens a new window and draws aph, an instance of the CGAL::Multipolygon_with_holes_2 class.
This function requires
CGAL_Qt6
, and is only available if the macro
CGAL_USE_BASIC_VIEWER
is defined. Linking with the cmake target
CGAL::CGAL_Basic_viewer
will link with
CGAL_Qt6
and add the definition
CGAL_USE_BASIC_VIEWER
.