Home › Code art › Lesson 1

Sunflower ka secret: 137.5°

Code art with Raj, notes 1. Draw a sunflower head with 15 lines of Python, then break it on purpose. Free, simple English.

Run this code in your browser

The idea

Put dot number 1, then dot number 2, then dot number 3, and so on. Each new dot goes a little further from the centre, and turns by the same angle from the dot before it. That is the whole recipe.

The only thing you choose is that angle. With 137.5° (the "golden angle") the dots spread out evenly and you get a sunflower-like head with spirals in it. With other angles, the picture changes a lot. Run it and see.

Important: this is a model of a sunflower head, not the exact formula of a real flower. Real sunflowers are only roughly like it. The model is called the Vogel spiral (see sources).

The code: sunflower.py

You need Python and Pillow (pip install pillow). Nothing else. The code saves pictures into a folder called frames, so make that folder first, in the same place as the file: mkdir frames. The file has 15 real code lines, plus comments.

# Sunflower from the golden angle (our own code, Pillow only). Vogel-style model: dot i sits at radius sqrt(i), angle i * ANGLE.
import math
from PIL import Image, ImageDraw, ImageFilter, ImageChops

def sunflower(angle=137.50776, n=800, size=1080, name="sunflower"):
    dots = Image.new("RGB", (size, size), "black")
    d = ImageDraw.Draw(dots)
    for i in range(1, n + 1):
        r = math.sqrt(i) * size * 0.0175                       # distance grows slowly
        a = math.radians(i * angle)                            # each dot turns by the same angle
        x, y = size / 2 + r * math.cos(a), size / 2 + r * math.sin(a)
        t = i / n
        d.ellipse((x - 9, y - 9, x + 9, y + 9), fill=(255, int(150 + 80 * t), int(40 + 90 * t)))
    glow = ImageChops.add(dots, dots.filter(ImageFilter.GaussianBlur(14)))   # soft glow
    glow.save(f"frames/{name}.png")

for ang in (137.50776, 137.0, 138.0, 90.0):
    sunflower(ang, name=f"sunflower-{ang}")

Run it: python sunflower.py. It saves four pictures in frames, one for each angle in the for line at the bottom: 137.50776, 137.0, 138.0 and 90.0.

How it works, line by line

The experiment: change one number

We drew the same 800 dots four times. Only the angle changed.

Sunflower dot pattern with 137.5 degrees: dots fill the circle evenly
137.5° The head fills evenly, with spirals going both ways.
Sunflower dot pattern with 137 degrees: curved spiral arms with gaps between them
137.0° Just half a degree less, and wide spiral arms with gaps appear.
Sunflower dot pattern with 138 degrees: rays with gaps between them
138.0° Half a degree more, and the dots line up in rays with gaps.
Dots with 90 degrees form a plus sign of four straight lines
90° Every dot lands on one of 4 lines. A plus sign, nothing else.
Our quick test (humara test). We also measured the gaps between the outer dots (800 dots, the outer 200 checked). At 137.5077° the gaps were the most even and the largest (mean 1.751, smallest 1.729). At 137.0° the mean was 1.600 and the smallest 1.519. At 138.0° the mean was 0.994 and the smallest 0.963. These numbers only compare the pictures made by this code. They are our own test, not a published result.

What is 137.5°, really?

The golden angle is about 137.5077640500°. It comes from 360 × (1 − 1/φ), where φ (phi) is the golden ratio, (1 + √5) / 2. "137.5" is a rounded number. We checked this with Python: the arithmetic gives 137.507764.

Careful: we have not shown here that the golden angle is proven to give the best possible packing. We only show what the picture looks like in this model.

Try it yourself

Remember

One small number, the angle, decides the whole picture. Real flowers are more complicated, but this small model shows why 137.5° is special.

Sources

The code and pictures on this page are our own. Next: Neon titli: ek equation →