Fractals in Logo

Today I was reading a blog post about Logo and I decided to play a little with UCBLogo (Berkeley Logo) and try to draw some fractal (more specifically, L-Systems) with it.

Here is a Koch Curve:

Koch Curve

The code:

to M n
  ifelse notequal? :n 0 [
    M :n - 1
    lt 90
    M :n - 1
    rt 90
    M :n - 1
    rt 90
    M :n - 1
    lt 90
    M :n - 1
  ] [
    fd 10
  ]
end

cs
pu
lt 90
fd 150
rt 180
pd
M 3

And here is a Fractal Plant:

Fractal Plant

The code:

make "stackpos []
make "stackhead []

to store
  push "stackpos pos
  push "stackhead heading
end

to restore
  pu
  setpos pop "stackpos
  setheading pop "stackhead
  pd
end

to F n l
  ifelse notequal? :n 0 [
    F :n - 1 :l
    F :n - 1 :l
  ] [
    fd :l
  ]
end

to X n l
  if notequal? :n 0 [
    F :n :l
    lt 25
    store
    store
    X :n - 1 :l
    restore
    rt 25
    X :n - 1 :l
    restore
    rt 25
    F :n :l
    store
    rt 25
    F :n :l
    X :n - 1 :l
    restore
    lt 25
    X :n - 1 :l
  ]
end

setbackground 2
setpencolor 0

cs
pu
bk 50
lt 90
fd 150
rt 90
pd
rt 45
X 5 2

The code could be a lot better, but that’s what I was able to grasp in about 3 hours. The last time I had really used logo was in primary school, when I was 8 or 9-years old.

I don’t know if it’s a problem with my Fractal Plant function code or if it’s a bug in UCBLogo, but sometimes execution froze completely and I had to hit Alt+S to stop it and call the function again until it worked. I also don’t know if that’s a limitation of UCBLogo or if I just haven’t studied it enough, but I couldn’t find out a way to unset a variable or redefine a function.

Anyways, Logo is absolutely fun to play with. As the author of the previously mentioned post said, it is kind of addicting to think of a set of steps and order the turtle to repeat them a few hundreds of times to see what happens. I consider the language itself a bit confusing at first, but I guess I should read more about it before forming any strong opinions on it.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">