Home > Art, L-System, Learning, Lesson, Problem, Projects/Experiments, Python > Python and Lindenmayer System – P1

Python and Lindenmayer System – P1



Learning : Lindenmayer System P1
Subject: Drawing with python using L-System

First What is Lindenmayer System or L-System? L-System is a system consists of an alphabet of symbols (A, B, C ..) that can be used to make strings, and a collection of rules that expand each symbol into larger string of symbols.

L-system structure: We can put it as Variables, Constants, Axiom, Rules

Variables (V): A, B, C …
constants : We define a symbols that present some movements, such as ‘+’ mean rotate right x degree, ‘F’ mean move forward and so on ..
Axiom : Axiom or Initiator is a string of symbols from Variable (V ) defining the initial state of the system.
Rules : Defining the way variables can be replaced with combinations of constants and other variables.

Sample:
Variables : A, B {we have two variables A and B}
Constants : none
Axiom : A {Start from A}
Rules : (A → AB), (B → A) {convert A to AB, and convert B to A}

So if we start running the Nx is the number the time we run the rules (Iteration).
N0 : A
N1 : AB
N2 : AB A
N3 : AB A AB
N4 : AB A AB AB A
N5 : AB A AB A AB A AB .. an so-on
So in this example after 5 Iteration we will have this pattern (AB A AB A AB A AB)

In this post we will write two functions, one to generate the pattern based on the Variables and Rules we have. Another function to draw the pattern using Python Turtle and based on the Constants we have within the patterns.

The constants that we may use and they are often used as standard are:

F means “Move forward and draw line”.
f means “Move forward Don’t draw line”.
+ means “turn left by ang_L°”.
− means “turn right ang_R°”.
[ means “save position and angle”.
] means “pop position and angle”.
X means “Do nothing”

and sometime you may add your own symbols and and rules.

First Function: Generate the Pattern will take the Axiom (Start symbol) and apply the rules that we have (as our AB sample above). The tricky point here is that the function is changing with each example, so nothing fixed here. In the coming code i am using only one variable F mean (move forward) and + – to left and right rotations. Other patterns may include more variables. once we finished the function will return the new string list.

Generate the Pattern

# Generate the patern
def l_system(s) :

new_s = []

for each in s :

if each == ‘F’:

new_s.append(‘F+F+FF-F’)

else :

new_s.append(each)

return new_s



The second function: Draw the Pattern will take the string we have and draw it based on the commands and rules we have such as if it read ‘F’ then it will move forward and draw line, and if it reads ‘-‘ then it “turn right ang_R°”.
here is the code ..

Draw the Pattern
def draw_l_system(x,y,s,b,ang_L,ang_R):

cp = [] # Current position

t.goto(x,y)

t.setheading(90)

t.pendown()

for each in s:

if each == ‘F’ :

t.forward(b)

if each == ‘f’ :

t.penup()

t.forward(b)

t.pendown()

elif each == ‘+’:

t.left(ang_L)

elif each == ‘-‘:

t.right(ang_R)

elif each == ‘[‘:

cp.append((t.heading(),t.pos()))

elif each == ‘]’:

heading, position = cp.pop()

t.penup()

t.goto(position)

t.setheading(heading)

t.pendown()

t.penup()


Now we will just see a one example of what we may get out from all this, and in the next post P2, we will do more sample of drawing using L-System.


In the image bellow, left side showing the Rules, angles and iterations and on the right side the output after drawing the patters.

  1. No comments yet.
  1. November 5, 2019 at 8:33 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s