Back to Index
L-Systems
Lindenmayer systems (L-systems) are a way of iterating on a grammar with an initial state and set of rules. The way that I have implemented L-systems are a little bit different than any others that I've seen, and it may be disparate enough that it isn't an L-system at all anymore, but I think it's a more explicit way to express the grammar.
My implementation of the L-system is made up of the following values:
-
Axiom: The initial state of the system.
-
Rules: An object in JSON form that represents the replacement rules for each of the symbols.
-
Terminal rules: An object in JSON form that represents the graphical behavior that happens when the number of iterations is reached. Available rules are:
- MOV (NUM): Draws from the current line on the current angle (NUM) points. e.g. MOV 1
- PUT (NUM): Moves the current line on the current angle (NUM) points without drawing a line. e.g. PUT 1
- ROT (NUM): Rotates the current heading by (NUM) degrees. e.g. ROT 90
- MULT (NUM): Multiplies future movements by (NUM). e.g. MULT 2.5
- PUSH: Pushes the current state (position, angle, color) to the stack.
- POP: Pops the last stored state from the stack.
- COL (COLOR): Sets the color for future strokes to (COLOR), where (COLOR) is a hex color. e.g. COL #FF0000 for a red line.
-
Iterations: The number of times to expand the axiom using the rules.
Link to this render
Final string:
Suggested settings:
#axiom=IF-G-G&rules={"F": "F-G+F+G-F", "G": "GG"}&terminal_rules={"F": "MOV 1", "G": "MOV 1", "+": "ROT -120", "-": "ROT 120", "I": "ROT -90"}&iterations=3
#axiom=FX&rules={"X": "X+YF+", "Y": "-FX-Y"}&terminal_rules={"F": "MOV 1", "-": "ROT 90", "+": "ROT -90"}&iterations=9
#axiom=0&rules={"1": "11", "0": "1[0]0"}&terminal_rules={"1": "MOV 1", "0": "MOV 1", "[": "PUSH,ROT -45", "]": "POP,ROT 45"}&iterations=5
#axiom=F--F--F&rules={"F": "F+F--F+F"}&terminal_rules={"F": "MOV 1", "-": "ROT -60", "+": "ROT 60"}&iterations=3
#axiom=IF&rules={"F": "F+F--F+F"}&terminal_rules={"F": "MOV 1", "-": "ROT -85", "+": "ROT 85", "I": "ROT -90"}&iterations=5
#axiom=IF&rules={"F": "+F--F+"}&terminal_rules={"F": "MOV 1", "+": "ROT 45", "-": "ROT -45", "I": "ROT 90"}&iterations=10
#axiom=W&rules={"W": "+++X--F--ZFX+", "X": "---W++F++YFW-", "Y": "+ZFX--F--Z+++", "Z": "-YFW++F++Y---"}&terminal_rules={"F": "MOV 1", "+": "ROT 30", "-": "ROT -30", "[": "PUSH", "]": "POP"}&iterations=5
#axiom=A&rules={"A": "A-B--B+A++AA+B-", "B": "+A-BB--B-A++A+B"}&terminal_rules={"A": "MOV 1", "B": "MOV 1", "+": "ROT 60", "-": "ROT -60"}&iterations=3
#axiom=F+F+F+F&rules={"F": "F+f-FF+F+FF+Ff+FF-f+FF-F-FF-Ff-FFF", "f": "ffffff"}&terminal_rules={"F": "MOV 1", "f": "PUT 1", "-":"ROT 90", "+": "ROT -90"}&iterations=3
#axiom=RF&rules={"F": "FF[[[+F]F]-F]"}&terminal_rules={"[": "PUSH", "]": "POP", "F": "MOV 1", "M": "MOV 1", "-": "ROT 90", "+": "ROT -90", "R": "ROT -45"}&iterations=3
#axiom=X&rules={"X": "F[-X][X]F[-X]+FX", "F": "FF"}&terminal_rules={"F": "MOV 1", "-": "ROT 25", "+": "ROT -25", "[": "PUSH", "]": "POP"}&iterations=5
#axiom=F&rules={"F": "1FF-[2-F+F+F]+[3+F-F-F]"}&terminal_rules={"F": "MOV 1", "-":"ROT 22.5", "+": "ROT -22.5", "[": "PUSH", "]": "POP", "1": "COL #000000", "2": "COL #00FF00", "3": "COL #77FF77"}&iterations=3
#axiom=F&rules={"F": "F[+F]F[-F][F]"}&terminal_rules={"F": "MOV 1", "-":"ROT 20", "+": "ROT -20", "[": "PUSH", "]": "POP"}&iterations=3
#axiom=F&rules={"F":"F-[F]-[F]-[F]"}&terminal_rules={"F":"MOV 1", "[":"PUSH", "]": "POP", "-": "ROT 120", "+": "ROT -120"}&iterations=3
#axiom=FX&rules={"X":"M[-FY]+FX", "Y":"FX+FY-FX"}&terminal_rules={"F":"MOV 1", "[":"PUSH", "]": "POP", "-": "ROT 30", "+": "ROT -30", "M": "MULT .6"}&iterations=3
#axiom=IX&rules={"X": "+FF-YFF+FF--FFFTXTF--YFFFYFFFT", "Y": "-FF+XFF-FF++FFFTYTF++XFFFXFFFT", "F": "GG", "G": "GG"}&terminal_rules={"F": "MOV 1", "G": "PUT 1", "-": "ROT -60", "+": "ROT 60", "I": "ROT -90", "T": "ROT -180"}&iterations=3
#axiom=[X]++[X]++[X]++[X]++[X]&rules={"W":"YF++ZF----XF[-YF----WF]++","X":"+YF--ZF[---WF--XF]+","Y":"-WF++XF[+++YF++ZF]-","Z":"--YF++++WF[+ZF++++XF]--XF","F":""}&terminal_rules={"F": "MOV 1", "-": "ROT -36", "+": "ROT 36", "[": "PUSH", "]":"POP"}&iterations=2