""" Author: Sohie Lee Purpose: Recursion Lab Solutions: drawing with turtles recursively and returning values Filename: recursiveTurtles.py """ from turtle import * # Provided helper function # You can, but don't need to, change anything in this function def initializeTurtle(): ''' Initialize turtle on Canvas before drawing ''' # Create a turtle window setup(800, 600) reset() pencolor('black') # Set the speed (1=slowest, 6=normal, 10=fast, 0=No animation) speed(1) # Move the turtle from it's default start point (center) pu() setx(-200) sety(-200) pd() # Magical statements to make turtle window come to top of screen getscreen()._root.attributes('-topmost', True) getscreen()._root.attributes('-topmost', False) # ------------------------------------ # Task 1. fruitfulRow # ------------------------------------ def fruitfulRow(size, shrinkFactor, minimumSize): if size < minimumSize: return 0 else: square(size) fd(size) numberBoxes = 1 + fruitfulRowTuple(size*shrinkFactor, shrinkFactor, minimumSize) bk(size) return numberBoxes # ------------------------------------ # Hint for task 2 # ------------------------------------ def pad(x, y, padding): return x + padding, y + padding #x, y = pad(300, 200, 10) #print x # 310 #print y # 210 def fruitfulRowTuple(size, shrinkFactor, minimumSize): if size < minimumSize: return 0,0 else: square(size) fd(size) boxes, distance = fruitfulRowTuple(size*shrinkFactor, shrinkFactor, minimumSize) bk(size) return 1 + boxes, (4 * size) + distance # ------------------------------------ # Task 3. nestedSquares # ------------------------------------ def nestedSquares(size, shrinkFactor, minSize, color): """ Draws a series of nested squares starting w/ dimensions `size` x `size, reducing by `shrinkFactor` each time and stopping when the next would be less than `minSize`. Returns a tuple of the total number of squares drawn and "length travelled" by the turtle. """ pencolor(color) # Base case if size <= minSize: # No squares are made and no length is travelled in the base case return 0, 0 else: # Draw a single square fd(size) lt(90) fd(size) lt(90) fd(size) lt(90) fd(size) lt(90) # Recursion squaresDrawn, lengthTravelled = nestedSquares(size * shrinkFactor, shrinkFactor, minSize, color) # Return the ongoing totals return squaresDrawn + 1, lengthTravelled + (size * 4) # ------------------------------------ # Task 4. windows (squares at 2 corners) # ------------------------------------ def windows(size, shrinkFactor, minSize, color): pencolor(color) # Base case if size <= minSize: # No squares are made and no length is travelled in the base case return 0, 0 else: # Draw corner fd(size) lt(90) fd(size) lt(90) # Recursion A squaresDrawnA, lengthTravelledA = windows(size * shrinkFactor, shrinkFactor, minSize, color) # Draw corner fd(size) lt(90) fd(size) lt(90) # Recursion B squaresDrawnB, lengthTravelledB = windows(size * shrinkFactor, shrinkFactor, minSize, color) # Return the ongoing totals return squaresDrawnA + squaresDrawnB + 1, lengthTravelledA + lengthTravelledB + (4 * size) # ------------------------------------ # Task 5. boxes (squares at 4 corners) # ------------------------------------ def boxes(size, shrinkFactor, minSize, color): pencolor(color) # Base case if size <= minSize: # No squares are made, no length is travelled in the base case return 0, 0 else: # Draw a side fd(size) lt(90) # Recursion A squaresDrawnA, lengthTravelledA = boxes(size * shrinkFactor, shrinkFactor, minSize, color) # Draw a side fd(size) lt(90) # Recursion B squaresDrawnB, lengthTravelledB = boxes(size * shrinkFactor, shrinkFactor, minSize, color) # Draw a side fd(size) lt(90) # Recursion C squaresDrawnC, lengthTravelledC = boxes(size * shrinkFactor, shrinkFactor, minSize, color) # Draw a side fd(size) lt(90) # Recursion D squaresDrawnD, lengthTravelledD = boxes(size * shrinkFactor, shrinkFactor, minSize, color) # Totals totalSquaresDrawn = 1 + squaresDrawnA + squaresDrawnB + squaresDrawnC + squaresDrawnD totalLengthTravelled = (size * 4) + lengthTravelledA + lengthTravelledB + lengthTravelledC + lengthTravelledD return totalSquaresDrawn, totalLengthTravelled def run(): initializeTurtle() # Task 1 #print row(300, 30) # 2 #print row(90, 20) # 1 # Task 2 #print rowImproved(300, 30) # (2,240) #print rowImproved(60, 10) # (1,40) # Task 3 #print nestedSquares(400, 0.2, 100, 'red') # 1, 1600 #print nestedSquares(400, 0.4, 100, 'orange') # 2, 2240 #print nestedSquares(400, 0.75, 100, 'green') # 5, 4881 #print nestedSquares(100, 0.8, 20, 'blue' ) # 8, 1664 # Task 4 #print windows(64, 0.4, 20, 'red') # 3, 460 #print windows(160, 0.4, 20, 'orange') # 7, 1561 #print windows(400, 0.4, 20, 'green') # 15, 4723 #print windows(400, 0.75, 100, 'blue') # 31, 21100 # Task 5 #print boxes(400, 0.4, 100, 'red') # 5, 4160.0 #print boxes(400, 0.33, 30, 'magenta') # 21, 6499.84 #print boxes(400, 0.25, 15, 'green') # 21, 4800.0 #print boxes(400, 0.4, 50, 'blue') # 21, 8256.0 if __name__ == "__main__": run()