DEV Community

Cover image for Drawing cycloids with a specific number of cusps using Spyrograph
Chris Greening
Chris Greening

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

Drawing cycloids with a specific number of cusps using Spyrograph

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

In this blog post, we will explore a powerful feature of the spyrograph package: the ability to create and draw cycloids with a specified number of cusps

Cycloids are special cases of trochoids where the distance parameter d is equal to the rolling circle radius r

By using the n_cusps class method provided in the Hypocycloid and Epicycloid classes, we can easily create beautiful cycloid shapes with a desired number of cusps


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…





Prerequisite imports

Before we begin, let's make sure we have the spyrograph package installed:

pip3 install spyrograph
Enter fullscreen mode Exit fullscreen mode

Creating a cycloid with a specified number of cusps

To create a cycloid with a specified number of cusps, we can use the n_cusps class method. Here's a quick example of how to create a hypocycloid with 5 cusps:

from spyrograph import Hypocycloid
import numpy as np

R = 50
n = 5
thetas = np.arange(0, 2 * np.pi, 0.01)

hypocycloid = Hypocycloid.n_cusps(R=R, n=n, thetas=thetas)
Enter fullscreen mode Exit fullscreen mode

In this example, we set the radius of the fixed circle R to 50 and the desired number of cusps n to 5. The n_cusps method calculates the rolling circle radius r as R/n and instantiates a cycloid object with the provided parameters


Tracing the cycloid with trace

Now that we have created a hypocycloid with 5 cusps, let's trace it using the trace method provided by the spyrograph package

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

Tracing of a hypocycloid with 5 cusps being drawn

The trace method will display an animation of the cycloid shape being drawn

The exit_on_click argument ensures that the animation window remains open until you click on it


Conclusion

The n_cusps class method in the Hypocycloid class is a powerful and easy-to-use feature for creating and drawing cycloid shapes with a specified number of cusps

By using this method, artists and educators alike can create visually stunning and mathematically precise cycloids in a matter of seconds

Give it a try and unlock your creativity with the spyrograph package today!

Top comments (0)