# Spiral.py # Charles Van Loan # February 14, 2015 """ Displays discrete logarithmic spirals """ from simpleGraphicsE import * import math def DrawSpiral(N,t,c1,c2,c3): """ Draws a spiral with N edges, turn angle t, and colors c1, c2, and c3. PreC: N is a positive int, t is float that specifies the turn angle in degrees, and c1, c2, and c3 are rgb arrays. """ # (xc,yc) is the start of the kth edge xc = 0 yc = 0 for k in range(N): # Draw the kth edge. # The rotation angle and length theta = k*t L = k+1 # Determine its color if k%3==0: c = c1 elif k%3==1: c = c2 else: c = c3 DrawLineSeg(xc,yc,L,theta,linecolor=c) # Determine the starting endpoint of the next edge. xc = xc + L*math.cos(theta*math.pi/180.) yc = yc + L*math.sin(theta*math.pi/180.) def SpiralRadius(N,t): """ Returns the radius of the spiral with N edges and turn angle t in degrees. PreC: N is a positive int that specifies the number of edges in a spiral and t is a float that specifies the turn angle in degrees. """ # (xc,yc) is the starting endpoint of the kth edge. xc = 0 yc = 0 # r is used to keep track of the maximum distance r = 0 for k in range(N): # the rotation angle and length of edge k. theta = k*t L = k+1 # Determine the starting endpoint of the next edge. xc = xc + L*math.cos(theta*math.pi/180.) yc = yc + L*math.sin(theta*math.pi/180.) # Standing at (xc,yc), are we further away from the origin # than at any previous time? dist = math.sqrt(xc**2+yc**2) if dist>r: # Yes! So revise r upwards... r = dist return r if __name__ == '__main__': # First spiral MakeWindow(350,labels=False,bgcolor=BLACK) DrawSpiral(300,62,PURPLE,RED,YELLOW) # Second Spiral N = 200 t = 118 r = SpiralRadius(N,t) c1 = CYAN c2 = MAGENTA c3 = YELLOW MakeWindow(r,labels=False,bgcolor=BLACK) DrawSpiral(N,t,c1,c2,c3) ShowWindow()