""" Author: Sohie Lee Purpose: Recursion Lab Solutions Filename: recursiveGraphics.py Drawing box patterns using recursion """ from cs1graphics import * def box(x, y, size, color1, canvas): ''' Provided function; Adds a box of size size to the location (x,y) on the given canvas ''' b = Rectangle(size,size,Point(x,y)) b.setFillColor(color1) canvas.add(b) # ------------------------------------ # Task 1 # ------------------------------------ def makeNestedBoxes(x, y, size, color1, color2, canvas): ''' Draws a set of nested boxes (boxes decrease by 10 each time ''' if size > 10: # Draw one box box(x,y,size,color1,canvas) # Swap colors and recurse to get smaller boxes makeNestedBoxes(x, y, size-10, color2, color1, canvas) if __name__ == "__main__": # Tests paper = Canvas(500, 500, 'darkolivegreen') makeNestedBoxes(200, 400, 100, 'red', 'ivory', paper) makeNestedBoxes(150, 100, 200, 'purple', 'gold', paper) makeNestedBoxes(400, 200, 180, 'navy', 'lightskyblue', paper) # ------------------------------------ # Task 2 # ------------------------------------ def nestedBoxPattern(x, y, size, color1, color2, canvas): if size > 10: # draw the center box makeNestedBoxes(x,y,size,color1,color2,canvas) # draw another (smaller) box in the upper left corner cornerUL = nestedBoxPattern(x-size/2., y-size/2., size/3., color1, color2, canvas) # draw another (smaller) box in the upper right corner cornerUR = nestedBoxPattern(x+size/2., y-size/2., size/3., color1, color2, canvas) # draw another (smaller) box in the lower left corner cornerLL = nestedBoxPattern(x-size/2., y+size/2., size/3., color1, color2, canvas) # draw another (smaller) box in the lower right corner cornerLR = nestedBoxPattern(x+size/2., y+size/2., size/3., color1, color2, canvas) if __name__ == "__main__": # Tests paper2 = Canvas(600, 600, 'white', 'paper2') nestedBoxPattern(300, 300, 350, 'navy', 'ivory', paper2)