Using Lindenmayer systems and Python turtle graphics to create art.
Lindenmayer systems (L-systems) are a string-rewriting process. They use a set of rules to recursively modify a string of symbols, transforming the initial string into something more complex. They are often applied in computer graphics, where they can be used to generate models of fractals and plants.
The simplest type of L-system is a DOL-system. They are deterministic (there is exactly one way to transform each letter) and context-free (symbols from the preceding string are rewritten regardless of their position).
To better understand this system, consider this string of characters (symbols):
AB+A
This initial string is known as the axiom, where each letter corresponds to a rewriting rule. In this case, let the rules be:
A → A-B
B → AB
After three iterations of this system, AB+A would be rewritten as the following:
Iteration | String |
---|---|
1 | A-BAB+A-B |
2 | A-B-ABA-BAB+A-B-AB |
3 | A-B-AB-A-BABA-B-ABA-BAB+A-B-AB-A-BAB |
By defining each symbol with an action, L-systems can be graphed. In particular, this project uses Python turtle graphics to draw two DOL-systems. Both use symbols with the following geometric interpretations:
Symbol | Command |
---|---|
F | Move forward by drawing a line of a specified length |
A-Z (excluding F) | Do nothing |
+ | Turn right by a specified degree |
- | Turn left by a specified degree |
[ | Push the current position and direction of the pen/turtle onto a stack |
] | Pop the previous position and direction of the pen/turtle from a stack |
If Python has not been previously installed, run the following:
$ sudo apt install python3.9
$ python3.9 --version
If Homebrew has not been previously installed, follow the instructions listed here.
If Python has not been previously installed, run the following:
$ brew install python@3.9
$ python3.9 --version
Clone this repository:
$ git clone https://github.com/miaisakovic/l-system-drawings.git
When asked to enter credentials, input your username and personal access token.
To view the Sierpiński arrowhead curve, run the following command:
$ python3.9 <relative path to sierpinski_arrowhead_curve.py>
To view a drawing generated by an L-system (which was provided by LSystemBot 2.0), run the following command:
$ python3.9 <relative path to l_system.py>