How to Make a Tree With Fractals

A physicist dilates on the delights of nature’s intricate geometry.
bare tree limbs on black background
Photograph: Flavio Edreira/Getty Images

Warning: If you start playing with fractals, it can get addicting. I don't know why, but it's stupid fun to make these things. Actually, I first started looking at fractals as part of the #TeamTrees challenge—a project to raise enough money by 2020 to plant 20 million trees. (We made our goal!)

What do fractals have to do with trees? Hold on, one step at a time. I'm going to show you how to create a tree using a fractal, but first let’s take a little detour through the concept of recursion.

What is recursion? Let me explain by example: Suppose you wanted to find the prime factors of a number n. Say n = 12. Well, you can do that in your head. 2 x 2 x 3 = 12, and those are all prime numbers. But what if n = 1,234,533? For that, you need a method. Here’s a recipe for generating the prime factors of any number:

  • Divide n by 2: If there is no remainder, write down 2 as a factor
    • Reset n = the result of the division (n/2)
  • Repeat the step above on the new number
    • Continue until division by 2 yields a remainder
    • Now increase the divisor by 1 (so, 2 + 1 = 3)
  • Divide the new number by 3: If there is no remainder, write down 3 as a factor ...

And so on. Keep cycling through this basic routine, adjusting n at each step and ratcheting up the divisor, until the result of the division is less than 2—then you’re done. So let's put this algorithm into a Python script:

Click the Play button to run it. Huh! Turns out the prime factors of 1,234,533 are 3, 79, and 5,209. I just picked that number at random, so it’s cool that it has such a high prime factor. You can also run the script on your own crazy numbers; just click the pencil icon and type something into line 15.

Anway, the thing to notice here is how it works: I created a function called “pfact” to perform the operations, and the definition of the function actually calls itself (in line 11 ). That’s recursion. By creating that nested structure, we can do elaborate calculations using very little code. It's sort of amazing that this works.

Recursion in the Wild

So what about fractals? Fractals are patterns that have the same appearance at different scales. You find them everywhere in nature. Look at a coastline: It’s full of bays and rivers and peninsulas. If you zoom in on a small section, is it smoother? Nope, up close you see the same jagged shapes at smaller scale.

This self-similarity can also be seen in trees. If you start from the trunk and move up, it breaks into several parts—we call these branches. If you follow one of these branches, it too splits in a way that is similar to the previous branch. Each branch of the tree is itself a smaller tree shape. So a tree is like a fractal. That means we can model a tree using fractals.

I think we’re ready for a tree fractal. I'm going to show you how to make this fractal with GlowScript Python. Of course there are other options. Perhaps you prefer to do it with Python and Turtle? Here's a nice tutorial for that.

Here is the basic plan for this tree fractal:

  • Start at some point and move a certain distance in a certain direction.
  • At that point, make a branch. Turn some angle to the right and then repeat the previous step with a shorter distance. (Recursion!)
  • Now go back and turn left to make the other branch. (Recursion again.)

You probably won't really understand this code until you break it. So here you go—this is my first tree fractal. Run this, and then change some stuff. If you click the pencil icon, you can see the code and edit it.

A quick note about vectors and cylinders. Since each branch is a cylinder in three dimensions, there are some parts of the code that might be confusing. When you make a cylinder in GlowScript Python, you need two things: a starting position (a vector in 3D space) and another vector that points from the start to the end of the cylinder. In the code, this pointing vector is the variable a—it's this vector that gets rotated in each branching.

Branching Out

Enough about vectors—let's make some cool stuff! What if I want to make my tree more tree-like? Here are some things I can change:

  • Instead of shortening successive branches by a given length, I can shorten them proportionally—say by 25 percent at each iteration.
  • I can make the thickness of branches proportional to their length.
  • I can make the smallest parts green and the larger parts brown.

Here's what my new tree looks like. Oh, and here's the code.

Illustration: Rhett Allain

But wait! That tree is in just two dimensions. What about a three-dimensional tree? What about a tree with some randomness thrown in? Yes, both of those things would make better looking (more realistic) trees—but I'm going to leave that to you as a homework assignment.

Are fractal trees just for fun? Well, if they were, so what? Fun is good. That's probably how the first fractal was made anyway. But they actually have real uses. One is in digital animation. Suppose you’re doing a forest scene in a CGI movie. Do you (a) draw every tree individually? (b) draw one tree and reproduce it? or (c) use a computer to generate fractal trees? Fractals!

There's another super-awesome use—estimating the amount of carbon dioxide that trees absorb. The basic idea is to measure the branching patterns of a particular tree (the distance between branches and the changes in width). From this, you get the “rules” for the tree structure rather than measuring the whole tree. Once you know how the tree could be built with these fractal branching rules, that will give you the properties of the entire tree. I think that's pretty cool.


More Great WIRED Stories