Python and Turtle Difficulty Level 6,recursion Vicsek Fractal with Python and Turtle (Source Code)

Vicsek Fractal with Python and Turtle (Source Code)

Draw the Vicsek Fractal based on cross with recursion and Turtle. The following figures show first a few recursive steps:

Recursive Depth 0
Recursive Depth 1
Recursive Depth 2
Recursive Depth 3
Recursive Depth 4
Recursive Depth 5

Source Code:

import turtle

screen = turtle.Screen()
screen.title('Vicsek Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()
turtle.color('blue')

def draw_cross(x,y,length):
    turtle.up()
    turtle.goto(x-length/2,y-length/6)
    turtle.down()
    turtle.seth(0)
    turtle.begin_fill()
    for _ in range(4):
        turtle.fd(length/3)
        turtle.right(90)
        turtle.fd(length/3)
        turtle.left(90)
        turtle.fd(length/3)
        turtle.left(90)      
    turtle.end_fill()

def vicsek(x,y,length,n):
    if n==0:
        draw_cross(x,y,length)
        return

    vicsek(x,y,length/3,n-1)
    vicsek(x+length/3,y,length/3,n-1)
    vicsek(x-length/3,y,length/3,n-1)
    vicsek(x,y+length/3,length/3,n-1)
    vicsek(x,y-length/3,length/3,n-1)
    
vicsek(0,0,1600,5)
screen.update()

Related Post