Line Fractals

LOGO is particularly suited to the production of line fractals. Fractals are continuous line drawings which have the property of looking similar at any viewing scale. If you magnify a fractal then it just looks the same. They have an infinite level of detail


Midpoint Displacement Fractals.

How to draw an example.

  1. First draw a straight horizontal line segment.
  2. The midpoint of each line segment is pushed upwards, along the y axis, by a random amount that is always less than half of the length of the parent line segment
  3. This operation turns each line segment into two new line segments.
  4. This process is repeated over and over again until the required depth is reached.

Drawing MPTD Fractals with Logo.

Open LOGO and the procedure editor. Copy and paste this code in and then save and exit.

to mptfrac :x1 :y1 :x2 :y2 :level
ifelse :level=0 [
pu setxy :x1 :y1
pd setxy :x2 :y2
stop ] [
localmake "x3 (:x1+:x2)/2
localmake "y3 (:y1+:y2)/2+random(abs int (:x1-:x2)/2)
mptfrac :x1 :y1 :x3 :y3 :level-1
mptfrac :x3 :y3 :x2 :y2 :level-1]
end

Call the procedure with the command line code:

mptfrac -150 0 150 0 5

  1. The start and end points of the line segment are given by (x1,y1) (x2,y2)
  2. If the level is 0 then the code in the first set of square brackets is executed.
  3. This code lifts the pen, sets it down at (x1,y1) and then draws a straight line to (x2,y2).
  4. If the level is greater than 0 then the midpoint (x3,y3) of the line segment is found.
  5. x3 is simply the mean of x1 and x2.
  6. y3 is the mean of y1 and y2 plus a random amount which is always less than the distance between x1 and x2.
  7. To produce different shapes of fractal simply alter the way that the midpoint is displaced.
  8. The procedure is then called recursively with the level reduced by 1 so that the procedure ultimately halts.

This type of fractal is often used to generate virtual landscapes for computer games.
Have a go yourself at forging a fractal landscape.


Levy Fractal

The Levy fractal is an example of a midpoint displacement fractal.
The midpoint of each line segment is pushed outwards, at ninety degrees to the line segment, by a distance equal to half the length of the line segment.
This operation turns each line segment into two new line segments.
This process is repeated over and over again until the required depth is reached.

This program draws the Levy fractal.

to levy :size :level
if :level = 0 [ fd :size stop]
lt 45
levy :size/1.41 :level-1 rt 90
levy :size/1.41 :level-1 lt 90
rt 45
end

Test the program by typing
levy 200 3
into the command line.


The program works by using a very important idea called recursion. The program checks to see if at has reached level 0. If it hasn't then it calls itself, again, but tells itself to make several more, smaller copies. Make sure that you reduce the level by 1 on each recursive call. (What happens if you don't?)

This is the same curve but at much deeper level of recursion.


Koch Fractal

This is an example of a fractal that is generated by taking each line segment of the existing curve and replacing it with a specific set of line segments.
The process is then repeated until the desired depth is reached. Each line segment of the replacement pattern is itself replaced and so on.
Many interesting curves can be generated by this process. All you need to do is alter the code for the collection of lines that are used in the replacement process.

This is the code for the Koch fractal.

to koch :size :level
if :level = 0 [ fd :size stop]
koch :size/3 :level-1 rt 60
koch :size/3 :level-1 lt 120
koch :size/3 :level-1 rt 60
koch :size/3 :level-1
end

You can test this program by typing
koch 200 3

Other Line Fractals

Here are several variations on this theme. All are constructed from similar commands. The main things to do are to think about the angles that you want your curve to turn through and how much it needs to shrink at each iteration. This amount of shrinkage is linked to the concept of "fractal dimension". These fractals have a fractal dimension lying between 1, a straight line, and 2, which is a flat area of the plane.

Last modified 25th January 2010