# SpiralSol.py """ Display Spirals """ from SimpleGraphics import * from math import cos, sin, pi def DrawRing(n,r,alpha,c1,c2): """ Draws a radius r ring with n spokes and colors c1 and c2. The spoke angle is alpha degrees. PreC: n is a positive int, r is a positive float, alpha is a positive int, and c1 and c2 are rgb lists. """ for k in range(n): # Determine the endpoints (x1,y1) and (x2,y2) of the # k-th spoke... tau = k*alpha*pi/180 x1 = r*cos(tau) y1 = r*sin(tau) tau = (k+1)*alpha*pi/180 x2 = r*cos(tau) y2 = r*sin(tau) # Draw the k-th spoke... if k%2==0: DrawLineSeg(x1,y1,x2,y2,LineColor=c1) else: DrawLineSeg(x1,y1,x2,y2,LineColor=c2) def DrawSpiral(n,r,alpha,c1,c2,p): """ Draw a radius r spiral with p rings. The spoke angle for each ring is alpha degrees and c1 and c2 are the spoke colors. The outer ring has n spokes PreC: r is a positive float, alpha is a positive int, and c1 and c2 are rgb lists, and p is a positive int, and n and int that is a power of 2. """ for k in range(p): # Draw the k-th ring DrawRing(n,r,alpha,c1,c2) # Update the radius and the number of spokes r = r*cos(alpha*pi/360) n = n/2 if __name__ == '__main__': MakeWindow(6.5,bgcolor=BLACK,labels=False) r = 6 n = 256 p = 8 alpha = 87 c1 = CYAN c2 = MAGENTA DrawSpiral(n,r,alpha,c1,c2,p) s = 'n = %3d p = %3d alpha = %3d ' % (n,p,alpha) Title(s,FontSize=24) ShowWindow()