Lindenmayer power


Output


Grammar input


Commands input


levels
starting from X: Y: and at an initial angle of

This is a plant/fractal generator that uses L-systems (L-system on Wikipedia)
Check out the new 3D version

How to use it:
First you need to write the production rules for your grammar (note that this application accepts only stocastic context-free grammars)
Below is a sample grammar:

-> Start
0.2 Number
0.4 Start + Start
0.3 Start - Start
0.1 ( Start )
-> Number
0.1 0
0.1 1
0.1 2
0.7 3

-> Start
//the first nonterminal symbol
//all nonterminal symbols are defined by -> Nonterminal

0.2 Number
0.4 Start + Start
0.3 Start - Start
0.1 ( Start )
//Start may rewrite itself as "Number", "Start + Start", "Start - Start" or "( Start )"
//thus we may have the following rewriting sequence: Start --> Start + Start --> Number + ( Start ) --> 3 + ( Start - Start )
//notice that EVERY nonterminal in a phrase MUST be rewritten
//notice that every line starts with a value - that value represents the chance of this rewriting rule to be chosen (their sum doesn't need to be 1 (the application normalizes these values))

-> Number
//here we tell the system to change that all its focus to the nonterminal "Number"
//and Number can be rewritten as 0, 1, 2, or 3 as these are the only known numbers in the universe

Turtle Graphics:
Now, if you want to draw preety plants and other stuff using this you need to know some Turtle Graphics (see Wikipedia)
It's simple, fun and powerful.
Imagine you're a pen on a piece of paper and you can walk Forward turn Left or Right.
You could walk straight ahead to draw a line. If you repeatedly walk 2 paces then slightly turn left you eventually come back to where you started, thus drawing a circle. Or you could follow a weird pattern of Forward, Left, Right commands and end up drawing fractal-like shapes.

A sequence of drawing commands can be generated using the above phrase generator.

Ex:
This is the grammar. It has only one nonterminal, namely "U". This goes in the "Grammar input" textarea
-> U
f r f l f l f r U
These are the turtle commands associated with symbols (place them in the "Commands" textarea)
: f
forward 10
: r
rotate 90
: l
rotate -90

forward 10 translates to "walk forward 10 units"
rotate 90 translates to "rotate clockwise 90 degrees"
rotate -90 translates to "rotate anticlockwise 90 degrees"

Complete list of Turtle Commands:
forward var/valuewalk forward
rotate var/valuerotate
pen true/falseactivate or deactivate drawing
pushpush the current state to the stack
poppop a state from the stack
color red green bluesets the color to rgb(red,green,blue); red, green and blue take values between 0 and 255
width var/valueset the width of the pen
text var/valueprint the variable or value
square var/valuedraw a square
var := other_var/valueassignement operation
var += other_var/valueaddition operation
var -= other_var/valuesubtraction operation
var *= other_var/valuemultiplication operation
* Note: variables that start with a capital letter are static;
variables that start with a lower case leter are part of the current state

More examples:

Koch snowflake

-> S
1 A + A + A
-> A
1 A - A + A - A
: A
forward 5
: +
rotate 120
: -
rotate -60
Koch curve

-> F
1 F + F - F - F + F
: F
forward 10
: +
rotate 90
: -
rotate -90
Sierpinski triangle

-> A
1 B - A - B
-> B
1 A + B + A
: A
forward 10
: B
forward 10
: +
rotate 60
: -
rotate -60
Binary fractal tree

-> S
i F
-> F
f s [ - F ] [ + F ] u
i a:=100
s a*=0.707106781
u a*=1.41421356
f F:a
+ R:90
- R:-90
[ +
] -
Fractal plant

-> X
1 F - [ [ X ] + X ] + F [ + F X ] - X
-> F
1 F F
: F
forward 10
: +
rotate 25
: -
rotate -25
: [
push
: ]
pop
Koch Curve

-> F
1 F - F + F + F F - F - F + F
: F
forward 5
: +
rotate 90
: -
rotate -90
Thread plant

-> B
1 T B
1 P [ r B ] [ q B ]
-> P
1 T P
1 T
1 T T
1 T T T
-> T
1 f r f r f r f r
1 f q f q f q f q
1 f u f u f u f u
1 f t f t f t f t
: f
forward 7
: r
rotate 10
: q
rotate -10
: t
rotate 13
: [
push
: ]
pop
: u
rotate -13
Broccoli

-> S
1 i F
-> F
1 f [ - F ] [ + F ]
: i
a := 100
w := 12
width w
: f
forward a
: +
rotate 45
: -
rotate -60
: [
push
a *= 0.707106781
w *= 0.7
width w
: ]
pop

No comments:

Post a Comment

Post a Comment