DEV Community

Cover image for Customizing the origin of our geometric visualizations using Python and Spyrograph
Chris Greening
Chris Greening

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

Customizing the origin of our geometric visualizations using Python and Spyrograph

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

Introduction

Trochoids and cycloids are beautiful geometric shapes generated by tracing a point on a rolling circle as it moves around a fixed circle - these shapes have captivated artists, mathematicians, and enthusiasts for centuries with their elegant, geometric patterns

These mesmerizing patterns, often seen in spirograph designs, offer endless possibilities for exploration and creativity. One of the most powerful ways to customize and control the appearance of trochoid curves is by adjusting the origin โ€“ the central point around which the entire pattern revolves

Red shape fading in from black background

In this blog post, we will delve into the concept of customizing the origin in our designs uncovering its role in the shape's geometry and its impact on the final design. We will explore various techniques to shift and manipulate the origin, as well as demonstrate how combining multiple trochoid shapes with different origins can result in intricate and visually stunning patterns

Table of contents

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โ€ฆ





Understanding the role of the origin in trochoid curves

Before diving into the process of customizing the origin, it's essential to understand its role in trochoid curves. The origin serves as the central point around which the entire pattern revolves. It determines the position of the fixed circle and, consequently, influences the trajectory of the rolling circle and the trace point

See the animation below as an example where the bright green dot is the origin:

A fixed circle with a bright green dot at its center is tracing a shape

By default, the origin is set at the coordinate (0, 0), placing the fixed circle's center at this location. However, by customizing the origin, we can shift the entire pattern to a different point in the Cartesian plane, thus altering the position of the trochoid curve

Keep in mind that changing the origin only affects the position of the pattern, not its shape or size. To modify the trochoid curve's actual geometry, you would need to adjust other parameters such as the radii of the fixed and rolling circles (R, r) or the distance (d) of the trace point from the rolling circle


Customizing the origin

Now that you have a clear understanding of the role of the origin in spirographs, let's walk through the steps to customize it. The process is quite straightforward, as you only need to modify the origin parameter when creating your shape (i.e. Hypotrochoid, Epicycloid, etc.)

Choose the new origin

First, decide on the new origin coordinates you want to use for your trochoid curve. These coordinates should be a tuple of two numbers representing the x and y values, e.g., (x, y)

Create a trochoid curve with the custom origin

When creating a new shape, pass the chosen origin coordinates as the origin parameter

For example the following code snippet creates a Hypotrochoid with a custom origin at the point (100, 100) instead of the default (0, 0):

shape = Hypotrochoid(
    R=200,
    r=150,
    d=100,
    thetas=np.arange(0, 10*np.pi, .01),
    origin=(100, 100)
)
Enter fullscreen mode Exit fullscreen mode

This results in the tracing being translated to the right 100 pixels and then up 100 pixels as shown below where the black tracing is at the default (0, 0) and the red tracing is at (100, 100)

Two identical shapes. One is black at the center of the screen and the other is red and translated up and over.

Visualize the curve

After creating the trochoid curve with the custom origin, you can visualize it using the trace method. The custom origin will be taken into account when drawing the pattern, and the fixed circle's center will be placed at the specified origin.

shape.trace(exit_on_click=True)
Enter fullscreen mode Exit fullscreen mode

Combining multiple trochoid shapes with different origins

Incorporating multiple trochoid shapes with different origins into a single design can create stunning, complex patterns. Here's a step-by-step guide on how to combine multiple trochoid shapes with different origins:

Create individual trochoid shapes

Start by creating each trochoid shape with its unique parameters (R, r, d, and thetas). Make sure to also set the origin for each shape, as this will determine its position relative to the other shapes.

shape1 = Hypotrochoid(
    R=211,
    r=151,
    d=137,
    thetas=np.arange(0, 40*np.pi, .1),
    origin=(-50, 0)
)
shape2 = Hypotrochoid(
    R=211,
    r=151,
    d=137,
    thetas=np.arange(0, 40*np.pi, .1),
    origin=(50, 0)
)
Enter fullscreen mode Exit fullscreen mode

Trace each shape on the same screen

To draw the shapes on the same screen, use the trace method for each shape and capture the returned turtle.Screen as a variable to pass into the next trace call to persist the designs onto the same screen. Note that the screen configurations defined in your first trace will determine how the screen looks for subsequent trace's (see this post for more info)

screen, turtles = shape1.trace(
    screen_color="white",
    color="black"
)
shape2.trace(
    screen=screen,
    exit_on_click=True
)
Enter fullscreen mode Exit fullscreen mode

Two identical shapes traced directly next to each other

Customize and experiment

Feel free to modify the parameters, colors, and origins of each shape to create unique and interesting patterns. By changing the origins, you can position the shapes in various ways, such as stacking them, creating a grid, or designing a mosaic.

By following these steps, you can create intricate designs that combine multiple shapes with different origins. This technique offers endless possibilities for creativity and customization, allowing you to produce stunning visual displays


Example: Creating a complex spirograph design by varying the origin

Manipulating the origin of trochoid shapes can produce intricate and fascinating spirograph patterns. In this section we will use a for loop to draw 25 Hypotrochoid's, each shifted slightly to the right compared to the previous iteration of the loop. We will also modify the color starting from black and slightly lighten the hex color towards white to give an interesting fade effect:

from spyrograph import Hypotrochoid
import turtle
import numpy as np
import time

screen = None
for i in range(0, 26, 1):
    origin = (i*15-200, 0)
    color = "#{:02x}{:02x}{:02x}".format(i*10, i*10, i*10)
    shape = Hypotrochoid(
        R=211,
        r=100,
        d=50,
        thetas=np.arange(-np.pi, 18*np.pi, 0.15),
        origin=origin
    )
    screen, turtles = shape.trace(
        screen_color="black",
        color=color,
        screen_size=(1000,1000),
        screen=screen
    )
    time.sleep(.1)
turtle.exitonclick()
Enter fullscreen mode Exit fullscreen mode

A shape traced from lines fading in from a dark background


Tips and tricks for choosing the right origin

Selecting the right origin for your trochoid shapes can make all the difference in creating captivating spirograph designs. Here are some tips and tricks to help you choose the perfect origin for your creations:

Consider symmetry

Symmetry is often visually pleasing and can create a sense of balance in your designs. When choosing the origin for multiple shapes, try to create symmetrical patterns by aligning the shapes relative to one another.

Offset shapes for contrast

By setting the origin of different shapes with an offset, you can create striking contrasts that make each shape stand out. This technique can be particularly effective when you're working with shapes that have similar parameters but different colors.

Three shapes drawn next to one another in varying colors

Think about screen size and aspect ratio

Keep in mind the size and aspect ratio of the screen or canvas on which you plan to display your design. Choose origins that fit comfortably within the available space and avoid placing shapes too close to the edges to prevent clipping or distortion

Trial and error

Don't be afraid to experiment with different origins. Sometimes, the best designs emerge from unexpected combinations. Adjust the origin of your shapes, observe the results, and fine-tune your choices until you achieve the desired effect.

Opt for automation

If you're working with a large number of shapes or seeking a more randomized design, consider writing a script to automate the process of selecting origins. For example, you can use a loop or a random number generator to create a grid or a circular arrangement of shapes.

from spyrograph import Hypotrochoid
import numpy as np
import turtle

screen = None
for x in range(-500, 500, 100):
    for y in range(-500, 500, 100):
        origin = (x, y)
        shape = Hypotrochoid(
            R=52,
            r=25,
            d=12.5,
            thetas=np.arange(-np.pi, 20*np.pi, 0.15),
            origin=origin
        )
        screen, turtles = shape.trace(
            screen_color="black",
            color="white",
            screen_size=(1000,1000),
            screen=screen,
            width=2
        )
turtle.exitonclick()
Enter fullscreen mode Exit fullscreen mode

Grid of tracings being drawn one after the other

Use existing artwork for inspiration

Look at existing spirograph designs, mandalas, or geometric patterns for inspiration when choosing the origins for your shapes. You can replicate or modify these designs, or use them as a starting point to develop your own unique creations.

Keep it simple

Sometimes, simplicity is key. You don't always need to choose complex origins or create intricate patterns to make a visually appealing design. Experiment with a single shape or a small number of shapes and see how changing the origin affects the overall appearance.

By keeping these tips and tricks in mind when choosing the origin for your trochoid shapes, you can create visually captivating spirograph designs that showcase your creativity and artistic flair.

Top comments (0)