In this lesson you will learn how to create beautiful fractal curves with Scratch. You need to have some experience with Scratch, as we won't explain the basics here.
A fractal is a geometric pattern that repeats itself at smaller and smaller scales to produce irregular shapes and surfaces that cannot be represented by classical geometry.
Many fractals can be found in nature, for example in the spirals of sea shells, the way ice crystals are formed or the branches of a tree. Even a coastline – zooming in to a Planck length – could be seen as a fractal.
To create fractals in Scratch, we're going to look at creating 2D fractal curves. For example the Koch curve. No matter how much you zoom in, you will keep seeing the same properties:
Before starting to draw fractals, let's first learn how to draw and create functions with Scratch. Create a new project in Scratch. Now add the pen extension blocks:
Now we have extra blocks to draw with. Add some blocks so that we start fresh every time we click the green flag:
Now add the blocks to the script to draw a square with sides of 100 pixels. You may draw a different shape if you like:
It would be nice if we would have a special block to create squares of a certain size. Luckily, Scratch allows us to define blocks ourselve. To do this, go to the red my blocks, and press make a block:
Start the block with a label of draw a square of size, and add a number input with the name size:
Now define the block so it draws a square of the given size. You can drag the size variable from the function definition:
To draw fractals, we can use a function that uses (calls) itself from within its own code. This is called recursion. Let's first create a function that recursively draws smaller rectangles, before moving on to fractal curves.
Recursive functions should always have a base case. At the base case, the function will not use itself anymore. Without a base case, it's very likely that the recursion keeps going on forever: infinite recursion (until you reach stack overflow or are out of memory). This is something you probably don't want.
Let's make a base case and a recursion case to draw a rectangle. Create a new function with two inputs: a size and a depth. Only if the depth is above 1, we call the function recursively with depth minus one and half the size:
If everything went well, you'll probably see something like below. Click here for an example project with the progress so far. It might be fun to experiment a bit with different shapes or different division factors in the recursion case. Might get pretty pretty already.
A Koch curve can be drawn in three simple steps:
In images, that looks like this with three recursion steps:
With all the information you now have about recursion, Scratch and Koch curves, can you figure out how to make a recursive function to draw them? It might be fun to stop reading now fiddle a bit yourself. There are some hints below, and also a possible solution in a later section.
Hints:
A possible solution for the base and recursion case can be like this. You still need to fill in the blanks with a green operator block.
There's a pen block to change the color. Add it at the right place in your script for a rainbow effect:
Koch snowflakes are actually better known than Koch curves. Start with a triangle to make a Koch snowflake, or just repeat your Koch curve three times in a triangle shape.
You can see a solution with this project in Scratch. Here's a pretty image of it, and below the full script:
There are a lot more types of fractals to explore. Some that are easy to make from this point on with Scratch are the Koch antisnowflake and the Cesàro fractal. Wikipedia has a nice section with some different types you can try. If you need help with any of them, ask around.
An incredibly powerful way to create all kinds of fractals is the Lindenmayer system. The representation of a Koch curve in an L-system looks really simple. They are used in many games to create random 3D trees.
There are also fractals generated by a set of complex numbers, of which the Mandelbrot set is the most famous one.
You can also generate fractals in 3D (warning: heavy on CPU and battery).