Code Monkey home page Code Monkey logo

polygenerator's Introduction

license test status link to PyPI

polygenerator

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

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,
)

from plot import plot_polygon

# this is just so that you can reproduce the same results
import random
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.

polygenerator's People

Contributors

bast avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

fennhelloworld

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.