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)

6-Line Snowflake Fractal with Python and Turtle (Tutorial and Source Code)

Use recursion to draw the following snowflake shape generated from 6 sub-fractals of splitting lines.

Recursion Depth 5

The following figures show recursion depths ranging from 0 to 4.

Solution:

To solve this problem, you want to start from a function that draws a line given starting point (x,y), length, direction, and pensize. The following is the code for this function:

def line(x,y,length,direction,pensize):
  turtle.up()
  turtle.pensize(pensize)
  turtle.goto(x,y)
  turtle.down()
  turtle.seth(direction)
  turtle.fd(length)

Now you can define the recursive function that keeps branching out the shorter pair of lines. The starting point of the branch is about 2/5 of way from the original starting point. One branch is turning right about 25 degrees and the other branch is turning left about 25 degrees. Pen size should decrease proportionally too. The following is the code for this function:

def line_fractal(x,y,length,direction,pensize,n):
  if n==0: return
  line(x,y,length,direction,pensize)
  line_fractal(x+math.cos(direction*math.pi/180)*length*2/5,
               y+math.sin(direction*math.pi/180)*length*2/5,
               length*3/5,
               direction-25,
               pensize*3/5,
               n-1)
  line_fractal(x+math.cos(direction*math.pi/180)*length*2/5,
               y+math.sin(direction*math.pi/180)*length*2/5,
               length*3/5,
               direction+25,
               pensize*3/5,
               n-1)

The last function is the easiest. Just call the function above 6 times with 6 evenly spaced lines across 360 degrees. The following is the complete code for this project:

import turtle
import math

screen = turtle.Screen()
screen.title('6 Line Snowflake Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)

def line(x,y,length,direction,pensize):
  turtle.up()
  turtle.pensize(pensize)
  turtle.goto(x,y)
  turtle.down()
  turtle.seth(direction)
  turtle.fd(length)

def line_fractal(x,y,length,direction,pensize,n):
  if n==0: return
  line(x,y,length,direction,pensize)
  line_fractal(x+math.cos(direction*math.pi/180)*length*2/5,
               y+math.sin(direction*math.pi/180)*length*2/5,
               length*3/5,
               direction-25,
               pensize*3/5,
               n-1)
  line_fractal(x+math.cos(direction*math.pi/180)*length*2/5,
               y+math.sin(direction*math.pi/180)*length*2/5,
               length*3/5,
               direction+25,
               pensize*3/5,
               n-1)

def snowflake():
  for i in range(6):
    line_fractal(0,0,400,i*60,5,5)

snowflake()
screen.update()

Gardi Fractal with Python and Turtle (Source Code)

Lori Gardi created this nested overlapping circle fractal in the paper ‘The Universe is a Fractal’. The fractal according to the paper looks similar to a supernova. A simple version to draw is to let the overlapping circles to pass through the centers of the other circle.

Gardi Fractal

To draw this, it will be helpful to create a basic function that draw a circle given the center and radius. Then, create a function that draws the overlapping circles in horizontal and vertical orientations. With these two functions, the recursive function just need to flip the orientation and reduce the radius size in each recursive call. You need do a little geometry to figure out the radius of the smaller circles. The following is the source code that draw this fractal:

import turtle

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

def circle(x,y,radius):
  turtle.up()
  turtle.goto(x,y-radius)
  turtle.down()
  turtle.seth(0)
  turtle.circle(radius, steps=360)

def two_circles(x,y,radius,orientation):
  turtle.pensize(radius/50)
  if orientation==0:
    circle(x-radius/2,y,radius)
    circle(x+radius/2,y,radius)
  else:
    circle(x,y-radius/2,radius)
    circle(x,y+radius/2,radius)

def gardi_fractal(x,y,radius,orientation,n):
  if n==0: return
  two_circles(x,y,radius,orientation)
  gardi_fractal(x,y,(4-7**0.5)/3*radius,1-orientation,n-1)
  
gardi_fractal(0,0,300,0,6)
screen.update()