Harriss Spiral Py5

The following demo is a port of Harriss Spiral from Processing to Py5; a Thonny editor is required. Uses modified Turtle Graphics with recursion.

# Reference for Turtle Graphics in Processing
# https://gist.githubusercontent.com/nataliefreed/8483050/raw/9e3f1d0f44bcb0c872762e4b984358d375e7b5fa/turtle.pde

orientation = radians(0)
end = 0.0
loc = 0.0
newLoc = 0.0
h = 600.0
w = (h/1.325)/1.325
rotation = 45
showLines = False

def harriss(iteration):
    global w
    
    if(iteration > 0):
        iteration -= 1
        fd(w,iteration)
        lt(radians(90))
        w = w/1.325
        harriss(iteration)
        
def setup():
    global loc
    global showLines
    
    size(700, 600)
    surface = get_surface()
    surface.set_title("Harriss Spiral")
    showLines = False
    loc = Py5Vector(width/2 - 150,height/2 + 100)
    no_loop()
    
def draw():
    harriss(8)
    
# ======= Turtle Graphics Functions =========
    
def fd(w,iteration):
    global loc
    global newLoc
    global end
    global rotation
    
    polar(w,orientation)
    end = loc + newLoc

    print("loc = ",loc)
    print("newLoc =",newLoc)
    print("end = ",end)
    print("====================")
    if(showLines):
        stroke(0)
        stroke_weight(1)
        line(loc.x,loc.y,end.x,end.y)
    
    no_fill()
    stroke(0,0,255)
    stroke_weight(12);
    if ((iteration == 7) or (iteration == 3)):
        arc(loc.x + w/2, loc.y - w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));

    if ((iteration == 6) or (iteration == 2)):
        arc(loc.x - w/2, loc.y - w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));
  
    if ((iteration == 5) or (iteration == 1)):
        arc(loc.x - w/2, loc.y + w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));
  
    if ((iteration == 4) or (iteration == 0)):
        arc(loc.x + w/2, loc.y + w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));
          
    loc = end
    rotation -= 90
        
def lt(theta):
    global orientation
    orientation += theta
    println("new orientation =",orientation)
    
def polar(r,theta):
    global newLoc
    newLoc = Py5Vector(r*cos(theta),r*sin(-theta))

Output:

MultiSpiral Design:

# Reference for Turtle Graphics in Processing
# https://gist.githubusercontent.com/nataliefreed/8483050/raw/9e3f1d0f44bcb0c872762e4b984358d375e7b5fa/turtle.pde

end = 0.0
loc = 0.0
newLoc = 0.0
h = 600.0
w = (h/1.325)/1.325
orientation = radians(90)
rotation = -45
showLines = False

def harriss(iteration):
    global w
    
    if(iteration > 0):
        iteration -= 1
        fd(w,iteration)
        lt(radians(90))
        w = w/1.325
        harriss(iteration)
        
def setup():
    global loc
    global showLines
    
    size(800, 750)
    surface = get_surface()
    surface.set_title("Harriss Spiral Design")
    showLines = False
    loc = Py5Vector(width/2 + 100,height/2 + 200)
    no_loop()
    
def draw():
    global orientation
    global rotation
    global h
    global w

# Base Spiral
    harriss(7)
# Right Upper Spiral
    jumpTo(495, 230)
    h = 300.0
    w = (h/1.325)/1.325
    orientation = radians(0)
    rotation = 45
    harriss(8)
# Left Upper Spiral
    jumpTo(240, 238)
    h = 250
    w = (h/1.325)/1.325
    rotation = -45
    orientation = radians(90)
    harriss(7)
# Left Lower Spiral
    jumpTo(240, 425)
    h = 200
    w = (h/1.325)/1.325
    rotation = 225
    orientation = radians(180)
    harriss(6);
# Center Lower Spiral
    jumpTo(390, 425)
    h = 175
    w = (h/1.325)/1.325
    rotation = 135
    orientation = radians(-90)
    harriss(5)    
    
# ======= Turtle Graphics Functions =========
    
def fd(w,iteration):
    global loc
    global newLoc
    global end
    global rotation
    
    polar(w,orientation)
    end = loc + newLoc

    if(showLines):
        stroke(0)
        stroke_weight(1)
        line(loc.x,loc.y,end.x,end.y)
    
    no_fill()
    stroke(random(255),0,random(255))
    stroke_weight(12);
    if ((iteration == 7) or (iteration == 3)):
        arc(loc.x + w/2, loc.y - w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));
    if ((iteration == 6) or (iteration == 2)):
        arc(loc.x - w/2, loc.y - w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));  
    if ((iteration == 5) or (iteration == 1)):
        arc(loc.x - w/2, loc.y + w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));  
    if ((iteration == 4) or (iteration == 0)):
        arc(loc.x + w/2, loc.y + w/2, w*1.414, w*1.414, radians(rotation), radians(rotation + 90));
          
    loc = end
    rotation -= 90
        
def lt(theta):
    global orientation
    orientation += theta

def jumpTo(x,y):
    global loc
    loc = Py5Vector(x,y)
    
def polar(r,theta):
    global newLoc
    newLoc = Py5Vector(r*cos(theta),r*sin(-theta))

Output:

3 Likes

Nice work, @svan ! Thank you for sharing this. The draw() function uses a reduced set of functions that would be great for beginner programmers to learn programming. Turtle Graphics programming is how I started with coding.

Also, you inadvertently uncovered a bug in py5…will open an issue on github.