Python and Turtle custom functions,Difficulty Level 7,math,python,recursion Gardi Fractal with Python and Turtle (Source Code)

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()

Related Post