The class Polygon_2 represents a simple polygon in the two-dimensional Euclidean plane . A polygon is called simple if there is no pair of nonconsecutive edges sharing a point (see [PS85]).
An object p of the data type Polygon_2 is defined by the sequence of its vertices. A simple polygon p is oriented, i.e., its boundary has clockwise or counterclockwise orientation. The side to the left of the boundary is called the positive side and the side to the right of the boundary is called the negative side. As any Jordan curve, the boundary of a polygon divides the plane into two open regions, a bounded one and an unbounded one.
An object p of Polygon_2 is a dynamic data structure, i.e. vertices can be added and removed. These operations may destroy the simplicity of the polygon, which is a precondition to most predicates of polygons.
The data type Polygon_2 is parameterized with two template parameters: a traits class Traits and a container class Container. The parameter Traits defines the types and predicates that are used in the polygon class and the polygon algorithms. For example Traits::Point_2 denotes the type of the vertices of the polygon. A default polygon traits class Polygon_traits_2<R> is provided (see Section ), where R is a representation class. The parameter Container specifies the type of container that is used to store the sequence of vertices of the polygon, e.g. a list, a vector, a tree, etc. The type Container should fulfill the requirements of a sequence container given in [MS96]. The value type of the container should be the same as the point type of the traits class.
| |
The traits type.
| |
| |
The container type.
|
The following types denote iterators that allow to traverse the vertices and edges of a polygon. Since it is questionable whether a polygon should be viewed as a circular or as a linear data structure both circulators and iterators are defined. The circulators and iterators with `const' in their name are non-mutable, the others are mutable. The iterator category is in all cases bidirectional, except for Vertex_iterator and Vertex_const_iterator, which have the same iterator category as Container::iterator. N.B. In fact all of them should have the same iterator category as Container::iterator. However, due to compiler problems this is currently not possible. This will be corrected when iterator traits become available. The consequence of using iterators / circulators with an incorrect iterator category is that when an STL algorithm is applied to such a range, the wrong (i.e. inefficient) version of an STL algorithm may be selected.
For vertices we define
| |
| |
| |
|
Their value type is Point_2.
For edges we define
| |
|
Their value type is Segment_2.
| |
| |
Introduces a polygon p with vertices from the sequence defined by
the range [first,last). Precondition: The value type of points in the range [first,last) is Point_2.
| |
| |
| |
Introduces a polygon p with vertices from the sequence defined by
the range [start,start]. Precondition: The value type of points in the range [first,last) is Point_2.
|
The following operations allow to modify a polygon.
|
| |||
Inserts the vertex q before i. The return value points to the inserted vertex. | ||||
| ||||
|
| |||
Inserts the vertices in the range [first, last) before
i. Precondition: The value type of points in the range [first,last) is Point_2. | ||||
|
| |||
Has the same semantics as p.insert(p.vertices_end(), q). | ||||
|
| |||
Erases the vertex pointed to by i. | ||||
|
| |||
Erases the vertices in the range [first, last). | ||||
|
| |||
Reverses the orientation of the polygon. The vertex pointed to by p.vertices_begin() remains the same. |
|
| |
Returns a mutable iterator that allows to traverse the vertices of the polygon p. | ||
|
| Returns the corresponding past-the-end iterator. |
| ||
| ||
Returns a non-mutable iterator that allows to traverse the vertices of the polygon p. | ||
| ||
| Returns the corresponding past-the-end iterator. | |
|
| |
Returns a mutable circulator that allows to traverse the vertices of the polygon p. | ||
| ||
| ||
Returns a non-mutable circulator that allows to traverse the vertices of the polygon p. | ||
| ||
| Returns a non-mutable iterator that allows to traverse the edges of the polygon p. | |
| ||
| Returns the corresponding past-the-end iterator. | |
| ||
| ||
Returns a non-mutable circulator that allows to traverse the edges of the polygon p. |
|
| Returns whether p is a simple polygon. |
|
| Returns whether p is convex. |
|
|
Returns the orientation of p. If the number of vertices
then COLLINEAR is returned. Precondition: p.is_simple(). |
|
| |
Returns POSITIVE_SIDE, or NEGATIVE_SIDE,
or ON_ORIENTED_BOUNDARY,
depending on where point q is. Precondition: p.is_simple(). | ||
|
| |
Returns the symbolic constant ON_BOUNDED_SIDE,
ON_BOUNDARY
or ON_UNBOUNDED_SIDE, depending on where point
q is. Precondition: p.is_simple(). | ||
|
| Returns the smallest bounding box containing p. |
|
| Returns the signed area of the polygon p. This means that the area is positive for counter clockwise polygons and negative for clockwise polygons. |
|
| Returns the leftmost vertex of the polygon p with the smallest y-coordinate. |
|
| Returns the rightmost vertex of the polygon p with the largest y-coordinate. |
|
| Returns topmost vertex of the polygon p with the largest x-coordinate. |
|
| Returns the bottommost vertex of the polygon p with the smallest x-coordinate. |
For convenience we provide the following boolean functions:
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
|
| Returns a (const) reference to the -th vertex. |
|
| Returns a (const) reference to the -th vertex. |
|
| Returns a const reference to the -th edge. |
|
| Returns the number of vertices of the polygon p. |
|
| Returns . |
|
| Returns a const reference to the sequence of vertices of the polygon p. |
template <class Traits, class Container1, class Container2>
|
| |
Test for equality: two polygons are equal iff there exists a cyclic permutation of the vertices of p2 such that they are equal to the vertices of p1. Note that the template argument Container of p1 and p2 may be different. |
template <class Traits, class Container1, class Container2>
|
| |
Test for inequality. |
template <class Transformation, class Traits, class Container>
| ||
| ||
Returns the image of the polygon p under the transformation t. |
The I/O operators are defined for iostream, and for the window stream provided by CGAL. The format for the iostream is an internal format.
|
| |
Inserts the polygon p into the stream os. Precondition: The insert operator is defined for class Point_2. | ||
|
| |
Reads a polygon from stream is and assigns it to p. |
#include <CGAL/IO/Window_stream.h>
|
| |
Inserts the triangulation p into the window stream W. The insert operator must be defined for Point_2. |
The following code fragment creates a polygon and checks if it is convex.
#include <CGAL/basic.h> #include <CGAL/Cartesian.h> #include <CGAL/Polygon_2.h> #include <list> typedef CGAL::Cartesian<double> R; typedef CGAL::Polygon_traits_2<R> Traits; typedef Traits::Point_2 Point; typedef std::list<Point> Container; typedef CGAL::Polygon_2<Traits,Container> Polygon; #include <iostream> int main() { Polygon p; p.push_back(Point(0,0)); p.push_back(Point(1,0)); p.push_back(Point(1,1)); p.push_back(Point(0,1)); cout << "The polygon is " << (p.is_convex() ? "" : "not ") << "convex." << endl; return 0; }