L-systems and what they allow themselves

Let's start with the basics, if we take a definition from the well-known and beloved Wikipedia, then the L-system (or Lindenmayer's system ) is a parallel rewriting system and a form 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 generative rules that specify substitution rules for each character, an initial string ( "axiom" ), with which the construction begins, and a translation mechanism formed by lines in geometric structures. The simplest example of an L-system is the tree construction problem.





Input data:





String ( hereinafter Axiom ): AB





Variables ( which we can use in building the tree ): ABC





Rule (the rule by which each variable on the next line changes ):





  • A -> AB





  • B -> AC





  • C -> A





The following transformations are obtained:                                        





Generation





condition





one





AB





2





AB AC





3





AB AC AB A





4





AB AC AB A AB AC AB





five





AB AC AB A AB AC AB AB AC AB A AB AC





6





etc…





The main direction in which L-systems are used is modeling the growth processes of both living organisms and inanimate objects (crystals, mollusk shells or honeycombs) .





Example:





To simulate such processes, we will use a programming language such as Python, which has a built-in "turtle" library .





So let's get started:





  1. Here we are importing the Turtle library into our project:





    import turtle







  2. Next, we include all the necessary configurations for our turtle :





    turtle.hideturtle()







    turtle.tracer(1)







    turtle.penup()







    turtle.setposition(-300,340)







    turtle.pendown()







    turtle.pensize(1)







  3. , :





    axiom = "F+F+F+F"







    tempAx = ""







    itr = 3







    (itr- , )





  4. , itr-, "" /:





    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 ch == "+":







    tempAx = tempAx + "+"







    ( ) “+” “+” . “-” “f”, “-” “f” . “F”, “F + F – f – F + F”, . , “”, . :





    axiom = tempAx







    tempAx = " "







    ( ):





  5. , , . , :





    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)







    , , "+", "-", "F" "f". , "+":





    if ch == "+":







    turtle.right(45)







    turtle.forward(10)







    turtle.right(45)







    , 45 , , 10, , 45 . "-":





    elif ch == "-":







    turtle.left(45)







    turtle.forward(10)







    turtle.left(45)







    , "+", , . "F" "f", 20 :





    else:







    turtle.forward(20)







    -:





  6. -, :





    turtle.fillcolor("#99BBFF")







    turtle.begin_fill()







    #99BBFF - (RGB: 153, 187, 255), begin_fill() . , , :





    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:












All Articles