CGAL::random_polygon_2

Definition

The function random_polygon_2constructs a random simple polygon from points that are drawn from a specific domain. Though each simple polygon defined on this set of points has a non-zero probability of being constructed, some polygons may have higher probabilities than others. The overall distribution of the generated polygons is not known since it depends on the generated points.

#include <CGAL/random_polygon_2.h>

template < class OutputIterator, class PointGenerator, class Traits >
OutputIterator random_polygon_2 ( int n, OutputIterator result, PointGenerator pg, Traits t = Default_traits)
computes a random simple polygon by writing its vertices (oriented counterclockwise) to result. The polygon generated will have a number of vertices equal to the number of unique points in the first n points generated by pg.

Requirements

  1. Traits is a model of the concept RandomPolygonTraits_2
  2. PointGenerator::value_type is equivalent to Traits::Point_2 and OutputIterator::value_type.

The default traits class Default_traits is the kernel in which Traits::Point_2 is defined.

See Also

CGAL::Random_points_in_disc_2<Point_2, Creator>
CGAL::Random_points_in_square_2<Point_2, Creator>

Implementation

The implementation is based on the method of eliminating self-intersections in a polygon by using so-called ``2-opt'' moves. Such a move eliminates an intersection between two edges by reversing the order of the vertices between the edges. No more than O(n3) such moves are required to simplify a polygon defined on n points [vLS82]. Intersecting edges are detected using a simple sweep through the vertices and then one intersection is chosen at random to eliminate after each sweep. The worse-case running time is therefore O(n4 logn).

Example

The following program displays a random simple polygon with up to 100 vertices, where the vertex coordinates are drawn uniformly from the unit square centered at the origin.

File: examples/Generator/random_polygon.cpp
#include <CGAL/Cartesian.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/random_polygon_2.h>
#include <CGAL/Random.h>
#include <CGAL/algorithm.h>

#ifdef CGAL_USE_GMP
#include <CGAL/Gmpz.h>
typedef CGAL::Gmpz RT;
#else
// NOTE: the choice of double here for a number type may cause problems
//       for degenerate point sets
#include <CGAL/double.h>
typedef double RT;
#endif


#include <fstream>
#include <list>

typedef CGAL::Cartesian<RT>                               K;
typedef K::Point_2                                        Point_2;
typedef std::list<Point_2>                                Container;
typedef CGAL::Polygon_2<K, Container>                     Polygon_2;
typedef CGAL::Creator_uniform_2<int, Point_2>             Creator;
typedef CGAL::Random_points_in_square_2<Point_2, Creator> Point_generator;

const double RADIUS = 100;
const int MAX_POLY_SIZE = 100;

int main( )
{
   Polygon_2            polygon;
   std::list<Point_2>   point_set;
   CGAL::Random         rand;

   int size = rand.get_int(4, MAX_POLY_SIZE);

   // copy size points from the generator, eliminating duplicates, so the
   // polygon will have <= size vertices
   CGAL::copy_n_unique(Point_generator(RADIUS), size,
                       std::back_inserter(point_set));

   std::ostream_iterator< Point_2 >  out( std::cout, " " );
   std::cout << "From the following " << point_set.size() << " points "
             << std::endl;
   std::copy(point_set.begin(), point_set.end(), out);
   std::cout << std::endl;

   CGAL::random_polygon_2(point_set.size(), std::back_inserter(polygon),
                          point_set.begin());
   std::cout << "The following simple polygon was made: " << std::endl;
   std::cout << polygon << std::endl;
   return 0;
}