Skip to content

Python Spirograph application - for dabbling with parametric equations to draw a circle

Notifications You must be signed in to change notification settings

ddeveloper72/spirographs-py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mini project, using python tool to create Spirographs

This mini-project is a replication of work done by Electronut Labs that uses parametric equations to draw a circle. I've personally not used any trig functions since high school back in the late '80 early 90's; I graduated Matric or St 10 class of '91. So this mini-project is an interesting refresher! I came across this work, in an eBook, called Python Playground: Geeky Projects for the Curious Programmer I've done this for the simple pure enjoyment factor of working with Python

Draw circle

Created a snapshot of the application output

The origin or centre point of the circle is 0, the pen travels 360deg around this origin at a distance of r, which begins at an (x,y) coordinate and eventually finishes back at this same coordinate. The function calculates the new (x,y) coordinate for each degree or radian about the origin.

If we specify a an origin point of (x=0, y=0), then place the imaginary pen nib at (x+r, y) we create a dot on the radius of the un-drawn circle. The function below then calculates the new (x,y) values, then creates a new dot that is 5 radians away from the last dot, then repeats.

The range below is similar to a css rotation animation, that starts at 0deg, finishes at 360deg and rotates in increments of 5 radians, 365 times.

for i in range(0, 365, 5):
        a = math.radians(i)
        turtle.setpos(
            x + r*math.cos(a),
            y + r*math.sin(a)
        )

Image showing Draw Circle output:

Image showing circle

Draw Spirographs

Image showing spirograph

Spirographs can be drawn by considering the equation for a circle with a radius of r, then calculating the (x,y) coordinates for the plot line around the origin.

Consider the following: An X Y axis has an angle of 90deg between them. Its a perfect right angle.

But now somewhere between, the X and Y axis, we place a dot. Then rule a line from the origin of the X Y axis, to our dot. Call the origin 0.

Now if we measure the angle between our line and the X axis, we call this angle Theta, or use the Greek symbol, Θ its units are in degrees or some may call it radians.

The formula for a circle is:

Function of a circle

This formula can be refactored below, to solve for x and y, which is our real interest- because we want to know where to place our pen; the dot, the (x,y) coordinate to begin drawing.

Solve for x

Solve for y

Ok, so we have a dot. But how does one draw a a circle, comprised of multiple dots, all 5 radians apart? So... what if the 1st dot is on the X-axis. That means Y is 0 and the angle, Θ is also 0 degrees and x is some value bigger than 0 so pick 5, then the coordinate is (5,0). This is handy, because 5 is the number of r, the radius as well. The next coordinate depends on two constants. The new value for Θ and the constant for r. With these two knowns, we can solve for the unknown, x and y. See the pattern? When we use python to do the math, it uses a tool called Turtle to act as the pen on paper. In Turn, as the math is done from within the structure of a function, Turtle animates the drawing process- like a stop motion animation. Calculate dot, draw dot, calculate next dot, draw dot. You and I see the magic of a beautiful curve, the result of loads and loads of mathematical computations to determine the values of (x,y).

So now we know how to draw a circle. But this isn't a spirograph right. A spirograph is a circle within a circle, where the inside circle rotates like a wheel, inside the larger circle.

What? Hey? The smaller circle roles around the inside for the bigger circle. So as it roles, its outside edge follows the path of the inside edge of the larger circle. Again we can calculate the (x,y) dots of the smaller circle, as they rotate their way around the the circumference of the larger circle.

So ok, what is it that is important, that we know?

For a start, the smaller circle has to have a centre. Then the radius r from the centre of the smaller circle, can't extend beyond the radius R of the bigger circle. If it did, it's not going to appear like the little circle rolling around the inside of the bigger circle, when we begin to animate all our dots together right.

mmm.. so what we are doing is calculating the centre of the smaller circle, but we don't plot a dot at this point. Instead, this (x,y) coordinate is the centre of the smaller circle, from were we can calculate another (x,y) coordinate where we can actually plot a dot. The ratio between the radii of the two circles can be expressed as k

k can be represented as:

Finding k

This new (x,y) coordinate has its own origin and there has its own angle Θ that rotates 360 degrees. We can call the origin of the smaller circle C. The next step in our design, is to find the spot were we can place the pend tip within the radius of the smaller circle. Call tis location P. Here we use another function to determine the distance the pen tip from P. The distance is called l. In the code we use L, because l looks too much like 1 and can result in unexpected outcomes as well as an ambiguity error for l in our code editor.

Finding l

When these variables are combined, the motion of P can be calculated to solve for (x,y)

Solving for x

Solving for y

This is how we draw a circle within a circle. So when we plot the next dot, showing the rotation of the smaller circle within the larger one, we start by incrementing the ϴ by 5deg in both circles, which means we get a new centre for the smaller circle which itself has rotated another ϴ. We repeat the process of calculating the new (x,y) coordinates each time, and turtle animates the data to create beautiful curves representing this data in a more meaningfully way.

Using python, Θ is represent by a for angle or radii

for i in range(0, 360*self.nRot + 1, self.step):
            a = math.radians(i)
            x = R*((1-k)*math.cos(a) + l*k*math.cos$((1-k)*a/k))
            y = R*((1-k)*math.sin(a) - l*k*math.sin((1-k)*a/k))

Image showing the Spirograph output:

Image showing Spirographs

Deployment

If you would like to play experiment with circles as well as Spirographs, feel free to fork this mini-project. The code source is noted at the start of this README document & I hope that you will enjoy using it as much as I have. All credits to the original author.

When coding this project, I did so from within a virtual environment, so as to confine all the application dependencies to a python virtual container.

CMD Command: python -m venv .venv

You may then install the application dependencies: pip install -r requirements.txt

This application lets you save your spirographs to a PNG file within the application folder. Since the application when running, is on the safe side of the virtual environment, I also had to install Ghostscript a Postscript and PDF interpreter/renderer, which (Thanks to users that had the same problem as me, that had responded to questions on stackOverflow) permits the python application to find and use a filename path from within your .VENV If using GhostScript don't forget that you have to add the binary file location to your Windows system environmental variables, PATH; or else it won't work. Your PC system has to be configured to make GhostScript accessible to your python application from within your code editor.

Once done, you can run the application:

`python drawcircle.py` simply draws as circle.  You can change the hardcoded parameters yourself.

`python spiro.py --sparams 220 65 08`  
The application will generate a single Spirogram, by specifying the parameters for R r l

`python spiro.py` 
The application will generate 4 Spirograms, from random parameters for R r l

Keyboard Controls

t - Show / hide turtle
s - save drawing
spcebar - restart Spiro

If you find that they application doesn't work, then welcome to the fun stuff! Debugging is often the best part of development! I hope that you enjoy the magic of this application as much as I have.

About

Python Spirograph application - for dabbling with parametric equations to draw a circle

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages