Hexagon Spiral of Spirals Colored (Source Code)

After drawing pentagon spiral of pentagon spirals, draw colored version of hexagon spiral of spirals with recursion and Turtle library.

Hexagon Spiral of Spirals Colored

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Hexagon Spiral of Hexagon Spirals - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)

colors = [ 'red', 'orange', 'green', 'teal', 'blue', 'magenta' ]

def draw_spiral(x,y,r,direction,first=False):
    if r < 1: return
    d = direction
    r_ratio = math.cos(math.radians(30))/math.cos(math.radians(30-alpha))
    d_ratio = math.sin(math.radians(30))-r_ratio*math.sin(math.radians(30-alpha))
    for i in range(6):
        if first: turtle.color(colors[i]) 
        px = x + r*math.cos(math.radians(direction))
        py = y + r*math.sin(math.radians(direction))
        r2 = r
        d = direction
        c = 0
        flag = False
        while True:
            dist = r2*d_ratio
            if c > 10 and dist < 1: break
            if dist > 4:
                draw_spiral(px,py,dist*0.4,d)
                turtle.up()
                turtle.goto(px,py)
                turtle.seth(d+180-60)
                turtle.fd(dist)
                px,py = turtle.xcor(), turtle.ycor()
            elif not flag:
                turtle.up()
                turtle.goto(px,py)
                turtle.down()
                flag = True
                turtle.seth(d+180-60)
                turtle.fd(dist)
            else:   
                turtle.seth(d+180-60)
                turtle.fd(dist)
    
            r2 = r2*r_ratio
            d += alpha
            c += 1
        direction += 60
    
    
alpha = 20
draw_spiral(0,0,800,90,True)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")

Pentagon Spiral of Pentagon Spirals Fractal (Source Code)

After finishing 5 spirals and spiral of spirals, draw the following pentagon spiral of pentagon spirals using recursion.

Pentagon Spiral of Pentagon Spirals

Source Code: (This code may run for several minutes)

import turtle
import math

screen = turtle.Screen()
screen.title('Pentagon Spiral of Pentagon Spirals - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)

def draw_spiral(x,y,r,direction):
    if r < 1: return
    d = direction
    r_ratio = math.cos(math.radians(36))/math.cos(math.radians(36-alpha))
    d_ratio = math.sin(math.radians(36))-r_ratio*math.sin(math.radians(36-alpha))
    for _ in range(5):
        px = x + r*math.cos(math.radians(direction))
        py = y + r*math.sin(math.radians(direction))
        r2 = r
        d = direction
        c = 0
        flag = False
        while True:
            dist = r2*d_ratio
            if c > 10 and dist < 1: break
            if dist > 3:
                draw_spiral(px,py,dist*0.5,d)
                turtle.up()
                turtle.goto(px,py)
                turtle.seth(d+180-54)
                turtle.fd(dist)
                px,py = turtle.xcor(), turtle.ycor()
            elif not flag:
                turtle.up()
                turtle.goto(px,py)
                turtle.down()
                flag = True
                turtle.seth(d+180-54)
                turtle.fd(dist)
            else:   
                turtle.seth(d+180-54)
                turtle.fd(dist)
    
            r2 = r2*r_ratio
            d += alpha
            c += 1
        direction += 360/5
    
    
alpha = 20
draw_spiral(0,0,800,90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")

5 Spirals (Source Code)

Modify pentagon spiral to draw 5 spirals as shown here.

Five Spirals

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('5 Spirals - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
turtle.pensize(2)

colors = [ 'red', 'orange', 'green', 'blue', 'purple' ]
def draw_spiral(x,y,r,direction):
    if r < 10: return
    d = direction
    r_ratio = math.cos(math.radians(36))/math.cos(math.radians(36-alpha))
    d_ratio = math.sin(math.radians(36))-r_ratio*math.sin(math.radians(36-alpha))
    for i in range(5):
        px = x + r*math.cos(math.radians(d))
        py = y + r*math.sin(math.radians(d))
        turtle.color(colors[i])
        turtle.up()
        turtle.goto(px,py)
        turtle.down()
        turtle.seth(d+180-54)
        dist = r*d_ratio
        r2 = r
        while dist > 0.1:
            turtle.fd(dist)
            r2 = r2*r_ratio
            dist = r2*d_ratio
            turtle.left(alpha)
        d += 360/5
       
alpha = 5
draw_spiral(0,0,900,90)

Pentagon Spiral inside Pentagon (Source Code)

This project draws square spiral inside a square. Now draw a pentagon spiral inside pentagon as shown.

Pentagon Spiral inside Pentagon

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Pentagon Spiral inside Pentagon - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
#screen.tracer(0,0)

def draw_spiral(x,y,r,direction):
    if r < 10: return
    d = direction
    r2 = r*math.cos(math.radians(36))/math.cos(math.radians(36-alpha))
    dist = r*math.sin(math.radians(36))-r2*math.sin(math.radians(36-alpha))
    turtle.up()
    px = x + r*math.cos(math.radians(d))
    py = y + r*math.sin(math.radians(d))
    turtle.goto(px,py)
    turtle.down()
    d += 360/5
    for _ in range(5):
        px = x + r*math.cos(math.radians(d))
        py = y + r*math.sin(math.radians(d))
        turtle.goto(px,py)
        d += 360/5
    draw_spiral(x,y,r2,direction+alpha)
    
    
alpha = 7
draw_spiral(0,0,900,90)

Nested Circle of Squares (Source Code)

Knowing how to draw circle of squares, draw the following nested circle of squares with either recursion or iteration.

Nested Circle of Squares with Python and Turtle

Source Code:

import turtle
import math
screen = turtle.Screen()
screen.title('Nested Squares Circles - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)

def draw_square(x,y,length,direction,color):
    turtle.up()
    turtle.goto(x-length/(2**0.5)*math.cos(math.radians(direction+45)),y-length/(2**0.5)*math.sin(math.radians(direction+45)))
    turtle.seth(direction)
    turtle.color(color)
    turtle.down()
    for _ in range(4):
        turtle.fd(length)
        turtle.left(90)

def draw_square_circle(radius,direction,n):
    length = 2*radius*math.sin(math.radians(180/n))
    length = length/(2**0.5)
    for _ in range(n):
        draw_square(radius*math.cos(math.radians(180/n))*math.cos(math.radians(direction)),\
                    radius*math.cos(math.radians(180/n))*math.sin(math.radians(direction)),\
                    length, direction+45,'black')
        direction += 360/n

def draw_square_circle_recursive(radius,direction,n):
    if radius < 10: return
    draw_square_circle(radius,direction,n)
    r = radius*math.cos(math.radians(180/n))-radius*math.sin(math.radians(180/n))
    draw_square_circle_recursive(r,direction+180/n,n)
    
draw_square_circle_recursive(600,0,11)
screen.update()

Hexagon Spiral with Python and Turtle (Source Code)

Draw the following spiral with hexagon shape. In each loop, increase the forward length and turn slightly less than 60 degrees.

Hexagon Spiral

Source Code:

import turtle
screen = turtle.Screen()
screen.title('Hexagon Spiral - PythonTurtle.Academy')
turtle.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

for i in range(10,780,3):
    turtle.fd(i)
    turtle.left(59.3)

Chase the Cycler Simulation (with Source Code)

In a previous project called massive chasing game, you simulated a game where you created 100 turtles and each turtle chase the next one and the last turtle chase the first one.

In this project, instead of making the last turtle chase the first one, make it move on a circle forever. It will create an interesting animation shown above.

Source Code:

import turtle
import random
import colorsys

screen = turtle.Screen()
screen.tracer(0,0)
screen.setup(1000,1000)
screen.title('Chase the Cycler - PythonTurtle.Academy')
turtle.hideturtle()

n=200
chasers = []
for i in range(n):
    chasers.append(turtle.Turtle())

h = 0
for i in range(n):
    c = colorsys.hsv_to_rgb(h,1,0.8)
    h += 1/n
    chasers[i].color(c)
    chasers[i].up()
    chasers[i].goto(random.uniform(-500,500), random.uniform(-500,500))
chasers[n-1].goto(0,-300)
chasers[n-1].shape('circle')
chasers[n-1].shapesize(1)

def chase():
    for i in range(n-1):
        angle = chasers[i].towards(chasers[i+1])
        chasers[i].seth(angle)
    chasers[n-1].left(2)
    chasers[n-1].fd(10)
    for i in range(n-1):
        chasers[i].fd(10)
    screen.update()
    screen.ontimer(chase,1000//20)
    
chase()

Drawing A Chinese College Entrance Exam Problem

A YouTube channel MindYourDecisions posted a video about an interesting Chinese College Entrance exam question. The following is the screen of the problem posted by MindYourDecisions:

Draw the graph of the function ranging from [-3,3]. Also draw a horizontal line with y value -8/9.

Check out this project for how to draw general function graphs with Python Turtle.