DEV Community

Cover image for Tracing multiple shapes on the same screen with Spyrograph
Chris Greening
Chris Greening

Posted on • Originally published at chris-greening.github.io

Tracing multiple shapes on the same screen with Spyrograph

DISCLAIMER: This blog post was written by a human with the help of AI

Introduction

In this tutorial, we'll demonstrate how to chain screens for multiple traces, allowing us to create captivating artwork by layering different spirograph shapes on the same canvas with Spyrograph!

Follow along as we use a custom code snippet to showcase this technique in action

GitHub logo chris-greening / spyrograph

Python library for analyzing, exploring, and visualizing epitrochoids and hypotrochoids in just a few lines of code

spyrograph: elegant mathematics and geometries

Animation of three geometric, symmetrical shapes being drawn next to one another left to right. The shape on the left is red, the middle green, and the right is blue.

What is it?

spyrograph is a lightweight Python package that provides an expressive and flexible set of tools for drawing beautiful mathematically driven art. With just a few lines of easy-to-read code you can start analyzing, visualizing, and exploring elegant mathematics

Downloads Issues License Version Documentation Status

"Buy Me A Coffee"

Official website

Official docs

Table of Contents


πŸ”‘ Key features

  • Expressive and consistent syntax
  • Robust underlying mathematics
  • Beginner and expert friendly
  • numpy is the only required third-party installation
  • Clear visualizations and animations
  • Flexible to a wide range of usecases
  • Lightweight, just plug and play

Sample hypotrochoid drawing showing a circle rolling around the interior of another circle drawing a geometric shape


πŸ’» Installation

pip

Install the latest stable release from PyPI using

$ pip3 install spyrograph
Enter fullscreen mode Exit fullscreen mode

or clone the development version from GitHub with

$ git clone https://github.com/chris-greening/spyrograph.git
Enter fullscreen mode Exit fullscreen mode

🌱 Quickstart

spyrograph is designed to be expressive and easy-to-use - simply import spyrograph and jump right into drawing elegant, complex shapes…





Creating our first spirograph

To begin, let's create our first spirograph shape using the Hypotrochoid class. Import the necessary libraries and define the required parameters for the shape

from spyrograph import Hypotrochoid
import numpy as np

first_shape = Hypotrochoid(
    R=233,
    r=200,
    d=233,
    thetas=np.arange(0, 100*np.pi, .5)
)
Enter fullscreen mode Exit fullscreen mode

Tracing the first shape

Next, trace the first shape and keep the screen open by calling the trace method. By default, the exit_on_click option is set to False, which ensures that the screen remains active after tracing the first shape

screen, turtles = first_shape.trace()
Enter fullscreen mode Exit fullscreen mode

A black tracing is drawn on white background


Creating a second shape

Now, let's create a second shape by using the scale method to adjust the size of the first shape into a slightly smaller copy

second_shape = first_shape.scale(.85)
Enter fullscreen mode Exit fullscreen mode

Trace the second shape on the same screen

Trace the second shape on the same screen by passing the existing screen to the trace function. Additionally, set the exit_on_click option to True and choose a different color for the second shape

second_shape.trace(
    exit_on_click=True,
    color="red",
    screen=screen
)
Enter fullscreen mode Exit fullscreen mode

A copy of the first shape is traced on the same screen but slightly smaller and in red


Experiment with different shapes and colors

Feel free to experiment with different shapes, sizes, and colors to create your unique spirograph artwork. You can create and layer multiple shapes on the same screen by following the steps outlined above


Conclusion

Chaining screens for multiple traces is a powerful technique to create visually stunning spirograph designs

By layering different shapes and colors on the same canvas, you can produce eye-catching and intricate artwork

Don't be afraid to explore various patterns and unleash your creativity with Spyrograph!

Top comments (0)