In a previous project, you coded a square spiral inside squares. Make minor changes to make it a striped spiral as shown here.
Tag Archives: spiral
Striped Pentagon Spirals
In a previous project, we draw a pentagon spiral inside pentagons. Based on the solution, draw a striped pentagon spirals as shown with Python and Turtle. The modification to the code should be very small.
Dodecagon Spiral (Source Code)
In other related projects, we draw a pentagon spiral and a square spiral. Now draw a dodecagon spiral with 12 sides. Also make the grayscale of dodecagons gradually grow lighter as it gets smaller.
Source Code:
import turtle
import math
screen = turtle.Screen()
screen.title('Dodecagon Spiral - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
def draw_spiral(x,y,r,direction):
if r < 10: return
d = direction
r2 = r*math.cos(math.radians(360/24))/math.cos(math.radians(360/24-alpha))
dist = r*math.sin(math.radians(360/24))-r2*math.sin(math.radians(360/24-alpha))
turtle.up()
px = x + r*math.cos(math.radians(d))
py = y + r*math.sin(math.radians(d))
turtle.goto(px,py)
turtle.color((1-r/900,1-r/900,1-r/900))
turtle.down()
d += 360/12
for _ in range(12):
px = x + r*math.cos(math.radians(d))
py = y + r*math.sin(math.radians(d))
turtle.goto(px,py)
d += 360/12
draw_spiral(x,y,r2,direction+alpha)
alpha = 3
draw_spiral(0,0,900,90)
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.
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.
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.
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.
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)
Colored Spiral of Spirals with Python Turtle (Source Code)
Just for fun, draw a colored version of spiral of spirals using the colorsys library.
Source Code: (This may a few minutes)
import turtle
import colorsys
screen = turtle.Screen()
screen.title('Colored Spiral of Spirals Fractal - 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,length,direction):
L = length
c = 0
color=colorsys.hsv_to_rgb((y+900)/1800,1,0.8)
turtle.color(color)
while length>1 or c<20:
if length>2:
draw_spiral(x,y,length*0.255,160+direction)
turtle.up()
turtle.seth(direction)
turtle.goto(x,y)
if length <= 2:
turtle.down()
turtle.fd(length)
x,y = turtle.xcor(), turtle.ycor()
length *= 0.93
direction += 20
c += 1
LL=300
draw_spiral(500,-500,LL,90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")
Spiral of Spirals Fractals 2 with Python Turtle (Source Code)
Modify spiral of spirals such that the direction of sub spirals start from almost the opposite direction to the current spiral.
Source Code: (This code may run a few minutes)
import turtle
screen = turtle.Screen()
screen.title('Spiral of Spirals Fractal - 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,length,direction):
L = length
c = 0
while length>1 or c<20:
if length>2:
draw_spiral(x,y,length*0.255,160+direction)
turtle.up()
turtle.seth(direction)
turtle.goto(x,y)
if length <= 2: turtle.down()
turtle.fd(length)
x,y = turtle.xcor(), turtle.ycor()
length *= 0.93
direction += 20
c += 1
draw_spiral(500,-500,300,90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")
Spiral of Spirals Fractal with Python Turtle (Source Code)
Draw the following spiral that consists of spirals. Check out this project (spiral of squares) first before working on this one.
Source Code: (This program may runs for minutes)
import turtle
screen = turtle.Screen()
screen.title('Spiral of Spirals Fractal - 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,length,direction):
L = length
for _ in range(50):
if length>5:
draw_spiral(x,y,length*0.27,direction-30)
turtle.up()
turtle.seth(direction)
turtle.goto(x,y)
if length <= 5: turtle.down()
turtle.fd(length)
x,y = turtle.xcor(), turtle.ycor()
length *= 0.93
direction += 20
draw_spiral(-800,700,400,-90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")