Skip to main content

license test status link to PyPI

polygenerator

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

Installation

$ pip install polygenerator

API

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.

Example

from polygenerator import (
    random_polygon,
    random_star_shaped_polygon,
    random_convex_polygon,
)


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


def plot_polygon(polygon, out_file_name):
    plt.figure()
    plt.gca().set_aspect("equal")

    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
    polygon.append(polygon[0])

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

    plt.savefig(out_file_name, dpi=300)
    plt.close()


# this is just so that you can reproduce the same results
random.seed(5)

polygon = random_polygon(num_points=20)

print(polygon)
# [(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

Notes

  • 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.

Release files for polygenerator 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for polygenerator 0.2.0
File Size Uploaded
polygenerator-0.2.0.tar.gz 360.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for polygenerator 0.2.0
File Interpreter ABI Platform
polygenerator-0.2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 366.4 kB

Release files / polygenerator-0.2.0.tar.gz

Download URL polygenerator-0.2.0.tar.gz
Size 360.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e48f8f47ace46faa7b3473fb0602ebd45820bfd921f685bb1ca35dbdff4ef1d6
BLAKE2b-256 checksum
How to use checksums
c5ef670c9d195be72ba5a13c93a7b9add843b3453ec96df98a75cd79a96dbb70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release files / polygenerator-0.2.0-py2.py3-none-any.whl

Download URL polygenerator-0.2.0-py2.py3-none-any.whl
Size 5.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3a942590003ea392df1b517830c19a4a6f8bd7660261b294c5f9398d23378125
BLAKE2b-256 checksum
How to use checksums
8e4b29fb1821f87bf9cf337eb4cda04d4109db36965725241ffb181c204ef7c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page