@extends('template') @section('title') Lab 12: More fruitful recursion w/ Turtle @stop @section('content') # Lab 12, Part 3: More Fruitful Turtle Recursion For this part of lab, open the file `moreFruitfulTurtles.py`. This is where you will add your code. ## Task 1. windows Create a function called `windows` that produces the fruitful recursive square patterns shown below: `windows(64, 0.4, 20, 'magenta')` **3 boxes drawn, 460.8 total length** ```xml 256 (64 * 4 sides) + 102.4 (25.6 * 4 sides) + 102.4 (25.6 * 4 sides) ======= 460.8 total length ``` It should return a tuple containing the number of squares drawn and the total distance traveled by the turtle. #### How to approach this design **Do not use a `square` helper function in this task**— the squares in the pattern should be drawn as a side effect of moving the Turtle into position between recursive calls (as shown in the following video). In order to do this, think of the fundamental piece of this design not as a single square, but as a corner of a square (repeated 2x via recursion to make a complete square). Here's a video demonstrating this approach: ```py windows(160, 0.4, 20, 'orange') ``` #### More examples #### Example B. `windows(160, 0.4, 20, 'magenta')` **7 boxes drawn, 1561.6 total length** #### Example C. `windows(400, 0.4, 20, 'magenta')` **15 boxes drawn, 4723.2 total length** #### Example D. `windows(400, 0.75, 100, 'black')` **31 boxes drawn, 21100.0 total length** ## Task 2: Boxes (with clipped corners) Write a fruitful recursive function called `boxes` that takes the following four parameters and produces squares in all four corners. 1. the **length** of the largest square 2. the **shrink factor** of the squares drawn at each of the four corners 3. the **length of an edge of the smallest outer box** 4. the **color** of the Turtle's pen
`boxes` should also return a tuple containing the number of squares drawn and the total distance traveled by the turtle. Once again, avoid approaching this design by trying to draw complete squares. Instead, the fundamental pattern of the design can be broken down to a single side (repeated x4 via recursion to eventually make a square). Here's a video demonstrating this approach: ```py boxes(400, 0.4, 100, 'red') ``` #### Example A. ```py boxes(400, 0.4, 100, 'magenta') ``` __Observations__ + This example produces total of 5 boxes (1 outer box + 4 nested ones) and a total length of 4160. + The largest box, has side length of __400__. + In each corner, there is a smaller box drawn with a side length of __160__ (`400 * 0.4 = 160`). + There are no additional boxes drawn because the next set of corner boxes have a side length of 64 (`160 * 0.4 = 64`), which less than the smaller outer box length of __100__. __Calculations__ ```xml 2560 (4 smaller boxes with side length 160 x 4) + 1600 (1 outer box with side length 400 x 4) ========== 4160 ``` #### Example B. ```py boxes(400, 0.33, 30, 'magenta') ``` 21 boxes, 6499.84 total length Here's a diagram that shows the flow of how the recursive method boxes invokes itself (the first image is enlarged to show detail): #### Example C. ```py boxes(400, 0.25, 15, 'magenta') ``` 21 boxes, 4800 total length #### Example D. ```py boxes(400, 0.4, 50, 'magenta') ``` 21 boxes, 8256 total length #### Example E. ```py boxes(400, 0.44, 30, 'magenta') ``` 85 boxes, 18095.0016 total length #### Example F. ```py boxes(400, 0.4, 10, 'magenta') ``` 1365 boxes, 42072.576 total length @include('/labs/lab12/_toc') @stop