Settings
Preview
Help
Grammar & Turtle commands
Here you can define the actions associated with a letter that can be used to produce pretty images:
letter: action{paramter}; action{paramter}; ...
Letter is a single character, followed by a colon. The allowed actions are the following:
letter: action{paramter}; action{paramter}; ...
;; A comment line starts with two semicolons. Inline comments are not allowed.
...
draw_forward{length}
: Move the turtle foward while drawing a linejump_forward{length}
: Move the turtle foward without drawing a lineturn_left{angle}
: Turn the turtle angle degrees to the leftturn_right{angle}
: Turn the turtle angle degrees to the rightsave_state
: Save the current state (position & direction)restore_state
: Pop a saved state off the stack and restore itset_direction{angle}
: Set the direction to angleset_position{[x, y]}
: Set the absolute position to (x, y)no_operation
: Do nothingset_color{color}
changes the stroke color to colorset_linewidth{width}
sets the linewidth of the segments.x: set_linewidth{this.iteration}
will increase the line width with every iteration a bit because the final image is often scaled down. Do not forget to addx
to your axiom!dump_state
: dump the current state. This is useful for debugging, so you probably do not need this. To see the dumped state, open the development console (press F12) and navigate to the console tab.
Axiom
The axiom is the starting point. From there the application of replacement rules will hopefully lead you to some amazing fractals!
Replacement Rules
The core of an L system is the set of replacement rules that determine the changes when iterating. A rule consists of two parts:
- A letter to replace.
- A word (sequence of letters) to replace the letter with.
Similar to the syntax you already know from the grammar definition, you can simply define replacement rules like this:
<letter a>: <letter a1><letter 2><letter a3>...
E. g.:
<letter b>: <letter b1><letter b2>...
F: GFLFRRFFLLFRFG
Note that comments are not allowed!
G: GG
Tips & Tricks
To simplify the implementation of this mini programming language, a bad practice is used: the parameters are parsed and executed as JavaScript. That is probably not ideal, but it enables us to do quite fancy stuff:
- We know in which state the turtle is. As you have already seen, we can access the current iteration number with
this.iteration
. - We can use math:
F: draw_forward{Math.sqrt(1764)}
- We can introduce randomness: Just use
Math.rand(min, max)
andMath.randNorm(avg, std)
, e. g.:F: draw_forward{Math.randNorm(50, 10)}
The first macro draws a line with a length that is normally distributed around 50 with a standard deviation of 10. The second macro chooses a random direction.
G: set_direction{Math.rand(0, 2 * Math.PI)}