I\'m trying to devise a method for generating random 2D convex polygons. It has to have the following properties:
Here is the fastest algorithm I know that generates each convex polygon with equal probability. The output has exactly N vertices, and the running time is O(N log N), so it can generate even large polygons very quickly.
X
and Y
, of N random integers between 0 and C. Make sure there are no duplicates.X
and Y
and store their maximum and minimum elements.X1
and X2
, and Y1
and Y2
.minX
at the start of X1
and X2
, maxX
at the end, etc.).X1[i + 1] - X1[i]
), reversing the order for the second group (X2[i] - X2[i + 1]
). Store these in lists XVec
and YVec
.YVec
and treat each pair XVec[i]
and YVec[i]
as a 2D vector.An animation and Java implementation is available here: Generating Random Convex Polygons.
This algorithm is based on a paper by Pavel Valtr: “Probability that n random points are in convex position.” Discrete & Computational Geometry 13.1 (1995): 637-643.