Python and Turtle custom functions,Difficulty Level 6,recursion Sierpinski Triangle Tree with Python and Turtle (Source Code)

Sierpinski Triangle Tree with Python and Turtle (Source Code)

Use recursion to draw the following Sierpinski Triangle the similar method to drawing a fractal tree.

Sierpinski Triangle Tree with Python and Turtle

Source Code:

import turtle
turtle.title('Sierpinski Tree - PythonTurtle.Academy')
turtle.setworldcoordinates(-2000,-2000,2000,2000)
screen = turtle.Screen()
screen.tracer(0,0)
turtle.hideturtle()

def sierpinski_tree(x,y,length,tilt,n):
    if n==0: return
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(tilt)
    turtle.down()
    turtle.fd(length)
    sierpinski_tree(turtle.xcor(),turtle.ycor(),length/2,turtle.heading(),n-1)
    
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(tilt+120)
    turtle.down()
    turtle.fd(length)
    sierpinski_tree(turtle.xcor(),turtle.ycor(),length/2,turtle.heading(),n-1)

    turtle.up()
    turtle.goto(x,y)
    turtle.seth(tilt-120)
    turtle.down()
    turtle.fd(length)
    sierpinski_tree(turtle.xcor(),turtle.ycor(),length/2,turtle.heading(),n-1)

sierpinski_tree(0,-250,1000,90,8)
screen.update()

Related Post