Skip to main content

Generates random polygons.

Project description

license test status link to PyPI

polygenerator

Generates random 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)

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.633385213909564, 0.7365967958574935), (0.5480555383950588, 0.6616499553124763), (0.5526824773965012, 0.5399082630833278), (0.47651656975044715, 0.1562847171200224), (0.211780263278101, 0.11985024210702566), (0.011563117103044742, 0.12131343894869698), (0.02776777678478965, 0.41297620242381444), (0.2831882595429206, 0.9411514041596818), (0.0, 0.8884881505484779), (0.20701109399057388, 0.9700145767610514), (0.2932049212796463, 0.9940396395241987), (0.7024623622191127, 1.0), (0.809077387585075, 0.9717422011442743), (1.0, 0.889663530991249), (0.9601646445268924, 0.6278354104701399), (0.9168705153810578, 0.0), (0.8110693657038247, 0.02995395054812518), (0.6278284961406966, 0.01581195981039065), (0.7790276993942304, 0.05437135270534656), (0.752691110661913, 0.948158571633034)]

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.

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.1.0.tar.gz (239.0 kB view details)

Uploaded Source

Built Distribution

polygenerator-0.1.0-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file polygenerator-0.1.0.tar.gz.

File metadata

  • Download URL: polygenerator-0.1.0.tar.gz
  • Upload date:
  • Size: 239.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.26.0

File hashes

Hashes for polygenerator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 86f9217a548f46059afbc7689288b57c9424e861898263f1d1f0a4c99232b5d1
MD5 2970b3040678946b587e21e4807165ac
BLAKE2b-256 9133e7ca8bf84cc24d9632cf6ec66971f5b4748f4eb74201b16e4a1f1b0ffe4a

See more details on using hashes here.

File details

Details for the file polygenerator-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for polygenerator-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2c6ec7376bd944fc237d42fe0e448d43625e6f2d54994f1d7bff141fec73ff56
MD5 3bdfaede6e9cb6acd1c00f3168171931
BLAKE2b-256 9029c76fa96dfc00ffefa778f6b541c83ddf1db6adce9935876a159c8ce960bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page