L-System

#1

I created a simple L-System for use with openFrameworks. After instantiating you can add variables, constants, rules and set the starting-state. You can then iterate with a call to update(). It also supports the use of brackets in the rules for saving and recalling values.

I’ve also included a simple turtle-graphics class for quickly viewing results. It’s very easy to customize the drawing code. I just started using git so you can grab-it-here.

L-sytem example
#2

Nice ! thanks for sharing !

#3

thats a good resource to have, thx for posting.
i was trying to draw a Algea L-system but i don’t think the Turtle class supports that format.
looks like Turtle can only draw things with ± contstants, is that right?

#4

Not exactly, you can pass in the strings for ‘draw forward’, ‘turn left’ and ‘turn right’ in the constructor. This, and the right brackets to ‘pushPosition’ en ‘popPosition’ are the only commands that this Turtle can take. It’s pretty limited but then again it was only included for checking if the L-system worked. It should be a pretty straightforward process to add more commands.

I looked for a nice algae L-system, found this one by Paul Bourke: http://paulbourke.net/fractals/lsys-algae-b/

Implementation:

  
system.addVariable("a");  
	system.addVariable("b");  
	system.addVariable("c");  
	system.addVariable("d");  
	system.addVariable("e");  
	system.addVariable("g");  
	system.addVariable("h");  
	system.addVariable("i");  
	system.addVariable("j");  
	system.addVariable("k");  
	system.addVariable("l");  
	system.addVariable("m");  
	system.addVariable("n");  
	system.addVariable("o");  
	system.addVariable("p");  
	system.addVariable("q");  
	system.addVariable("r");  
	system.addVariable("s");  
	system.addVariable("t");  
	system.addVariable("u");  
	system.addVariable("v");  
	system.addVariable("w");  
	system.addVariable("x");  
	system.addVariable("y");  
	system.printVariables();  
  
	system.addConstant("F");  
	system.printConstants();  
	  
	system.setStart("aF");  
	system.printStart();  
	  
	system.addRule(LRule("a", "FFFFFy[++++n][----t]fb"));  
	system.addRule(LRule("b", "+FFFFFy[++++n][----t]fc"));  
	system.addRule(LRule("c", "FFFFFy[++++n][----t]fd"));  
	system.addRule(LRule("d", "-FFFFFy[++++n][----t]fe"));  
	system.addRule(LRule("e", "FFFFFy[++++n][----t]fg"));  
	system.addRule(LRule("g", "FFFFFy[+++fa]fh"));  
	system.addRule(LRule("h", "FFFFFy[++++n][----t]fi"));  
	system.addRule(LRule("i", "+FFFFFy[++++n][----t]fj"));  
	system.addRule(LRule("j", "FFFFFy[++++n][----t]fk"));  
	system.addRule(LRule("k", "-FFFFFy[++++n][----t]fl"));  
	system.addRule(LRule("l", "FFFFFy[++++n][----t]fm"));  
	system.addRule(LRule("m", "FFFFFy[---fa]fa"));  
	system.addRule(LRule("n", "ofFFF"));  
	system.addRule(LRule("o", "fFFFp"));  
	system.addRule(LRule("p", "fFFF[-s]q"));  
	system.addRule(LRule("q", "fFFF[-s]r"));  
	system.addRule(LRule("r", "fFFF[-s]"));  
	system.addRule(LRule("s", "fFfF"));  
	system.addRule(LRule("t", "ufFFF"));  
	system.addRule(LRule("u", "fFFFv"));  
	system.addRule(LRule("v", "fFFF[+s]w"));  
	system.addRule(LRule("w", "fFFF[+s]x"));  
	system.addRule(LRule("x", "fFFF[+s]"));  
	system.addRule(LRule("y", "Fy"));  
	system.printRules();  
	  
	res = system.getLevel(30);  
	cout << res << endl;  
	  
	turtle = Turtle("F", "-", "+");  
	turtle.length = 1;  
	turtle.angle = 12;  
  

I don’t know what ‘f’ is supposed to do, as it’s not included in one of the rules I guess it’s a turtle-command. Anyone know what it’s supposed to do?

Without adding the ‘f’ command, the algae look like the attached image.

#5

cool, very interesting.

#6

I really liked this one. Thanks for sharing

#7

this is GREAT

#8

just wanted to say that this is awesome. looking forward to seeing what people do with this…

#9

#10

#11

I have started working with a white background, I feel it really makes openframeworks “pop”

#12

but nothing seems to be the alpha blended smoothed 1px lined black background goodness

#13

Great stuff! Maybe try an additive blend on that last one, make it glow :wink: I think it would be nice to do some animations with the tree-like structures, I’ve been wanting to grow beautiful trees for a while now. Maybe map the loudness of audio to the speed of growing, hmmm… :slight_smile:

#14

What is the limit on the length of turtle instructions? I imagine my segfaults are due to the limitation on string length

#15

cplusplus.com

size_t max_size ( ) const;
Return maximum size of string

Returns the maximum number of characters that the string object can hold.

Calling string.max_size() returns the result of 1073741820. Due to the recursive nature of the system this limit is easy to reach.

#16

getting vertex buffer objects to render these guys out

#17

;D

#18

thx,daanvanhasselt

How can I make the growth process?
Such as trees or grass.

#19

Awesome, I was just getting into L-Systems last week, to then find this. Many thanks!

#20

Ok, maybe this thread is quite old but I have been experimenting with these l-system classes for a while, and now I think it’s time to contribute :slight_smile:

I have rewritten the turtle class to support 3d drawing, coloring and growing of branches (because I’ve been experimenting mainly with generating plant like stuff).

Of course this turtle can still render 2d and non-growing images.

I also tried to optimize the parsing of the input string from the l-system.

Since I am too lazy to write a documentation I have attached a zip with the commented source

Some screenshots:

You can use this stuff any way you want just tell me if you found any bugs or if you have any ideas for improvements

post scriptum:

how can I get vertex buffer objects to render? I’m quite new to opengl but the effect in memphistechno’s post looks good

3dturtle.zip