Dragon Curves
By Michael Doornbos
- 6 minutes read - 1112 wordsIntroduction
While you may be familiar with Jurassic Park as a movie, the book offers a unique and different experience that’s worth exploring.
No, this isn’t one of those “we’re better than you because we read the book.”
BUT, the book is much different from the movie. There’s some depth to the characters and the story that the movie doesn’t have time to explore, and there are some interesting ideas that the movie doesn’t have time to explore.
For example, the book is divided into seven iterations.
The book is also full of mathematical concepts, like chaos theory, fractals, and self-similarity.
And it’s these fractal patterns we’ll look at today. They happen to be Dragon curves, named for their scale-like patterns. Fitting for a dinosaur book, right?
Folding paper
Dragon curves are a type of fractal that can be created using recursive algorithms. The concept of dragon curves has been used in various fields, from computer graphics to mathematics, and they are a good example of how simple rules can create complex patterns.
You can make them by folding a strip of paper in half repeatedly, then unfolding it to reveal a pattern that resembles the scales of a dragon.
This process can be simulated using code, allowing us to create intricate fractals on a computer screen.
Commodore 64 Logo
There are many ways to do super high resolution fractals these days. But they lack something that the drawing methods like LOGO give you.
I’m sure you’re not surprised that I’m going to show you how to draw dragon curves with Logo on the Commodore 64.
TO DRAGON :LEN :DEP :SGN
IF :DEP = 0 [FD :LEN STOP]
RT 45 * :SGN
DRAGON :LEN / 1.414 :DEP - 1 1
LT 90 * :SGN
DRAGON :LEN / 1.414 :DEP - 1 -1
RT 45 * :SGN
END
TO SETUP
CLEARSCREEN
PENUP
SETPOS [-100 0]
PENDOWN
END
TO DRAWDRAGON :DEP
SETUP
DRAGON 180 :DEP 1
END
Here’s how this code relates to the folding process:
-
Each recursive call to DRAGON represents one “fold” in our paper.
-
The direction of each fold is represented by the turns:
RT 45 * :SGN
at the start represents the first half of a fold.LT 90 * :SGN
in the middle represents the “crease” of the fold.RT 45 * :SGN
at the end completes the fold.
-
The two recursive calls (
DRAGON ... 1
andDRAGON ... (-1)
) represent the two halves of the folded paper. The different signs (1 and -1) ensure that these halves are mirror images of each other, just like the two sides of a folded paper. -
The reduction in length (
:LEN / 1.414
) for each recursive call represents how each fold creates a pattern half the size of the previous one. (1.414
is roughly the square root of 2) -
The
:DEP
parameter keeps track of how many “folds” we’ve made. When it reaches 0, we just draw a straight line (FD :LEN
), representing the smallest segment of our fully unfolded paper.
Now, let’s relate this to the folding process:
-
First fold: This is represented by the first level of recursion. The code makes a right turn, draws the first half, makes a left turn, and draws the second half.
-
Subsequent folds: Each deeper level of recursion represents another fold of the paper. The pattern gets more complex as we go deeper, just like how the paper gets more intricately folded.
-
Unfolding with right angles: In the paper analogy, we unfold everything to 90° angles. In the code, this is represented by the
LT 90 * :SGN
turn between the two recursive calls. -
Alternating directions: In paper folding, each crease alternates direction when unfolded. In the code, this is achieved by alternating the sign (1 and -1) in the recursive calls.
-
Self-similarity: Just as each section of the folded paper contains a smaller version of the whole pattern, each recursive call in the code creates a smaller version of the entire curve.
The key insight is that the code is not directly simulating the folding process. Instead, it’s drawing the final result as if we had folded the paper many times and then unfolded it all at once with right angles. Each recursive call is essentially “zooming in” on a smaller part of the folded paper, drawing it in more detail.
This approach allows the computer to efficiently draw a complex pattern that would be physically impossible to create by actually folding paper, giving us a beautiful visualization of this mathematical concept.
UCBLogo
If you want it to work on a modern Logo interpreter, you’ll need to make some minor modifications.
Here’s how you can adapt the code for UCBLogo:
to dragon_curve :length :depth :sign
if :depth = 0 [
forward :length
stop
]
right 45 * :sign
dragon_curve :length / 1.414 :depth - 1 1
left 90 * :sign
dragon_curve :length / 1.414 :depth - 1 -1
right 45 * :sign
end
to setup
clearscreen
penup
setxy -200 0
pendown
end
to draw_dragon_curve :depth
setup
dragon_curve 400 :depth 1
end
; Example usage:
draw_dragon_curve 15
UCBLogo is very fast on my M1 Mac, so you can draw some pretty high resolution dragon curves with it. I set it to 15 here.
Python Turtle
This code can also be adapted to Python’s Turtle graphics module to draw the dragon curve on the screen.
import turtle
def dragon_curve(t, length, depth, sign=1):
if depth == 0:
t.forward(length)
else:
t.right(45 * sign)
dragon_curve(t, length / 1.414, depth - 1, 1)
t.left(90 * sign)
dragon_curve(t, length / 1.414, depth - 1, -1)
t.right(45 * sign)
# Set up
screen = turtle.Screen()
t = turtle.Turtle()
t.speed(0)
t.penup()
t.goto(-200, 0)
t.pendown()
# Set depth of recursion
depth = 10
# Draw
dragon_curve(t, 400, depth)
# Finish
screen.exitonclick()
It’s not as fast the the UCBLogo version, but it’s still pretty cool.
Extra Credit
Dragon curves are a beautiful example of how simple rules can create complex patterns. By using recursive algorithms, we can create intricate fractals that resemble the scales of a dragon. Whether you’re folding paper or writing code, the process of creating a dragon curve is a fascinating exploration of self-similarity and recursion. By drawing dragon curves with Logo on the Commodore 64, we can appreciate the beauty of these fractals and the power of simple algorithms to create stunning visualizations.
What happens if you change the angle of the turns in the code? How does it affect the resulting pattern?
What other fractals can you create using similar recursive algorithms?
If you read the book, how do you think the dragon curves relate to the themes of Jurassic Park? Is Malcom right about chaos theory?
Happy coding!
- Code
- Getting Started
- How-To
- Retro
- Programming
- Tutorial
- Vintage
- Commodore
- Logo
- Turtle
- Fractals
- Dragon
- Jurassic Park