Skip to content

Gosper Curve (Flowsnake)

By the end of this lab you'll be able to:

  • Implement an L-system: a set of string rewriting rules applied repeatedly
  • Translate a string of characters into turtle commands
  • Understand why the Gosper Curve is called a "flowsnake" — it tiles hexagonal space

A beautiful winding curve that fills a hexagonal region — the Gosper Curve, also called the "flowsnake." Built from just two string rewriting rules applied four times.

Welcome to the Gosper Curve!

Monty waving welcome The Gosper Curve is built with an L-system — a string that rewrites itself. It's the same technique used to model real plant growth in computer graphics! Let's code it together!

How It Works

Start with the string 'A'. Apply these rewriting rules repeatedly: - AA-B--B+A++AA+B- - B+A-BB--B-A++A+B

After 4 iterations, translate the result into turtle moves: - A or Bforward(step) - +left(60) - -right(60)

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import turtle
monty = turtle.Turtle()
monty.speed(0)
monty.hideturtle()
monty.pencolor('darkorchid')
monty.pensize(1)

def gosper(n):
    s = 'A'
    for _ in range(n):
        new = ''
        for c in s:
            if c == 'A':
                new += 'A-B--B+A++AA+B-'
            elif c == 'B':
                new += '+A-BB--B-A++A+B'
            else:
                new += c
        s = new
    return s

step = 5
instructions = gosper(4)

monty.penup()
monty.goto(-100, 80)
monty.pendown()

for c in instructions:
    if c in 'AB':
        monty.forward(step)
    elif c == '+':
        monty.left(60)
    elif c == '-':
        monty.right(60)

What Do You Think Will Happen?

Monty thinking The rewriting doubles the string length several times. After 4 iterations, how many characters long will the string be approximately? (Each A and B expands to 15 characters.) Make your guess — then click Run!

Try It Now


  

After 4 iterations the string has thousands of characters — and the curve fills a hexagonal region. Were you right about the count?

How It Works

An L-system (Lindenmayer system) replaces characters in a string using rewriting rules. The string represents a program for the turtle. The key insight: when the replacement rules are designed correctly, the resulting shape tiles space without gaps or overlaps.

The Gosper Curve's hexagonal boundary tiles the plane — seven Gosper curves can be arranged to form a larger Gosper Curve!

Explanation Table

Line What it does
s = 'A' Initial axiom
if c == 'A': new += '...' Apply the A rewriting rule
if c == 'B': new += '...' Apply the B rewriting rule
if c in 'AB': forward(step) Both A and B are "draw forward" commands
+ and - Turn left or right 60°

Learning Check

Your Turn — Try 3 Iterations

Monty thinking Change gosper(4) to gosper(3) and step = 5 to step = 10. The curve will be coarser but faster to draw. Predict how the shape will look simpler.


  

At 3 iterations the hexagonal boundary is clearly visible and the internal winding path has fewer loops.

Experiments

  1. Print the string. Add print(instructions[:50]) before the drawing loop. You'll know it worked when you see the start of the L-system string.

  2. Change the color. Try pencolor('forestgreen'). You'll know it worked when the flowsnake turns green.

  3. Try 5 iterations. Change to gosper(5) with step = 3. More detailed but slower. You'll know it worked when the curve is much denser.

  4. Count the draw steps. Add a counter and print(counter) after the loop. You'll know it worked when you see how many forward steps the turtle takes.

L-Systems Unlocked!

Monty celebrating You mastered L-systems — the same technique used to model real plant growth! Any fractal with a string-rewriting rule can be drawn this way. Up next: Category 5 — Symmetry and Tiling, starting with the Sunflower Seed Spiral.