ECE-C301 Advanced Programming for Engineers
Programming Assignment 2
Part 1 – Problem Background
In this assignment you will create a virtual spirograph using the principles of object oriented programming. Figure 1 shows a physical spirograph creation setup consisting of a circular toothed
track (red) within which the user would place toothed plastic wheels (blue) having pen placement
holes placed slightly off the wheel center.
Figure 1: An “old fashion” manual spirograph toy.
The geometry of the toy can be represented as shown in Figure 2, where C is the center of the
smaller circle, and P is the pen’s tip.
P = (x, y)
Figure 2: Geometry of the spirograph problem.
The radius of the bigger circle is R and the smaller circle r. For convenience, we will express the
ratio between these two radii as k:
Let d represent the distance of the pen tip P from the center of the smaller circle C. Again, for
convenience, we will define the variable l to represent distance of our pen tip from C normalized to
within 0 and 1. Here, l = 0 places the pen tip at C and l = 1 at the edge of the circle):
We can now represent the coordinates of the pen tip as P⃗ (θ) = [Px(θ), Py(θ)] via:
Px(θ) = R
(1 − k)cos(θ) + lkcos (
1 − k
Py(θ) = R
(1 − k)sin(θ) − lksin (
1 − k
A spirograph curve is complete when the starting and ending points of the continuous line forming
the curve meet. If you have ever played with a real spirograph, you will know that the pen will need
to move through multiple complete cycles of θ ∈ [0, 2π] before the final spirograph curve is complete.
A simple way of doing this is to compute the periodicity of the spirograph curve by examining the
ratio of the small inner radius r to that of the larger outer radius R.
Take, for example, the spirograph shown in Figure 3 generated using parameters R = 220, r = 65, l =
0.8. The radii ratio for this spirograph is:
The period is found by reducing this ratio and taking the numerator. For our example, this would
So, the period of the spirograph is 13 revolutions. Additionally, the smaller circle of radius r will
revolve 44 times about its center, which will play a major factor in the final shape of the resulting
curve. Take a moment to count the number of “petals” on the spirograph shown in Figure 3–it’s 44.
R = 220
r = 65
l = 0.8
Figure 3: Example of a Spirograph curve.
Part 2 – Deliverables
For graphics operations, such as drawing, you will use the Turtle module. First lookup the Turtle
module documentation before starting. Remember, you can always get the documentation for a
module easily in Python’s interactive mode:
1 $ python
2 import turtle
You will write an importable Python module that implements class Spirograph having, at minimum, the following methods:
• init (self, R) – Create a new spirograph toy with outer radius R (i.e. the red part shown
in Figure 1).
• setSmallCircle(self, r) – Set the radius of the small circle used to draw (i.e. the blue part
shown in Figure 1).
• setPen(self, l, color) – Set the pen color and its distance from C
• draw(self) – Draw a spirograph using the current small circle and pen settings
• clear(self) – Reset the drawing surface
If you have written your program correctly, you should be able to draw multiple spirographs of
different shapes and colors on the same drawing surface, for example, by using the following code:
1 import spirograph
3 # Create a new Spirograph toy with R = 500
4 my_spirograph = Spirograph(500)
6 # Draw one curve
8 my_spirograph.setPen(0.65, ’red’)
11 # …and then draw another on top of the first
13 my_spirograph.setPen(0.22, ’blue’)
16 # …and then get a new sheet of paper
19 # …and draw another
21 my_spirograph.setPen(0.8, ’purple’)
You will submit your complete code to BBLearn. This includes your spirograph.py module as well
as another file named test.py which runs the above example that draws three superimposed curves
in red, blue, and purple. The TA should be able to run your code without needing to download,
move, copy, or modify additional files. Code that does not run will receive a zero. It is better to
have code that runs and gives the wrong answer than code that does not run.
You will also submit a report detailing the project in PDF format. This report should include, at
• an introduction detailing the mathematical background
• a detailed documentation of your code and its internal operation
• examples showing how somebody who has downloaded your code can use your Spirograph
module to generate various spirographs (include color figures).