This project demonstrates how to create stunning fractal tree graphics using Python’s Turtle module. A fractal tree is a recursive structure that repeats a simple pattern at smaller scales, creating complex and intricate designs. The Turtle module is perfect for drawing such graphics because of its simple commands and ability to visualize recursion easily.
To work on this project, you should have:
By the end of this project, you will:
To run this project, you need to have Python installed on your system. You can download it from python.org.
import turtle as tu
roo = tu.Turtle() # Turtle object
wn = tu.Screen() # Screen Object
wn.bgcolor("black") # Screen Bg color
wn.title("Fractal Tree Pattern")
roo.left(90) # moving the turtle 90 degrees towards left
roo.speed(20) # setting the speed of the turtle
def draw(l): # recursive function taking length 'l' as argument
if (l < 10): return
else:
roo.pensize(2) # Setting Pensize
roo.pencolor("yellow") # Setting Pencolor as yellow
roo.forward(l) # moving turtle forward by 'l'
roo.left(30) # moving the turtle 30 degrees towards left
draw(3 * l / 4) # drawing a fractal on the left of the turtle object 'roo' with 3/4th of its length
roo.right(60) # moving the turtle 60 degrees towards right
draw(3 * l / 4) # drawing a fractal on the right of the turtle object 'roo' with 3/4th of its length
roo.left(30) # moving the turtle 30 degrees towards left
roo.pensize(2)
roo.backward(l) # returning the turtle back to its original psition
draw(20) # drawing 20 times
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("magenta") # magenta
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("red") # red
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor("lightgreen") # lightgreen
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor("red") # red
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor("yellow") # yellow
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("cyan") # cyan
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("yellow") # yellow
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("magenta") # magenta
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
wn.exitonclick()
import turtle as tu
roo = tu.Turtle() # Turtle object
wn = tu.Screen() # Screen Object
wn.bgcolor("black") # Screen Bg color
wn.title("Fractal Tree Pattern")
roo.left(90) # moving the turtle 90 degrees towards left
roo.speed(20) # setting the speed of the turtle
def draw(l): # recursive function taking length 'l' as argument
if (l < 10): return
else:
roo.pensize(2) # Setting Pensize
roo.pencolor("yellow") # Setting Pencolor as yellow
roo.forward(l) # moving turtle forward by 'l'
roo.left(30) # moving the turtle 30 degrees towards left
draw(3 * l / 4) # drawing a fractal on the left of the turtle object 'roo' with 3/4th of its length
roo.right(60) # moving the turtle 60 degrees towards right
draw(3 * l / 4) # drawing a fractal on the right of the turtle object 'roo' with 3/4th of its length
roo.left(30) # moving the turtle 30 degrees towards left
roo.pensize(2)
roo.backward(l) # returning the turtle back to its original psition
draw(20) # drawing 20 times
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("magenta") # magenta
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("red") # red
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor("lightgreen") # lightgreen
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor("red") # red
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor("yellow") # yellow
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(3)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("cyan") # cyan
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("yellow") # yellow
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor("magenta") # magenta
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10): return
else:
roo.pensize(2)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
wn.exitonclick()
The pseudo code for drawing the fractal tree is as follows:
function draw(length)
if length < 10
return
else
set pen size and color
move forward by length
turn left by 30 degrees
draw(3/4 of length)
turn right by 60 degrees
draw(3/4 of length)
turn left by 30 degrees
move backward by length
The pseudo code for drawing the fractal tree is as follows:
function draw(length)
if length < 10
return
else
set pen size and color
move forward by length
turn left by 30 degrees
draw(3/4 of length)
turn right by 60 degrees
draw(3/4 of length)
turn left by 30 degrees
move backward by length
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India