Customizing the origin of our geometric visualizations using Python and Spyrograph
Tags: python, showdev, tutorial, opensource
Published: 2023-04-10 23:20:27

_DISCLAIMER: This post was written by a human with the help of AI_ 

## Introduction <a src="#introduction"></a>
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkx6x6hz5zjcdofznbmk.gif)

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
- [Introduction](#introduction)
- [Understanding the role of the origin in trochoid curves](#understanding-the-role-of-the-origin-in-trochoid-curves)
- [Customizing the origin](#customizing-the-origin)
- [Combining multiple trochoid shapes with different origins](#combining-multiple-trochoid-shapes-with-different-origins)
- [Example: Creating a complex spirograph design by varying the origin](#example:-creating-a-complex-spirograph-design-by-varying-the-origin)
- [Tips and tricks for choosing the right origin](#tips-and-tricks-for-choosing-the-right-origin)

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

---

## Understanding the role of the origin in trochoid curves <a src="#understanding-the-role-of-the-origin-in-trochoid-curves"></a> 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wav0oqors8pexhb56xc.gif)

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 <a src="#customizing-the-origin"></a>
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)**:

```python
shape = Hypotrochoid(
    R=200,
    r=150,
    d=100,
    thetas=np.arange(0, 10*np.pi, .01),
    origin=(100, 100)
)
```

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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mscm63f4p4a2rhd0mkgh.PNG)

### 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.

```python
shape.trace(exit_on_click=True)
```

---

## Combining multiple trochoid shapes with different origins <a src="#combining-multiple-trochoid-shapes-with-different-origins"></a>
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.

```python
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)
)
```

### Trace each shape on the same screen <a src="##trace-each-shape-on-the-same-screen"></a>
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](https://chris-greening.github.io/spyrograph/python/2023/04/08/tracing-multiple-shapes-on-the-same-screen-with-spyrograph.html) post for more info)

```python
screen, turtles = shape1.trace(
    screen_color="white",
    color="black"
)
shape2.trace(
    screen=screen,
    exit_on_click=True
)
```


![Two identical shapes traced directly next to each other](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk2ou3giq6icaz69lqpq.gif)

### Customize and experiment <a src="##customize-and-experiment"></a>
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 <a src="#example:-creating-a-complex-spirograph-design-by-varying-the-origin"></a>

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](https://www.w3schools.com/colors/colors_hexadecimal.asp) towards white to give an interesting fade effect:

```python
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()
```


![A shape traced from lines fading in from a dark background](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxn764er8jy0tjro7n1y.gif)

---

## Tips and tricks for choosing the right origin <a src="#tips-and-tricks-for-choosing-the-right-origin"></a>
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 <a src="##consider-symmetry"></a>
**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 <a src="##offset-shapes-for-contrast"></a>
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqufwe00pgzfzfv9hc2t.gif)

### Think about screen size and aspect ratio <a src="##think-about-screen-size-and-aspect-ratio"></a>
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 <a src="##trial-and-error"></a>
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 <a src="##opt-for-automation"></a>
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.

```python
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()
```


![Grid of tracings being drawn one after the other](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjk5pafu1xp7qxhb6ibb.gif)

### Use existing artwork for inspiration <a src="##use-existing-artwork-for-inspiration"></a>
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 <a src="##keep-it-simple"></a>
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.