Let’s start with the basics, if we take the definition from the well-known and beloved Wikipedia, then the L-system (or Lindenmeier system) is a parallel rewriting system and a type of formal grammar.
In simple terms, the L-system consists of an alphabet of characters that can be used to create strings, a set of generating rules that specify substitution rules for each character, the initial string ( “axioms”), with which the construction begins, and the mechanism for translating the formed string into geometric structures. The simplest example of an L-system is the tree construction problem.
Input data:
String (hereinafter referred to as the axiom): A B
Variables (which we can use in building the tree): A B C
Rule (the rule that changes each variable on the next line):
These transformations are obtained:
Generation | Condition |
---|---|
1 | A B |
2 | AB AC |
3 | AB AC AB A |
4 | AB AC AB A AB AC AB |
5 | AB AC AB A AB AC AB AB AC AB A AB AC |
6 | etc… |
The main direction in which L-systemsare used is modeling the growth processes of both living organisms and non-living objects (crystals, mollusk shells or honeycombs).
Example:
To simulate such processes, we will use a programming language such as Python, it has a built-in library “turtle”.
So, let’s get started:
import turtle
turtle.hideturtle()
turtle.tracer(1)
turtle.penup()
turtle.setposition(-300,340)
turtle.pendown()
turtle.pensize(1)
axiom = "F+F+F+F"
tempAx = ""
itr = 3
(itr – values of loop iterations, we will need it in the next step when writing our program)for k in range(itr):
for ch in axiom:
if ch == "+":
tempAx = tempAx + "+"
elif ch == "-":
tempAx = tempAx + "-"
elif ch == "F": #F
tempAx = tempAx + "F+F-f-F+F"
else:
tempAx = tempAx + "f"
axiom = tempAx
tempAx = " "
print(axiom)
If we run through the loop, we will immediately see a filter for the “+”symbol in the first condition:if ch == "+":
tempAx = tempAx + "+"
Here we look for the “+” sign in the axiom (the original line) and when it appears, we add the “+” symbol to the next line. The same thing happens with the “-” and “f ” characters, we just add the “-” and “f” characters to the next line, respectively. But when the symbol “F” appears in our axiom, we will act a little differently and add the sequence of characters “F + F – f – F + F”to the next line , to increase the length of each subsequent line. This action, in principle, is not essential, but for the speed of generating the ” genome” I decided to do just that. At the end, we make sure to equate the axiom with our genome and reset it:axiom = tempAx
tempAx = " "
The result (the value of the axioms):for ch in axiom:
if ch == "+":
turtle.right(45)
turtle.forward(10)
turtle.right(45)
elif ch == "-":
turtle.left(45)
turtle.forward(10)
turtle.left(45)
else:
turtle.forward(20)
You can immediately see that by analogy with the previous paragraph, we detect the characters“+”,” -“, “F” and” f”. Only now at the moment when we encounter the “+”symbol:if ch == "+":
turtle.right(45)
turtle.forward(10)
turtle.right(45)
We turn first to the right, 45 degrees, then drive a distance of 10, and then we turn again to the right, 45 degrees. When do we encounter the “-“symbol?:elif ch == "-":
turtle.left(45)
turtle.forward(10)
turtle.left(45)
We do all the same actions as with the ” + ” symbol, only this time we turn not to the right, but to the left. But if we encounter the characters “F” or “f”, then we will simply drive forward 20 pixels:else:
turtle.forward(20)
As a result, we get such a fractal-a snowflake:turtle.fillcolor("#99BBFF")
turtle.begin_fill()
Where #99BBFF is the color encoding (RGB: 153, 187, 255), and begin_fill() is the start of filling in the color. And at the end, after the loop, add the lines:turtle.end_fill()
turtle.update()
turtle.done()
А end_fill() означает конец заполнения. Далее мы обновляем и выключаем нашу “черепашку”. И на выходе мы получаем вот такой фрактал-снежинку:In the end, I also want to add that I really enjoyed working and writing on this topic, perhaps in the future, I will write a number of articles on the topic of “L-systems”, but in the meantime, I want to present you the results of my poking creativity: