Golden Fractal Tree with Python Turtle (Source Code)

Golden Fractal Tree is a tree based on Golden Ratio. The golden fractal tree contains the main branch and three smaller golden fractal trees: the first branch turns left by 72 degrees with ratio of main branch to the parent’s main branch = 2-golden_ratio; the second branch have the same the direction as the parent with ratio of main branch to the parent’s main branch = golden_ratio-1; the first branch turns right by 72 degrees with ratio of main branch to the parent’s main branch = 2-golden_ratio. For more details on golden fractal tree, please check out this web page.

Golden Fractal Tree

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Golden Fractal Tree - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
turtle.tracer(0,0)
golden_ratio = (1+5**0.5)/2

def golden_fractal_tree(x,y,direction,length):
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(direction)
    turtle.pensize(math.log(length,2)/3)
    if length<10: turtle.color('forest green')
    else: turtle.color('gray')
    turtle.down()
    turtle.fd(length)
    if length < 3: return
    cx,cy = turtle.xcor(), turtle.ycor()
    golden_fractal_tree(cx,cy,direction+72,(2-golden_ratio)*length)
    golden_fractal_tree(cx,cy,direction-72,(2-golden_ratio)*length)
    golden_fractal_tree(cx,cy,direction,(golden_ratio-1)*length)

golden_fractal_tree(0,-900,90,700)
turtle.update()

Five Pointed Star Fractal with Python and Turtle (Source Code)

Draw a five pointed star fractal that have nested five pointed star as shown.

Hint: Create a function that draws five pointed star in any location, orientation, and size.

Five Pointed Star Fractal

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Five Pointed Star Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
turtle.pensize(1)

def five_pointed_star(x,y,direction,r): #x,y is the center
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(direction)
    turtle.fd(r)
    turtle.right(180-18)
    turtle.down()
    length = r*math.sin(math.pi*2/5)/(1+math.sin(math.pi/10))
    for _ in range(5):
        turtle.fd(length)
        turtle.left(72)
        turtle.fd(length)
        turtle.right(180-36)

def five_pointed_star_fractal(x,y,direction,r): #x,y is the center
    five_pointed_star(x,y,direction,r)
    if r < 20: return
    five_pointed_star_fractal(x,y,180+direction,r*math.sin(math.pi/10)/math.cos(math.pi/5))

five_pointed_star_fractal(0,-40,90,1000)

Pentagram Fractal with Python Turtle (Source Code)

Draw the following fractal made up of nesting pentagrams with either recursion or iteration.

Hint: Making a function that draws pentagram in any location, radius, and direction will help!

Pentagram Fractal

Source Code:

import turtle
import math

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

def star(x,y,direction,r): #x,y is the center
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(direction)
    turtle.fd(r)
    turtle.right(180-18)
    turtle.down()
    length = 2*r*math.sin(math.pi*2/5)
    for _ in range(5):
        turtle.fd(length)
        turtle.right(180-36)

def star_fractal(x,y,direction,r):
    star(x,y,direction,r)
    if r < 50: return
    star_fractal(x,y,180+direction,r*math.sin(math.pi/10)/math.cos(math.pi/5))


star_fractal(0,0,90,1000)