- Notifications
You must be signed in to change notification settings - Fork1
A Python implementation of some L-systems described in Chapter 1 of the book «The Algorithmic Beauty of Plants»
License
6rampus/lsystems
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A Python implementation of some L-systems described in Chapter 1 of the bookThe Algorithmic Beauty of Plants.
Koch construction | Plant-like structure |
---|---|
![]() | ![]() |
An L-system is a type of formal grammar that can be defined as a tupleG = ⟨V, ω, P⟩ where
- V is thealphabet of the system
- ω is a nonempty word called theaxiom
- P is a finiteset of productions
Aproduction is a rewrite rulea→X (indicating thata, the predecessor, can be replaced byX, the successor)
As it is mentioned inthe book, the central concept of L-systems is that of rewriting. The initial state of the system is defined by the axiom, a string containing symbols from the alphabet that may or may not be replaceable (variables vs. constants or terminals). From this initial state/string, the rewrite rules or productions are applied iteratively (find symbol as a predecessor in the set of productions, replace in the string by its successor, repeat).
In L-systems, productions are applied in parallel. In other words, each iteration applies as many rewriting rules as possible, which results in all replaceable symbols in the string being replaced in the same iteration. This is the essential difference between L-systems and formal languages generated by formal grammars, which apply only one rule per iteration.
Python 3 is the only requirement. You can clone the repo and try out some examples that I took fromthe book like this:
python3 lsystems.py examples/fig-1.24/a.txt
or you can write your own L-systems in a text file with the input format specified below.
This implementation uses the turtle interpretation of strings described inthe book, so many of the symbols you can find in it have the same meaning here (with a few exceptions).
Symbol | Command |
---|---|
F | Move forward a step of lengthd |
f | Move forward a step of lengthd without leaving a trail |
+ | Turn left by angleδ |
- | Turn right by angleδ |
l | Fl in the book. Move forward |
r | Fr in the book. Move forward |
L | Ignore, do nothing |
R | Ignore, do nothing |
[ | Push the current state of the turtle onto a stack |
] | Pop a state from the stack and make it the current state of the turtle |
A step size or distanced and an angle incrementδ are given as parameters
The input should be a.txt
file containing just one parameter per line, in this order: number of iterations, step distance, angle increment, axiom, set of productions (one production per line).
number of iterationsstep distanceangle incrementaxiomproduction #1production #2...production #N
For example:
2490F+F+F+FF->F+f-FF+F+FF+Ff+FF-f+FF-F-FF-Ff-FFFf->ffffff
Every execution saves the result in a PostScript file in the folder that contains the input file.
- Loading L-system parameters (number of iterations, axiom, productions, etc.) from file
- Loading turtle and screen parameters (trail and background color, starting orientation, etc.) from file
- 2D turtle interpretation of...
- deterministic and context-free L-systems (DOL-systems)
- bracketed OL-systems for branching structures
- stochastic L-systems
- context-sensitive L-systems
- parametric L-systems
- Modeling in three dimensions
- Saving results in a PostScript file