DIY Fractal

Want a more colorful fractal? See my Mandlebrot fractal generator

L-System Fractals

The image at the upper left of this webpage was generated using a very simple program written in MSWLogo.

The fractals that are easily drawn by Logo are L-systems or "Lindenmayer System" fractals. (http://www.cogs.susx.ac.uk/users/gabro/lsys/lsys.html) An L-system is a type of grammar that specifies rules which can be iterated to form self-representive images. Given one rule, an entire fractal can be drawn. Here are some examples below:

Let's start with the c-curve. The basic unit of these fractals is the straight line. The first iteration of the c-curve is a second line perpendicular to the first:

c-curve

The method of drawing this fractal is to first draw a line, make a 90 degree angle, then draw another line. Actually this is incorrect...somewhat. It is difficult to see in this picture. Perhaps it would be clearer this way:

As you can see, each iteration of the fractal replaces all the lines with a 90 degree L-shape.

First a line is drawn. Then two, then four.

The first iteration is the line.
second iteration: make the 90 degree L-shape.
third iteration: make two of the 90 degree L shapes.

You may be wondering how this can create the iterations above. Well, we write the entire bit of code to produce the c-curve just by knowing the first and second iterations, and then we use recursion.

ok, so we know to draw a line and we know then we have to draw two lines, then four...

well, what we can start to see is that each time a line should be drawn, it can be replaced with the two lines at a 90 degree angle...

The pseudocode for the first iteration looks like this:

Now, I said we only needed to know the first and second iterations. Actually the second is the most important. It is the basic structure of the fractal. Let's look at the pseudocode for it:

draw straight line
turn right
draw straight line
turn left

We know each of those "draw straight line" could actually be this shape too, so what if instead of drawing a straight line, we just drew another L-shape? The way to do this is to run this fractal routine recusively. Here's what I'm talking about:

call c_curve
turn right
call c_curve
turn left

The only issue is that we don't actually draw a straight line with this code. So we add a functional argument, :iterations.

call c_curve :iterations-1
turn right
call c_curve :iterations-1
turn left

Now we have to add some condition that when iterations finally gets to 1, we draw a line:

if :iterations = 1 draw straight line
else
call c_curve :iterations-1
turn right
call c_curve :iterations-1
turn left

In the language of Logo, the code looks like this:

to c_curve :length :iterations
ifelse :iterations=1 [forward :length] [
c_curve :length :iterations-1
right 90
c_curve :length :iterations-1
left 90
]
end

at length=3 and iterations=13 we can draw this picture:

c-curve

A variation on this fractal is the dragon curve. The dragon curve appears very complex, however it is based on a simple c-curve. Let's look at the iterations:

dragon curve

Iterations one and two are identical to the c-curve, however we see that the L-shapes alternate inside and outside the lines. Instead of replacing each line with the L-shape, each line is replaced with alternating L-shapes. To achieve this, we must introduce another variable. Here, I used the variable :angle (which I always intially set to 90 when calling) to provide the alternation:

to dragon :length :iterations :angle
ifelse :iterations=1 [forward :length] [
dragon :length :iterations-1 90
right :angle
dragon :length :iterations-1 -90
]
end

dragon curve

The above picture was generated with only fourteen iterations, with a length of 3 units.

This general method of coding fractals in Logo can be applied to many more.

The Koch fractal in Logo looks like:

kock snowflake

The code to generate this is:

to koch :lenght :iterations
ifelse :iterations=1 [forward :length] [
koch :length :iterations-1
left 60
koch :length :iterations-1
right 120
koch :length :iterations-1
left 60
koch :length :iterations-1
[
end

koch line

This image was generated with length of two and iterations = 5. Now, this is interesting, but what if we start with a closed shape instead of a line? We can generate the famous Koch snowflake with these iterations:

koch snowflake explained

The basic first shape is the triangle, which is drawn with a separate function, however for each of the sides, the Koch Snowflake routine calls koch. The resulting code in Logo is this:

to koch_snowflake :length :iterations
left 120
koch :length :iterations
right 120
koch :length :iterations
right 120
koch :lenght :iterations
end

koch snowflake

This image was generated with length 2 and iterations = 5.

Here is a quirky little fractal that I am playing around with now. It is somewhat a variation on the Koch curve in that it's structure is like this:

digital blip

I was experimenting with waveforms at different iterations, and this is the first interesting one I came across. The waveform is digital, and exhibits a spike in the sample. For this reason, I entitled it "digital blip."

The Logo code is:
to digital_blip :length :iterations
ifelse :iterations=1 [forward :length] [
digital_blip :length :iterations-1
left 90
digital_blip :length :iterations-1
digital_blip :length :iterations-1
right 90
digital_blip :length :iterations-1
right 90
digital_blip :lenght :iterations-1
digital_blip :length :iterations-1
left 90
digital_blip :length :iterations-1
]
end

digital blip

The image was created with length of one and only six iterations. It is interesting to note all the circular shapes that were formed from its very square basis. The overall shape almost appears to be a cross, and is quite gothic in appearance. Perhaps this fractal is in need of a better name.

Here is another view of it, repeated four times in the shape of a square:

digital blip square

One more thing: I came across an interesting page about the Sierpinski Triangle and randomness. The site is: http://serendip.brynmawr.edu/complexity/sierpinski.html