# Flakes.py # CS 1110 profs, Spring 2016 # May 2016 """ Checks out the implementation of the recursive procedure MakeFlake.""" from SimpleGraphics import * from ThePointClass import * from TheLineSegClass import * from math import sqrt def DrawFlake(F,c): """ Draws each line segment in F with LineColor c. PreC: F is a list of LineSeg objects and c is an rgb list """ for f in F: # f is a LineSeg DrawLineSeg(f.P1.x,f.P1.y,f.P2.x,f.P2.y,LineColor=c) def MakeFlake(n,p=0): """ Returns a list of LineSeg objects L that represents an n-Flake with rightSide probability p. L has the property that L[k].P2 and L[(k+1)%n].P1 represent the same point for k = 0,1,..,n-1 where n = len(L). "PreC: n is a nonnegative int, p is a float that satisfies 0<=p<=1. """ if n==0: # Base case. P0 = Point(0,1) P1 = Point(sqrt(3)/2,-.5) P2 = Point(-sqrt(3)/2,-.5) L = [LineSeg(P0,P1),LineSeg(P1,P2),LineSeg(P2,P0)] return L else: # Create a list of LineSeg objects that represent an # (n-1)-Flake with rightSide probability p L0 = MakeFlake(n-1,p) L = [] for f in L0: # f is a LineSg. Blip it and "add into" L... L.extend(f.Blipped(rightProb=p)) return L def main(): """ Illustrate MakeFlake for a number of different n and p. """ for n in range(5): MakeWindow(1.5,labels=False,bgcolor=BLACK) L1 = MakeFlake(n,p=0) DrawFlake(L1,MAGENTA) MakeWindow(1.5,labels=False,bgcolor=BLACK) L2 = MakeFlake(n,p=1) DrawFlake(L2,CYAN) s = 'n = %1d' % n Title(s,FontColor=BLACK,FontSize=18) MakeWindow(1,labels=False,bgcolor=BLACK) pval = .5 L3 = MakeFlake(4,p=pval) DrawFlake(L3,YELLOW) s = 'n = %1d p = %5.3f' % (n,pval) Title(s,FontColor=BLACK,FontSize=18) ShowWindow() if __name__ == '__main__': main()