DEV Community

Cover image for Creating a range of hypotrochoids with the create_range method using Spyrograph and Python
Chris Greening
Chris Greening

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

Creating a range of hypotrochoids with the create_range method using Spyrograph and Python

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

When working with the spyrograph package, we often want to create a range of different shapes by varying one of the input parameters while keeping the others constant

This is where the create_range class method comes in handy!

In this blog post let's walk through how to use the create_range method to generate a list of instantiated Hypotrochoid objects with varying input parameters

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 create_range method

The create_range method allows us to create a range of different hypotrochoids and epitrochoids by varying one of the input parameters (R, r, or d) while keeping the others constant

This method accepts the following parameters:

  • R: The radius of the fixed circle, either a single number or a list of numbers.
  • r: The radius of the rolling circle, either a single number or a list of numbers.
  • d: The distance of the trace point from the rolling circle, either a single number or a list of numbers.
  • thetas: An optional list of values for theta for inputting into parametric equations.
  • theta_start: An optional starting theta value for creating a list of thetas.
  • theta_stop: An optional stop theta value for creating a list of thetas, where the stop value is not included in the final array.
  • theta_step: An optional incremental step value for stepping from start to stop.
  • origin: An optional custom origin to center the shapes at, with the default being (0, 0).

The method returns a list of instantiated Hypotrochoid objects where one of the input parameters is a list of increments and the others are fixed

Using the create_range method

Let's say we want to create a range of hypotrochoids with varying radii of the rolling circle (r) while keeping the radii of the fixed circle (R) and the distance of the trace point from the rolling circle (d) constant

To do this we can use the create_range method as follows:

from spyrograph import Hypotrochoid
import numpy as np
import turtle

arr = Hypotrochoid.create_range(
    R=600,
    r=np.arange(295,300,.2),
    d=51,
    theta_start=0,
    theta_stop=35*np.pi,
    theta_step=.1
)
Enter fullscreen mode Exit fullscreen mode

Now, hypotrochoids contains a list of Hypotrochoid objects with varying r values

We can then use the trace method to visualize these shapes:

screen = None
for shape in arr:
    screen = shape.trace(
      screen=screen,
      color="white",
      screen_color="black"
    )
turtle.exitonclick()
Enter fullscreen mode Exit fullscreen mode

This will display the hypotrochoids one after the other on the same screen resulting in a more complex looking image

Black background with white line geometric shapes animated to trace a circular pattern

Conclusion

In this post, we explored the powerful create_range method provided by the spyrograph package, which allows us to generate a wide variety of beautiful hypotrochoids and epitrochoids with just a few lines of code

By specifying a range of values for R, r, or d we can quickly create and visualize multiple shapes, making it an excellent tool for artists, educators, and anyone interested in exploring the fascinating world of spirograph patterns

With spyrograph, the possibilities for creating captivating designs are virtually endless - we hope this method inspires your creativity and leads to the discovery of stunning spirograph patterns unique to you!

Top comments (0)