## Python and Lindenmayer System – P3

**Learning : Lindenmayer System P3**

** Subject: Drawing Fractal Tree using Python L-System**

In the first two parts of the L-System posts (Read Here: P1, P2) we talk and draw some geometric shapes and patterns. Here in this part 3 we will cover only the **Fractal Tree** and looking for other functions that we may write to add leaves and flowers on the tree.

Assuming that we have the Pattern generating function and l-system drawing function from part one, I will write the rules and attributes to draw the tree and see what we may get.

So, first tree will have:

# L-System Rule to draw ‘Fractal Tree’

# Rule: F: F[+F]F[-F]F

# Angle: 25

# Start With: F

# Iteration : 4

and the output will be this:

If we need to add some flowers on the tree, then we need to do two things, first one is to write a function to draw a flower, then we need to add a variable to our rule that will generate a flower position in the pattern. First let’s write a flower function. We will assume that we may want just to draw a small circles on the tree, or we may want to draw a full open flower, a simple flower will consist of 4 Petals and a Stamen, so our flower drawing function will draw 4 circles to present the Petals and one middle circle as the Stamen. We will give the function a variable to determine if we want to draw a full flower or just a circle, also the size and color of the flowers.

Here is the code ..

font = 516E92

commint = #8C8C8C

**Header here**

# Functin to draw Flower

def d_flower () :

if random.randint (1,1) == 1 :

# if full_flower = ‘y’ the function will draw a full flower,

# if full_flower = ‘n’ the function will draw only a circle

full_flower = ‘y’

t.penup()

x1 = t.xcor()

y1 = t.ycor()

f_size = 2

offset = 3

deg = 90

if full_flower == ‘y’ :

t.color(‘#FAB0F4’)

t.fillcolor(‘#FAB0F4’)

t.goto(x1,y1)

t.setheading(15)

for x in range (0,4) : # To draw a 4-Petals

t.pendown()

t.begin_fill()

t.circle(f_size)

t.end_fill()

t.penup()

t.right(deg)

t.forward(offset)

t.setheading(15)

t.goto(x1,y1 – offset * 2 + 2)

t.pendown() # To draw a white Stamen

t.color(‘#FFFFF’)

t.fillcolor(‘#FFFFFF’)

t.begin_fill()

t.circle(f_size)

t.end_fill()

t.penup()

else: # To draw a circle as close flower

t.pendown()

t.color(‘#FB392C’)

t.end_fill()

t.circle(f_size)

t.end_fill()

t.penup()

t.color(‘black’)

Then we need to add some code to our rule and we will use variable ‘o’ to draw the flowers, also I will add a random number selecting to generate the flowers density. Here is the code for it ..

In the code the random function will pick a number between (1-5) if it is a 3 then the flower will be drawn. More density make it (1-2), less density (1-20) |

And here is the output if we run the l-System using this rule: Rule: F: F[+F]F[-F]Fo

Using the concepts, here is some samples with another **Fractal Tree** and flowers.

Another Fractal Tree without any Flowers. |

Fractal Tree with closed Pink Flowers. |

Fractal Tree with closed Red Flowers. |

Fractal Tree with open pink Flowers. |

To Download my Python code (.py) files

Click-Here

## Python and Lindenmayer System – P2

**Learning : Lindenmayer System P2**

** Subject: Drawing with python using L-System**

In the first part of **Lindenmayer System L-System** post (**Click to Read**) we had wrote two functions: one to generate the pattern based on the variables and roles, and one to draw lines and rotate based on the pattern we have.

In this part I will post images of what Art we can generate from L-System

the codes will be the L-system that generate the patterns, so the code will include: the Rules, Angle (Right, Left) Iteration and Starting Variable.

The possibilities to generate the putters and therefore drawing the output is endless, any slightly changes in the iterations or rotation (+ -) angles will take all output to a new levels. In the coming post, I will use the L-system to generate fractal tree and see what we can get from there.

To Download my Python code (.py) files

Click-Here

## 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.

To Download my Python code (.py) files

Click-Here