Home › Code art › Lesson 2

Neon titli: ek equation

Code art with Raj, notes 2. One equation, 18 lines of Python, and a glowing neon butterfly. Then we change one number and watch the wings. Free, simple English.

Run this code in your browser

The idea

There is a famous curve called the butterfly curve. Temple H. Fay found it in 1989. It is drawn from one equation in polar form: for every angle t, the equation gives a distance r from the centre. Join all the points and a butterfly appears.

The classic equation is r = e^cos(t) − 2·cos(4t) + sin⁵(t/12), with t going from 0 to 12π. Our code writes the "2" as a number called K, so we can change it.

Important: K = 2 is the butterfly curve by Fay. The other K values (4, 1, 0.5, 0) are our own experiments on this page. They are not Fay's curve.

The code: butterfly.py

You need Python and Pillow (pip install pillow). Make a folder called frames next to the file first (mkdir frames), because the code saves the pictures there. The file has 18 real code lines, plus comments.

# Neon butterfly from ONE equation (our own code). Polar butterfly curve, T. H. Fay (1989):
#   r = e^cos(t) - K*cos(4t) + sin(t/12)^5      <- K is the "one number" we will change
import math
from PIL import Image, ImageDraw, ImageFilter, ImageChops

def butterfly(k=2, size=1080, name="butterfly"):
    sc = 0.105 if k <= 2 else 0.082                      # smaller scale so big wings still fit
    pts = []
    for i in range(0, 24000):
        t = i * 12 * math.pi / 24000                      # t from 0 to 12*pi
        r = math.exp(math.cos(t)) - k * math.cos(4 * t) + math.sin(t / 12) ** 5
        pts.append((size / 2 + r * math.sin(t) * size * sc, size * 0.46 - r * math.cos(t) * size * sc))
    glow = Image.new("RGB", (size, size), "black")
    ImageDraw.Draw(glow).line(pts, fill=(255, 40, 200), width=7)
    neon = Image.new("RGB", (size, size), "black")
    for radius in (40, 18, 8):                            # layered blur = neon glow
        neon = ImageChops.add(neon, glow.filter(ImageFilter.GaussianBlur(radius)))
    ImageDraw.Draw(neon).line(pts, fill=(255, 230, 255), width=2)   # thin bright core
    neon.save(f"frames/{name}.png")

for k in (2, 4, 0.5):
    butterfly(k, name=f"butterfly-K{k}")

Run it: python butterfly.py. It saves three pictures in frames, for K = 2, 4 and 0.5.

How it works

The experiment: change K

To get all five pictures below, change the for line at the bottom to for k in (2, 4, 1, 0.5, 0): and run it again. Everything else stays the same.

Neon pink butterfly curve with K equal to 2: four wings
K = 2 The classic butterfly curve (Fay, 1989).
Neon pink curve with K equal to 4: much larger overlapping wings
K = 4 Our experiment: much larger wings that overlap.
Neon pink curve with K equal to 1: smaller heart-shaped wings
K = 1 Our experiment: smaller, heart-shaped wings.
Neon pink curve with K equal to 0.5: a closed notched shape
K = 0.5 Our experiment: the wings shrink into a closed shape with a notch.
Neon pink curve with K equal to 0: nested rings and no wings
K = 0 Our experiment: no wings, only nested rings.

So the number in front of cos(4t) is what makes the wings. Make it smaller and the wings shrink. Make it bigger and they grow. At zero they are gone.

Try it yourself

Remember

A drawing can come from one equation. Change one number, K, and you get a different drawing. The sources say the sin⁵(t/12) part was added mainly to make the picture look nicer.

Sources

The code and pictures on this page are our own.

← Sunflower ka secret: 137.5°