Skip to main content

Generates random simple polygons.

Project description

license test status link to PyPI


Generates random simple polygons. This can be useful to test computational geometry algorithms or to generate maps.


$ pip install polygenerator


There are 3 functions and each returns a list of (x, y) tuples:

  • random_convex_polygon(num_points)
  • random_polygon(num_points)
  • random_star_shaped_polygon(num_points)

All polygons are generated to be counterclockwise. You can reverse the order outside if you need the points in clockwise order.

The generated polygon is made to fit the bounding box (0.0, 0.0) ... (1.0, 1.0) and you can then scale and translate it to where you need it.


from polygenerator import (

# these two are only for demonstration
import matplotlib.pyplot as plt
import random

def plot_polygon(polygon, out_file_name):

    for i, (x, y) in enumerate(polygon):
        plt.text(x, y, str(i), horizontalalignment="center", verticalalignment="center")

    # just so that it is plotted as closed polygon

    xs, ys = zip(*polygon)
    plt.plot(xs, ys, "r-", linewidth=0.4)

    plt.savefig(out_file_name, dpi=300)

# this is just so that you can reproduce the same results

polygon = random_polygon(num_points=20)

# [(0.752691110661913, 0.948158571633034), (0.7790276993942304, 0.05437135270534656), ..., (0.633385213909564, 0.7365967958574935)]

plot_polygon(polygon, "random_polygon.png")

random polygon

polygon = random_star_shaped_polygon(num_points=20)
plot_polygon(polygon, "random_star_shaped_polygon.png")

random star shaped polygon

polygon = random_convex_polygon(num_points=20)
plot_polygon(polygon, "random_convex_polygon.png")

random convex polygon


  • For the generation of a concave/general polygon, algorithms with better scaling exist but this was good enough for me since for testing I did not need polygons with more than 100 points. Improvements welcome.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polygenerator-0.2.0.tar.gz (360.7 kB view hashes)

Uploaded source

Built Distribution

polygenerator-0.2.0-py2.py3-none-any.whl (5.8 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page