Adding a boomerang effect to our Spyrograph animations
Tags: python, opensource, tutorial, contributorswanted
Published: 2023-04-07 14:10:51

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

## Introduction <a src="#introduction"></a>
Spirographs are **captivating geometric designs** created by **rolling a circle** inside or outside **another circle**. These mesmerizing patterns have fascinated artists, mathematicians, and enthusiasts alike for centuries. With the power of Python and the [spyrograph](https://github.com/chris-greening/spyrograph) library, we can create complex spirograph animations that visualize the evolution of these intricate patterns

In this blog post, we will explore the `boomerang` effect, a newly introduced feature in the `spyrograph` library, which allows us to create mesmerizing animations that play **forward and then in reverse**. We'll provide examples and explanations on how to harness this feature to create captivating spirograph animations

## Table of contents
- [Introduction](#introduction)
- [The boomerang effect](#the-boomerang-effect)
- [Example 1: Basic boomerang animation](#example-1-basic-boomerang-animation)
- [Example 2: Combining boomerang with looping](#example-2-combining-boomerang-with-looping)
- [Example 3: boomerang effect with custom configurations](#example-3-boomerang-effect-with-custom-configurations)

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

---

## The boomerang effect <a src="#the-boomerang-effect"></a>
The boomerang effect is achieved by playing the animation sequence in **reverse** after it reaches the end, creating a **back-and-forth effect**. This feature is particularly useful when you want to visualize the **evolution of a spirograph** pattern with changing parameters and then observe how it returns to its **initial state**

![An animation of a white tracing of a star that is rotating back and forth on a black background](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yklbj0r6n60rlgmxs26j.gif)

To enable the `boomerang` effect, we need to set `boomerang=True` when calling the `animate` method of the `spyrograph` library. In the following sections, we will demonstrate how to use this feature with various examples.


---

## Example 1: Basic boomerang animation <a src="#example-1-basic-boomerang-animation"></a>
In this example, we will create a **simple spirograph animation** with the boomerang effect. We will use the `Hypotrochoid` class from the `spyrograph` library to generate our animation

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

# Animate a hypotrochoid by incrementally
# changing the rolling radius r
Hypotrochoid.animate(
    R=200,
    r=np.arange(113.75, 114.25, .01),
    d=133,
    thetas=np.arange(0,100, .05),
    frame_pause=.01,
    boomerang=True,
)
```

![An animation of a tracing of a star that is rotating back and forth](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsbjdnoq4itv6azkoxhp.gif)

This example generates a `Hypotrochoid` animation with varying `r` values while keeping `R` and `d` the same. After reaching the end of the sequence, the animation plays in reverse, creating a boomerang effect.


---

## Example 2: Combining boomerang with looping <a src="#example-2-combining-boomerang-with-looping"></a>
The boomerang effect can also be **combined with looping**, allowing the animation to play **continuously**. To achieve this, set both the `boomerang` and `repeat` arguments to `True` in the `animate` method

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

# Repeating animation of a hypotrochoid by
# incrementally changing the rolling radius r
Hypotrochoid.animate(
    R=200,
    r=np.arange(113.75, 114.25, .01),
    d=133,
    thetas=np.arange(0,100, .05),
    boomerang=True,
    repeat=True
)
```
With these settings, the animation will loop indefinitely, playing forwards and then in reverse, creating an entrancing visual experience


---

## Example 3: boomerang effect with custom configurations <a src="#example-3-boomerang-effect-with-custom-configurations"></a>
The boomerang effect can be combined with other **custom configurations**, such as changing the:
- background color
- line color
- or line width.

In this example, we will create an animation with a black background, a neon green tracing line, and a thicker line width

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

# Neon green hypotrochoid boomerang
# animation on black background
Hypotrochoid.animate(
    R=200,
    r=np.arange(113.75, 114.25, .01),
    d=133,
    thetas=np.arange(0,100, .05),
    frame_pause=.01,
    repeat=True,
    boomerang=True,
    screen_color="black",
    color="#39FF14",
    width=3
)
```

![An animation of a neon green tracing of a star that is rotating back and forth on a black background](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0bvxapg4f0mhzof37uq.gif)

This animation will feature a **striking contrast** between the neon green spirograph patterns and the black background. The boomerang effect adds another layer of visual interest to this already captivating animation

## Conclusion
The boomerang effect is a **powerful addition** to the `spyrograph` library, allowing us to create **captivating spirograph animations** that play forwards and in reverse. By combining this feature with various parameter configurations and customization options, we can create a **wide array of mesmerizing** spirograph animations

With a basic understanding of the boomerang effect and the examples provided in this blog post, we are **well-equipped** to explore the **endless possibilities** of spirograph animations. Whether you are an artist, mathematician, or a spirograph enthusiast, the boomerang effect is sure to add a unique touch to your spirograph creations