Draw tree using Turtle module in Python
Last Updated :
01 Oct, 2020
Prerequisite: Turtle module, Drawing Triangle, Drawing Rectangle
There are many modules in python which depicts graphical illustrations, one of them is turtle, it is an in-built module in Python, which lets the user control a pen(turtle) to draw on screen(drawing board). It is mostly used to illustrate figures, shapes, designs etc. In this article, we will learn how to draw a simple tree using the turtle module. Illustrating a Tree consists of creating a single rectangle and then three triangles of same sizes sequentially from the bottom.
Below are the steps to create a tree:
- Import turtle and math module.
- Set screen with dimensions and color.
- Create a turtle object.
- Create tree by illustrating stacked triangles and a rectangle.
Below is the program of the above approach:
Python3
import turtle
import math
def drawRectangle(t, width, height, color):
t.fillcolor(color)
t.begin_fill()
t.forward(width)
t.left( 90 )
t.forward(height)
t.left( 90 )
t.forward(width)
t.left( 90 )
t.forward(height)
t.left( 90 )
t.end_fill()
def drawTriangle(t, length, color):
t.fillcolor(color)
t.begin_fill()
t.forward(length)
t.left( 135 )
t.forward(length / math.sqrt( 2 ))
t.left( 90 )
t.forward(length / math.sqrt( 2 ))
t.left( 135 )
t.end_fill()
screen = turtle.Screen ( )
screen.bgcolor( "skyblue" )
tip = turtle.Turtle()
tip.color ( "black" )
tip.shape ( "turtle" )
tip.speed ( 2 )
tip.penup()
tip.goto( 100 , - 130 )
tip.pendown()
drawRectangle(tip, 20 , 40 , "brown" )
tip.penup()
tip.goto( 65 , - 90 )
tip.pendown()
drawTriangle(tip, 90 , "lightgreen" )
tip.penup()
tip.goto( 70 , - 45 )
tip.pendown()
drawTriangle(tip, 80 , "lightgreen" )
tip.penup()
tip.goto( 75 , - 5 )
tip.pendown()
drawTriangle(tip, 70 , "lightgreen" )
|
Output:-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...