Have you ever played with a spirograph? They are the toys with multiple cogs within which you place a pen. You follow the intended path, and the specific gear ratios allow you to draw some beautiful patterns and loops.
I never had one myself, but in this task, I simulate some in Python.
I then apply Fourier analysis to design a spirograph that can draw any closed loop shape.

All the code produced can be downloaded here:

Download ZIP File


Spirograph drawing

This part is really quite straight forward. First, define a set of lenghts, angular frequencies and initial phases. Then, simply plot a set of lines of those lengths which connect, end-to-end, and rotate with those frequencies and phases. Trace the curve drawn by the final line, and it will be beautiful!

The code for this is found under the spirographFrequencies.py file.

Fourier decomposition of loops

Let's say, now, that you wanted to use the lines to draw some shape of your choice.
To do this, we define the function that describes this curve. The curve can, by its nature, be embedded in a 2D-plane, So a simple way to represent this information is with a complex function of one real parameter, and the value of the function, as plotted on an Argand diagram, gives the curve.
We then decompose this function into complex phasor terms using the complex Fourier series. This provides an infinite set of frequencies, along with complex amplitudes, conveying length and phase information. One must simply choose a degree to truncate this series at, and plot the provided phasors with the spirograph tool.

My code allows the user to input a set of points, and the function essentially "connects the dots". This results in a piecewise complex function and, with enough effort, any shape can be plotted to a sufficient level of accuracy.
The mathematics to determine the Fourier coefficients of such a piecewise complex function is included in the appendix of the report on Current response of linear circuit to arbitrary voltage waveform.
The code for producing a drawing from a set of points is found under the fourierDrawing.py file.
Some example sets of points are included in the exampleCoordinates.txt file.

The video below gives an example of drawing my name with the tool!

Live drawing

If you dont want to label the points yourself, but merely drawing by clicking with your mouse, then I have the solution as well.
In the pickleFileDrawing folder, you can find code which allows you to click points on your screen. This is the coordinateMaker.py file.
After executing this program, the points are stored in x_data.pk and y_data.pk, where they are then called by the generalDrawingForCoordinateMaker.py file.

An example of this being used for a drawing with many points is in the skull folder.
Here, the dotsImage.pdf file contains a connect the dots for a skull which I completed and stored in the relevant pickle files. The drawSkull.py file then executes the drawing of this file. Below you can see an image of the final result it drew.