Geometry
Random Generation
Computational Algorithms
2D Polygons
Computer Graphics

Algorithm to generate random 2D polygon

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Generating a random 2D polygon is easy if you only want a random list of points, but much harder if you need a valid simple polygon with no self-intersections. The right algorithm depends on whether you want convex polygons only, star-shaped polygons, or arbitrary simple polygons with stronger randomness requirements.

The Main Difficulty Is Avoiding Self-Intersection

If you pick random points and connect them in arbitrary order, you usually get a self-crossing shape rather than a valid simple polygon. So a useful polygon generator must impose structure on the point order.

Two common practical approaches are:

  • generate random points and take their convex hull for a convex polygon
  • generate points around a center with sorted angles for a star-shaped simple polygon

The first is safer. The second gives more varied shapes.

A Simple Convex Polygon Method

If a convex polygon is acceptable, the easiest method is:

  1. generate random points in the plane
  2. compute their convex hull
  3. use the hull vertices as the polygon

In Python, scipy.spatial.ConvexHull or other geometry libraries can do this, but the idea is straightforward even without a library.

python
1import random
2from scipy.spatial import ConvexHull
3import numpy as np
4
5points = np.array([(random.random(), random.random()) for _ in range(30)])
6hull = ConvexHull(points)
7polygon = points[hull.vertices]
8
9print(polygon)

This guarantees a non-self-intersecting convex polygon, but it does not give concave shapes.

A Star-Shaped Random Polygon Method

For more organic shapes, a practical method is to generate vertices around a center using random angles and random radii, then sort by angle before connecting the points.

python
1import math
2import random
3
4
5def random_polygon(n, center=(0.0, 0.0), min_radius=0.5, max_radius=1.0):
6    cx, cy = center
7    angles = sorted(random.random() * 2 * math.pi for _ in range(n))
8    points = []
9
10    for angle in angles:
11        radius = random.uniform(min_radius, max_radius)
12        x = cx + radius * math.cos(angle)
13        y = cy + radius * math.sin(angle)
14        points.append((x, y))
15
16    return points
17
18
19print(random_polygon(8))

Because the vertices are ordered by polar angle around a common center, the polygon is typically simple and star-shaped.

This method is popular in games and procedural graphics because it is easy to control visually.

Controlling Shape Quality

A raw random polygon generator often produces ugly or degenerate shapes unless you add constraints. Useful controls include:

  • minimum angle separation between consecutive vertices
  • bounded radius variation
  • smoothing after generation
  • minimum edge length

For example, if two sampled angles are nearly identical, the polygon may get a tiny sliver edge. If one radius is much larger than the rest, the shape may look spiky and unstable.

So practical generators usually combine randomness with shape constraints.

If You Need Arbitrary Simple Polygons

Generating uniformly random simple polygons with no convexity restriction is a harder computational geometry problem. There are research-level methods involving permutations, edge swaps, and rejection-based corrections, but they are more complex than most applications need.

For typical engineering tasks, ask what you actually need:

  • if convex is enough, use convex hull
  • if natural-looking but simple is enough, use angle-sorted radial sampling
  • if you need very specific statistical properties, use a dedicated geometry library or published algorithm

That distinction saves a lot of unnecessary complexity.

Post-Validation Is Still Useful

Even with a sensible generation strategy, validating the polygon can be worthwhile, especially if later code depends on a strictly simple polygon.

A basic validation step checks whether any non-adjacent edges intersect. If they do, reject the polygon and regenerate.

That is often cheaper and simpler than trying to design a perfect generator on the first attempt.

Common Pitfalls

  • Connecting random points in random order almost always produces self-intersections rather than a valid polygon.
  • Assuming convex hull generation is a general random polygon solution ignores that it only produces convex shapes.
  • Letting radii vary too wildly in an angle-sorted generator creates extreme spikes and poor-quality geometry.
  • Skipping validation can leave rare but invalid polygons in downstream graphics or simulation code.
  • Asking for a "random polygon" without defining whether convexity, simplicity, or distribution matters often leads to the wrong algorithm choice.

Summary

  • The hard part of random polygon generation is preserving a valid non-self-intersecting boundary.
  • Convex hull generation is the easiest safe method when convex polygons are acceptable.
  • Sorting random angles around a center is a practical way to generate simple star-shaped polygons.
  • Shape-quality constraints and validation improve real-world results.
  • Choose the algorithm based on the polygon class you actually need, not just the word random.

Course illustration
Course illustration

All Rights Reserved.