A magazine where the digital world meets the real world.
On the web
- Home
- Browse by date
- Browse by topic
- Enter the maze
- Follow our blog
- Follow us on Twitter
- Resources for teachers
- Subscribe
In print
What is cs4fn?
- About us
- Contact us
- Partners
- Privacy and cookies
- Copyright and contributions
- Links to other fun sites
- Complete our questionnaire, give us feedback
Search:
The Fractal Club
OK. So we are going to write a recursive program that generates not just one picture but a whole sequence, each containing a deeper level of self-similarity than the last. It is going to be based on the club fractals in the opening sequence of the film Casino Royale.
You first need to download and run the free GeomLab package from Oxford University (external link). Instructions of how to do that are on the GeomLab site along with worksheets to do other kinds of GeomLab pictures.
Once you've got the GeomLab package up and running, we first need to create the basic picture. When we have that we can move on to creating ever more detailed fractal versions from it.
Leftside Story
The first thing we need to use in GeomLab is what are called Turtle Graphics. They are just a way of drawing where you imagine you have a remote controlled turtle that wanders around leaving a trail behind it (thinking about it "slug graphics" might be a better description!) The turtle (or slug) makes turns in the order you tell it. In GeomLab you use 0 as a command to mean go straight on, 1 to mean turn left and -1 to mean turn right. Here is an example shapes drawn in this way. Cut and paste the instruction into GeomLab and see the shape it makes. (Make sure all the brackets are the right ones if you type it yourself.)
define leftside = draw([0,-1,-1,1,1,-1,1]);
Join our club
We have called this shape leftside - it creates the left half of a club-like symbol. The flip command makes mirror images, so we can use that to make the right-hand side from our turtle graphics shape. Then using the dollar command ($) we can put these two images (which we have called leftside and rightside) side by side to make the club symbol called club.
define rightside = flip(leftside); define club = leftside $ rightside;
So far so good, but there is nothing fractal or recursive about it so far. All we have done is created a base image to work from. In recursive terms, it is called a base case - a trivial case. If our problem is to create a single standard club symbol, then we can just draw it like that, no clever recursive tricks needed.
We now need to create the "step case" of the recursion: we suppose we want to create a fractal image with n fractal buds (n can stand for any number). We assume we have already created a fractal image with one less fractal bud. We need just to work out how we can create a new club around that existing image.
We will need to add a bunch of spacing in to make it work so that the images line up. Getting that right can be the hard part. Here we just give the answer but to work it out involved drawing out the separate squares needed, working out what size the square image scaled down to to fit in the middle and how much space was needed in different places to get the images to line up.
Essentially we are just going to put three images together side-by-side: a left side a recursive version of the club in the middle and a right side of the club. If we try that with our images leftside, club and rightside, it doesn't quite work though. Try it and see (remember $ puts things side by side):
leftside $ club $ rightside
Spaced out
To make the lines connect, it turns out we need not only to add space below the club to push it down, but also to add space at the top and bottom of the side pieces. There is a command blank already defined to make a space. It's not quite the size and shape we want though. That doesn't matter - we can stretch it in the way we need, so it becomes a rectangle twice as long as it is high using the stretch command.
define thinblank = stretch(2,blank);
Then we can add that at the top and bottom of the leftside picture (pictures are put on top of each other using an ampersand symbol rather than a dollar). We can then make a new spaced rightside just by flipping it. Finally we need a tall and thin centre space to push the middle part up. We can make that by rotating (using the rot command) our existing thin blank image.
define spacedleft = thinblank & leftside & thinblank; define spacedright = flip (spacedleft); define centrespace = rot(thinblank);
Smaller, smaller, smaller
Finally, we are ready to do the interesting bit. A fractal club has two parts. To create any single picture we need to give a number to say how deep the budding of smaller and smaller clubs should go. We call that n. For any depth n (greater than 1) we put together a spaced left picture, a fractal picture of a depth one less on top of some space, and a spaced right picture.
A depth of 1 is a special case (the recursive base case we mentioned) so we say how to deal with that separately. In that case we just need to put our complete club symbol on top of some space.
define fractalclub (n) = spacedleft $ (centrespace & fractalclub(n-1)) $ spacedright when n > 1 | fractalclub (1) = thinblank & club;
We create a particular fractal image by saying how deep we want the fractal to go. fractalclub(1) just gives a straightforward club. fractalclub(2) makes one two deep. fractalclub(10) makes one 10 deep (though you need good eyesight to see the smaller buds because they get very small very quickly).
fractalclub(1); fractalclub(2); fractalclub(10);
Here is the full program for generating fractal clubs.
define leftside = draw([0,-1,-1,1,1,-1,1]); define rightside = flip(leftside); define club = leftside $ rightside; define thinblank = stretch(2,blank); define spacedleft = thinblank & leftside & thinblank; define centre = rot(thinblank); define spacedright = flip (spacedleft); define fractalclub (n) = spacedleft $ (centre & fractalclub(n-1) ) $ spacedright when n > 1 | fractalclub (1) = thinblank & club; fractalclub(1); fractalclub(2); fractalclub(10);
Now you've got the basics, try and create your own fractal images. Maybe you can create a fractal club that buds in all directions at once. Just like in the Casino Royale sequence. Come up with something that looks good and you could even enter it in our GeomLab competition.