Drawing Sierpinski Pentagon with Chaos Game and Python Turtle (with Solution)

Draw the Sierpinski Pentagon with Chaos Game. Work on this Sierpinski Triangle with Chaos Game first if you haven’t done so already.

Hint: Try jump less than half way to the target.

Sierpinski Pentagon with Chaos Game

Solution:

import turtle
import random
import math

screen = turtle.Screen()
screen.title('Sierpinski Pentagon with Chaos Game - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-250,-250,250,250)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.up()

m=5
angle = math.pi/2
V = []
for i in range(m):
    p = (400*math.cos(angle),400*math.sin(angle))
    V.append(p)
    angle += math.pi*2/m

for v in V:
    turtle.goto(v)
    turtle.dot('red')

n = 100000 # number of points to draw
p = V[0] # start from first vertex
t = turtle.Turtle()
t.up()
t.hideturtle()
for i in range(n):
    t.goto(p)
    t.dot(2,'orange')
    q = V[random.randrange(len(V))] # pick a random vertex
    p = ((q[0]+p[0])/2.6,(q[1]+p[1])/2.6) # go to mid point between the random vertex and point   
    if i % 1000 == 0: # update for every 1000 moves, this part is for performance reason only
        t = turtle.Turtle() # use new turutle
        t.up()
        t.hideturtle()
        screen.update()

Drawing Sierpinski Carpet with Chaos Game (with Solution)

You can draw a Sierpinski Carpet with Chaos Game too. Read the first Chaos Game project first to understand what Chaos Game is.

In this project, you start with a square. The moving point will toward a randomly chosen corner of the square as well as the center of the four edges of the square. Therefore, there are total eight points that the moving point will move toward. In addition, it will only 1/3 of the way towards the target.

Sierpinski Carpet with Chaos Game

Solution:

import turtle
import random
import math

screen = turtle.Screen()
screen.title('Sierpinski Carpet with Chaos Game - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-200,-200,200,200)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.up()

m=4
angle = math.pi/4
V = []
for i in range(m):
    p = (400*math.cos(angle),400*math.sin(angle))
    V.append(p)
    angle += math.pi*2/m

for v in V:
    turtle.goto(v)
    turtle.dot('blue')

n = 100000 # number of points to draw
p = V[0] # start from the first vertex
t = turtle.Turtle()
t.up()
t.hideturtle()
for i in range(n):
    t.goto(p)
    t.dot(2,'red')
    r = random.randrange(len(V)) # pick a random vertex
    if random.randint(0,1) == 0: # randomly decide to use edge or vertex
        q = V[r] # vertex
    else:
        q = ((V[r][0]+V[(r+1)%len(V)][0])/2,(V[r][1]+V[(r+1)%len(V)][1])/2) # go to mid point between two vertices   
    p = ((q[0]+p[0])/3,(q[1]+p[1])/3) # go 1/3 towards target   
    if i % 1000 == 0: # update for every 1000 moves, this part is for performance reason only
        t = turtle.Turtle() # use new turutle
        t.up()
        t.hideturtle()
        screen.update()

Drawing Barnsley’s Fern with Chaos Game (Solution Included)

Draw Barnsley’s Fern with Chaos Game. The equation for construction is provided in this Wikipedia article.

Barnsley’s Fern with Chaos Game

Solution:

import turtle
import random

screen = turtle.Screen()
screen.title('Barnsley\'s Fern Chaos Game with Python Turtle')
screen.setup(1000,1000)
screen.setworldcoordinates(-6,-1,6,11)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.up()

n = 100000 # number of points to draw
p = (0,0) 
t = turtle.Turtle()
t.up()
t.hideturtle()
for i in range(n):
    t.goto(p)
    t.dot(2,'green') 
    r = random.uniform(0,1)
    if r < 0.01:
        p = (0,0.16*p[1])
    elif r < 0.86:
        p = (0.85*p[0] + 0.04*p[1], -0.04*p[0] + 0.85*p[1] + 1.6)
    elif r < 0.93:
        p = (0.2*p[0] - 0.26*p[1], 0.23*p[0] + 0.22*p[1] + 1.6)
    else:
        p = (-0.15*p[0] + 0.28*p[1], 0.26*p[0] + 0.24*p[1] + 0.44)
        
    if i % 1000 == 0: # update for every 1000 moves, this part is for performance reason only
        t = turtle.Turtle() # use new turutle
        t.up()
        t.hideturtle()
        screen.update()

Restricted Polygon Chaos Game 2 with Python Turtle

In a previous project, you drew a Sierpinski Triangle with Chaos Game. You also did restricted chaos projects for square and pentagon. Now, generalize the project that allows chaos game for polygon of any number of vertices.

In this project, the restriction is: next vertex has to be immediate neighbors of the previous vertex or the same vertex as the previous vertex. This restriction should draw following figures from pentagon to decagon.

Pentagon Fractal with Chaos Game
Hexagon Fractal with Chaos Game
Heptagon Fractal with Chaos Game
Nonagon Fractal with Chaos Game
Decagon Fractal with Chaos Game

Restricted Polygon Chaos Game 1 with Python Turtle

In a previous project, you drew a Sierpinski Triangle with Chaos Game. You also did restricted chaos projects for square and pentagon. Now, generalize the project that allows chaos game for polygon of any number of vertices.

In this project, the restriction is: next vertex cannot be close neighbors of previously chosen vertex. This restriction should draw following figures from pentagon to decagon.

Pentagon Fractal with Chaos Game
Hexagon Fractal with Chaos Game
Heptagon Fractal with Chaos Game
Octagon Fractal with Chaos Game
Nonagon Fractal with Chaos Game
Decagon Fractal with Chaos Game

Restricted Pentagon Chaos Game (with Solution)

In a previous project, you drew a Sierpinski Triangle with Chaos Game. Instead of a triangle, use five points of a pentagon as the target points and play the chaos game. There is restriction: you can’t choose the same direction as the last move. It should draw something like this after 100,000 iterations:

Solution:

import turtle
import random
import math

screen = turtle.Screen()
screen.title('Restricted Pentagon Chaos Game with Python Turtle')
screen.setup(1000,1000)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.up()

m=5
angle = 0
V = []
for i in range(m):
    p = (400*math.cos(angle),400*math.sin(angle))
    V.append(p)
    angle += math.pi*2/m

for v in V:
    turtle.goto(v)
    turtle.dot('red')

n = 100000 # number of points to draw
p = (random.uniform(-200,200),random.uniform(-200,200)) # random starting point
t = turtle.Turtle()
t.up()
t.hideturtle()
lastr = r = -1
for i in range(n):
    t.goto(p)
    t.dot(2,'blue')
    while r == lastr:
        r = random.randrange(len(V)) # pick a random vertex
    lastr = r
    p = ((V[r][0]+p[0])/2,(V[r][1]+p[1])/2) # go to mid point between the random vertex and point   
    if i % 1000 == 0: # update for every 1000 moves, this part is for performance reason only
        t = turtle.Turtle() # use new turutle
        t.up()
        t.hideturtle()
        screen.update()

Restricted Square Chaos Game 2

In a previous project, you drew a Sierpinski Triangle with Chaos Game. Instead of a triangle, use four points of a square as the target points and play the chaos game. There is restriction: you can’t choose a vertex that is 2 places away. That is: you can only move to the same vertex as the last move or the immediate neighbor of the last move. It should draw something like this after 100,000 iterations:

Restricted Square Chaos Game

Restricted Square Chaos Game 1

In a previous project, you drew a Sierpinski Triangle with Chaos Game. Instead of a triangle, use four points of a square as the target points and play the chaos game. There is restriction: you can’t choose the same direction as the last move. It should draw something like this after 100,000 iterations:

Restricted Square Chaos Game

Chaos Game – Sierpinski Triangle with Python Turtle (with Solution)

Draw three points to form a triangle. Draw the fourth point in a random position. In each of the following step, this point will move halfway towards one of the three randomly chosen points of the original triangle. Repeat the above step 1000, 10,000, and 100,000 times. You will see a Sierpinski Triangle! This process is an example of Chaos Game.

Triangle Chaos Game after 1,000 Steps
Triangle Chaos Game after 10,000 Steps
Triangle Chaos Game after 100,000 Steps

Solution:

import turtle
import random

screen = turtle.Screen()
screen.title('Triangle Chaos Game with Python Turtle')
screen.setup(1000,1000)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.up()

A = (0,350)
B = (-300,-200)
C = (300,-200)
V = (A,B,C) # list of three vertices
for v in V:
    turtle.goto(v)
    turtle.dot('red')

n = 10000 # number of points to draw
p = (random.uniform(-200,200),random.uniform(-200,200)) # random starting point
t = turtle.Turtle()
t.up()
t.hideturtle()
for i in range(n):
    t.goto(p)
    t.dot(2,'blue')
    r = random.randrange(len(V)) # pick a random vertex
    p = ((V[r][0]+p[0])/2,(V[r][1]+p[1])/2) # go to mid point between the random vertex and point   
    if i % 1000 == 0: # update for every 1000 moves, this part is for performance reason only
        t = turtle.Turtle() # use new turutle
        t.up()
        t.hideturtle()
        screen.update()