L-systems and what they allow themselves – InformTFB

L-systems and what they allow themselves

L-systems and what they allow themselves

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):

  • A – > AB
  • B – > AC
  • C – > A

These transformations are obtained:

GenerationCondition
1A B
2AB AC
3AB AC AB A
4AB AC AB A AB AC AB
5AB AC AB A AB AC AB AB AC AB A AB AC
6etc…

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:

  1. Here we import the Turtle library into our project:import turtle
  2. Next, we connect all the necessary configurations for our turtle:turtle.hideturtle()turtle.tracer(1)turtle.penup()turtle.setposition(-300,340)turtle.pendown()turtle.pensize(1)
  3. Next, we specify the value of the axiom itself and the associated parameters necessary for specifying the sequence: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)
  4. In this cycle, where we just need the ITR-variable, we are engaged in processing and “growing” the genome of our fractal/plant directly:for k in range(itr):for ch in axiom:if ch == "+":tempAx = tempAx + "+"elif ch == "-":tempAx = tempAx + "-"elif ch == "F": #FtempAx = tempAx + "F+F-f-F+F"else:tempAx = tempAx + "f"axiom = tempAxtempAx = " "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 = tempAxtempAx = " "The result (the value of the axioms):
  5. Well, it seems that everything is clear on genome generation, let’s start directly with visualization. To do this, we need a simple loop that will translate the symbols found in the genome into the movements of the turtle: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:
  6. If we want to colorize our fractal snowflake, then we will need to add lines before the cycle from the previous paragraph: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:

Anderson
Anderson
Web site editor and tester.

Leave a Reply

Your email address will not be published. Required fields are marked *