Spirograph in python with source code

Spirograph in python with source code

Introduction

A Spirograph is a geometric drawing device that is used to draw a pattern of cyclical curved lines.  We can create our own Spirograph device using python turtles.

A Spirograph is a very interesting geometrical figure that has different colors. It is a geometric drawing that generates mathematical curves called hypotrochoids and epitrochoids. These patterns are formed by following the route of a point attached to a circle as it rolls within or outside another circle. Denys Fisher, a British engineer, invented the spirograph as a mechanical sketching toy in 1965. It immediately became a popular tool for producing elaborate designs.

Today, computer languages such as Python allow us to reproduce and even improve the capabilities of this classic toy. They are a bunch of circles overlapping each other and forming in a circular structure. In this tutorial, we are creating a spriograph drawing using Python turtle graphics. Turtle graphics is a fun way of learning programming and using graphics in python programming. For creating a spriograph, we must import turtle, random library etc. See the code below.

import turtle as t
import random

tim = t.Turtle()
t.colormode(255)

def random_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    color = (r, g, b)
    return color

tim.speed("fastest")

def draw_spiograph(size_of_gap):
    for _ in range(int(360 / size_of_gap)):
        tim.color(random_color())
        tim.circle(100)
        tim.setheading(tim.heading() - size_of_gap)

draw_spiograph(5)

screen = t.Screen()
screen.exitonclick()

output:

spirograph

How to use this Spirograph project?

  • Copy the code and paste in your editor.
  • Execute the code.
  • See the output making.
  • Enjoy and share.

Why Use Python?


Python is an excellent choice for data visualization and graphic representation. Python allows you to not only draw spirographs, but also animate them, colorize them, and save the results as photos or films. It’s user-friendly, well-documented, and supports a wide number of libraries, including:

  • Turtle Graphics is a typical Python module for 2D drawing.
  • Matplotlib is a graphing package that can render parametric forms.
  • Pygame: For interactive and real-time rendering.
  • Tkinter – For creating GUIs and adding drawing features.


Real-World Applications

  • Clock Mechanisms: Gears and rotations have similar logic.
  • Signal processing: waveforms and cycles approximate spirograph tracks.
  • Graphic Design: Many logos and backdrop patterns resemble spirographs.
  • Security Patterns: Banknotes and official certificates frequently incorporate spirograph-like motifs to avoid counterfeiting.
  • Animation and Games: Generative art for games and visual simulations.