This is a python script that generates images of polygons inscribed in circles, by utilizing the svg
format.
- Python 3.5 or higher
- sympy module
The script provides the following functions:
makeCircle()
: Generates a circle with a random fill color.rotate(degrees, image)
: Rotates the image (in degrees) with respect to the center of the circle.scaleDown(scale, image)
: Scales down the image according to the specified scale and shifts it to the center of the circle.reflect(image)
: Reflects the image about the vertical axis of symmetry.makePolygon(prime)
: Generates a polygon withprime
sides inscribed in the circle.composeShape(prime, image)
: Generates a composed shape withprime
sub-images and an inputimage
.wrapCanvas(stuffInside)
: Wraps thestuffInside
inside an svg canvas with background color black.
To use the script, simply call the desired functions within the script or create a separate python file and import the functions.
An example use of the script is to generate an image of a hexagon and a square inscribed in a circle:
from sympy.ntheory import factorint
# define primes
hexagon = 6
square = 4
# make the shapes
hexShape = makePolygon(hexagon)
sqShape = makePolygon(square)
# compose the shapes
composed = composeShape(hexagon, sqShape)
# wrap the image
finalImage = wrapCanvas(composed)
# write the image to file
with open('hexagon_and_square_in_circle.svg', 'w') as f:
f.write(finalImage)
The script is an implementation of inscribing polygons in circles and is inspired by the following stackoverflow post. The script is scalable to generate polygons with a high number of sides, but note that generating an image of a polygon with a very large number of sides may result in long processing times.