L-System Simulations

Apr 08, 2010 00:30

I always hesitate posting computer stuff on here because I never know who else besides Angela would care. :P  But!  This is a project I wrote for Animation and Computer Simulations and, even though it needs some work, I'm still pretty proud of it.  :)

There are pictures and a movie at the bottom if you don't want to read "technical" details.  :P

So L-Systems are a variant of your typical computer grammar and they're interesting because you can use them to simulate different natural organisms, most famously plants.  Grammars in computer science are rules you use to form character strings, just like a language grammar states the rules for forming a sentence.

For example, if I want to explain to a computer how a numerical expression like 5 = 4 + 3 should be written, I might use a grammar like

Numerical Expression ->  Number = Number Operator Number
Operator -> +  OR - OR * OR /
Number -> 0 OR 1 OR 2 OR 3 OR 4 OR 5 OR 6 OR 7 OR 8 OR  9

So then, whenever it wants to write a numerical expression, it can look at that set of rules and just fill in the blanks.  Obviously, OR means it will pick one of the options given, not just write down *+/- and think that makes any sense.

In L-Systems, we can do something similar:

F -> F[-F]F[+F][F]

which means every time we see an F in a character string, we replace it with F[-F]F[+F][F]

So, given that rule and a starting string of F and a couple of iterations, we can generate a fairly long and complicated string.

In fact, after only 3 iterations, we get: F[-F]F[+F][F][-F[-F]F[+F][F]]F[-F]F[+F][F][+F[-F]F[+F][F]][F[-F]F[+F][F]]

Now, we can interpret F, +,-, and [, ] as drawing commands.  F means draw forward, +/- means turn left or right and [ ] means whatever changes we make inside the [ ] only affect whatever is within the [ ].  Using these definitions, we can draw pretty pictures :)

For example, the grammar above generates:



But we can also get some more complicated images:



This is one of my favorites.  I think it looks like thistle.  :)



This one is actually quite complicated.  You can see that each stem is really a whole bunch of little twigs



I like that this looks like a branch from a fake pine tree.  Merry Christmas!

But of course, it wouldn't be an animation class without animated floral growth.  This is my first attempt at video capturing on my computer and I'm not sure if I can zoom in or how to zoom in yet.  Hopefully you can still see the little algae plant growing :)

image Click to view


If the youtube embed is awful, my LJ upload of it doesn't look too bad.  For some reason LJ won't let you embed videos from your own scrapbook in your journal entries ....

And that's what I was up to last weekend.  Ta da~
Previous post Next post
Up