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

## Python: Drawing Shapes

**Learning : Drawing Shapes **

** Subject: New shapes function**

To Draw a Square shape, we need to know the width ( W ) of the square side, and then we draw a line and moving in 90 degree and drawing another line and so on until we finished the 4 side of the square. In the same principle if we want to draw a triangle (equilateral one), we need to know length of its sides and in mathematics we know that in equilateral triangles the angles (corners) are 120 degree, so we draw a line and move in 120 degree and drawing another two sides.

In coming code, we will write a general function in Python to pass the number on sides we want to draw (triangle =3, Square=4,Pentagon = 5, Hexagon =6 .. and so on), the width (size) of the shape and the position (x,y) of the first angle or point.

**The Codes:**

def d_shape(s_heads,w,x1,y1):

t.goto(x1,y1)

# To get t.right angle

rang = 360 / s_heads

t.pendown()

for x in range (s_heads +1) :

t.forward(w)

t.right(-rang)

t.penup()

__Results__ after using the new function we can pass any number of sides and the function will draw the shape, here are a sample execution of it. .. *.. Click to enlarge ..*

Now if we call the function number of times equal to it’s heads what we will get ? let’s see . *.. Click to enlarge ..*

And take a look when we set the numbers to 20. *.. Click to enlarge ..*

To Download my Python code (.py) files

Click-Here

## Python: Random Squares

** Random Squares Art **

** Subject: Python, Graphics and simulation **

In This project say we have a Square (10 x 10 cm) the square has four corners labeled as (a, b, c, d) as in figure i.

then we select a random corner (c or d) [assume it is c] then we select an angle (ang) of rotation between (10, 45), and we draw another square positioning (a) on the (c) and rotating it with (ang) anticlockwise as figure ii.

Now if we repeat this two steps ..

S1. Selecting a random corner (c or d).

S2. Selecting a random rotation angle between (10, 45). and draw the square.

let’s see what we may have as a random art generator.

**Python Code for Random Squares Art**

__Codes to select corner (c or d)__

def select_c_or_d():

if random.randrange(0,2) == 0 :

x = cdpos[0][0]

y = cdpos[0][1]

else:

x = cdpos[1][0]

y = cdpos[1][1]

t.setheading(0)

t.right(random.randrange(rotmin,rotmax)*f)

__Codes to draw the Square (c or d)__

def d_square(w,x1,y1):

t.goto(x1,y1)

t.pendown()

t.forward(w)

t.right(-90)

t.forward(w)

# save corner c position

cdpos.append([t.xcor(),t.ycor()])

t.right(-90)

t.forward(w)

# save corner d position

cdpos.append([t.xcor(),t.ycor()])

t.right(-90)

t.forward(w)

t.penup()

I notes that if we increase the number of Squares, we start to have some interesting results.

.. Have Fun ..

To Download my Python code (.py) files

Click-Here

## Python project

**Python: Draw Flower**

**Drawing with Python**

In this post i tried to draw another flower using arcs of circles, i get a simple one and i guess with more sizes we can do more complex shape.

You can play with the code and change the numbers, size and rotation degree and let’s see what we will have.

t = turtle.Turtle()

t.hideturtle()

t.pendown()

for x in range (30):

t.circle(60,70)

if x %3==0:

t.circle (10,90)

## Python Project

**Python: Draw Flower**

**Drawing with Python**

One of my favorite areas is drawing, and when its come to programming languages, one of the first thing i look after is the commands that let me draw lines and shapes.

In python, we have to import a library to help us in this task, here in this short code block I am using codes to draw a flower shape. We can improve the idea later but i want the first version to be as simple as i can. So lets see the code.

# We have to import this library

import turtle

# Here are some setting

t = turtle.Turtle()

t.shape(“point”)

t.color(‘black’)

t.speed(9)

t.left(0)

t.penup

size=10

#This function to draw one petal

def petal():

t.pendown()

for x in range (40):

t.forward(size)

t.left(x)

t.penup()

left_d=-15

for pet in range (8):

petal()

t.goto(0,0)

t.left(-15)

———————————————–

## From my Sketch Book

Bird From my Sketch Book

I am sketching to enhance my skill, here is one of sketches from my sketchbook using pencil as a guide-line and ink-pen to finish the drawing.

Click to Enlarge

**::My SketchBook::**

**Sketch of**:* Bird*

**Using:*** Ink-Pen*

**Date:*** 5/12/2017 *

**SketchBook:*** SketchBook # 22*

**More Sketches:*** Click Here*

Ali,

## From my Sketch book

Giraffe From my Sketch Book

I am sketching to enhance my skill, here is one of sketches from my sketchbook using pencil as a guide-line and ink-pen to finish the drawing.

Click to Enlarge

**::My SketchBook::**

**Sketch of**:* Giraffe*

**Using:*** Ink-Pen*

**Date:*** 15/10/2017 *

**SketchBook:*** SketchBook # 22*

**More Sketches:*** Click Here*

Ali,

## From my Sketch Book

Little Sparrow From my Sketch Book

I am sketching to enhance my skill, here is one of sketches from my sketchbook using pencil as a guide-line and ink-pen to finish the drawing.

Click to Enlarge

**::My SketchBook::**

**Sketch of**:* Little Sparrow*

**Using:*** Ink-Pen*

**Date:*** 29/12/2017 *

**SketchBook:*** SketchBook # 23*

**More Sketches:*** Click Here*

Ali,

## From my Sketchbook

Touche Turtle From my Sketch Book

Click to Enlarge

**::My SketchBook::**

**Sketch of**:* Touche Turtle*

**Using:*** Ink-Pen*

**Date:*** 21/1/2017 *

**SketchBook:*** SketchBook # 13*

**More Sketches:*** Click Here*

Ali,

## From my sketch book

Old Mosque From my Sketch Book

Click to Enlarge

**::My SketchBook::**

**Sketch of**:* Water Color*

**Using:*** Ink-Pen*

**Date:*** 11/1/2017 *

**SketchBook:*** SketchBook # 13*

**More Sketches:*** Click Here*

Ali,