Draw beautiful geometric visualizations with Python and Spyrograph
Tags: python, math, opensource, showdev
Published: 2023-03-12 18:32:04

In the world of mathematics there is a limitless supply of patterns, visualizations, and concepts just waiting to be explored

A particularly interesting set of concepts are known as **hypotrochoids** and **epitrochoids** - beautiful patterns traced by a point extending from a circle **rolling** around another **fixed** circle

![Pattern of white lines drawn on a dark background](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehz2h7mueir8ffs1c8ue.PNG)

Let's dive in and explore how `spyrograph` helps us **analyze** and **explore** these interesting mathematical concepts in just a few lines of code!

{% github https://github.com/chris-greening/spyrograph %}

## Table of Contents

- [What are hypotrochoids and epitrochoids?](#what-are-hypotrochoids-and-epitrochoids)
- [Exploring the math behind these patterns](#exploring-the-math-behind-these-patterns)
- [Installing spyrograph](#installing-spyrograph)
- [Using `spyrograph` for effortless modeling](#using-spyrograph-for-effortless-modeling)
- [Conclusion](#conclusion)
- [Additional resources](#additional-resources)

## What are hypotrochoids and epitrochoids? <a src="#what-are-hypotrochoids-and-epitrochoids"></a>

Simply put, **hypotrochoids** and **epitrochoids** are curves formed by **tracing** a point extending from the center of a circle as it **rolls** around the **inside** (for a hypotrochoid) or the **outside** (for an epitrochoid) of a **fixed** circle

![Small circle rolls around the outside of a larger circle drawing a pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdvq2potmp9onbzz5hll.gif)

The resulting curves can be incredibly intricate, elegant, and pleasing to the eye and have a surprisingly wide range of applications ranging from **engineering and astronomy** to even patterns printed on **banknotes and passports** as security measures against counterfeiting

## Exploring the math behind these patterns <a src="#exploring-the-math-behind-these-patterns"></a>

Hypotrochoids and epitrochoids can be broken down to a few key inputs and a set of parametric equations

The **inputs** are:

- {% katex inline %} R {% endkatex %}: the **radius** of the fixed circle
- {% katex inline %} r {% endkatex %}: the **radius** of the rolling circle
- {% katex inline %} d {% endkatex %}: the **distance** from the interior of the rolling circle
- {% katex inline %} \theta {% endkatex %}: the **angle** formed by the horizontal and the center of the rolling circle

with the **parametric equations** defined as:

### Hypotrochoid

{% katex inline %} x(\theta) = (R - r)cos\theta + d cos(\frac{R - r}{r}\theta) {% endkatex %}
{% katex inline %} y(\theta) = (R - r)sin\theta - d sin(\frac{R - r}{r}\theta) {% endkatex %}

### Epitrochoid

{% katex inline %} x(\theta) = (R + r)cos\theta - d cos(\frac{R + r}{r}\theta) {% endkatex %}
{% katex inline %} y(\theta) = (R + r)sin\theta - d sin(\frac{R + r}{r}\theta) {% endkatex %}

Given this information, we can draw our curves using the output {% katex inline %} (x,y) {% endkatex %} coordinates calculated by the parametric equations

![Plot of a circular pattern shaped like a donut](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxg16af4ltt0l982a2yr.png)

## Installing spyrograph <a src="#installing-spyrograph"></a>

Getting `spyrograph` **installed** and ready to go is as easy as installing the **latest stable release** from [PyPI](https://pypi.org/project/spyrograph/) or cloning the **development version** from the official [GitHub](https://github.com/chris-greening/spyrograph) repo

### PyPI

`pip3 install spyrograph`

### GitHub

`git clone https://github.com/chris-greening/spyrograph.git`

**No additional configurations** or third-party libraries necessary to get started; `spyrograph` leverages the **Python standard library** with optional support for [pandas](https://pandas.pydata.org/) and [matplotlib](https://matplotlib.org/)

## Using `spyrograph` for effortless modeling <a src="#using-spyrograph-for-effortless-modeling"></a>

The purpose of `spyrograph` is to take the parametric equations  and build them into an **expressive, easy-to-use** set of tools for quickly **modelling, exploring, and visualizing** the behavior of these curves in Python

```python
from spyrograph import Hypotrochoid
import numpy as np

hypotrochoid = Hypotrochoid(
    R=423,
    r=203,
    d=101,
    thetas=np.arange(0, 720, .1)
)
hypotrochoid.trace()
```

![A donut shaped circular pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9lukjxc3pxhu6ck69xs.PNG)

With just our input parameters and a couple lines of code, we can get on our way tracing our curves and generating stunning visualizations letting `spyrograph` abstract away the underlying math and implementation details

On top of hypotrochoids and epitrochoids, we also define special cases of these curves such as the

- `Hypocycloid`
- `Epicycloid`
- `Deltoid`
- and the `Astroid`

for explicit, **object-oriented** models that are clean, flexible, and ready for programming against

## Conclusion <a src="#conclusion"></a>

And just like that we can draw **beautiful, complex patterns** and shapes letting `spyrograph` deal with all the math and visualization boilerplate

Just plug-and-play your parameters and start exploring the beautifiul world of math!

## Additional references <a src="#additional-resources"></a>
- [Hypotrochoid -- from Wolfram MathWorld](https://mathworld.wolfram.com/Hypotrochoid.html)
- [Hypotrochoid - Wikipedia](https://en.wikipedia.org/wiki/Hypotrochoid)
- [Roulette -- from Wolfram MathWorld](https://mathworld.wolfram.com/Roulette.html)
- [Parametric Equations](https://mathworld.wolfram.com/ParametricEquations.html#:~:text=Parametric%20equations%20are%20a%20set,(2))