@extends('template') @section('title') Lab 3, Turtle Cake Baking @stop @section('content') # Lab 3 Bonus: Turtle Cake Baking (Functions with Python Turtles) Our goal in this part of the lab is to write a collection of fruitful functions that draw cakes using Python's turtles. Open `lab03/cake.py` to get started.
## Task 1: Two layer cake
Partner A
You are given a function called `cake` that takes height and width and draws a two-layer cake. The bottom layer are the given height and width, and the top layer is half the width and the same height as the bottom layer. `cake(80, 400, 'red')` draws this cake and should return __48000__: A two layer cake made out of two red-outlined rectangles with white fill stacked on top of each other, with the bottom one being twice as wide as the top one. The turtle (a small arrow) is in the middle of the bottom rectangle; facing to the right. `cake(75, 200, 'blue'')` draws this cake and should return __22500__: A two layer cake as above, but this time in blue and with the bottom layer the same width as the top layer of the first cake. The top layer of this cake is again half as wide as the bottom layer. A few notes about the `cake` function: + Note that the turtle always returns to its initial starting position at the end of the function. Make sure any edits that you make to this function still ensure that the turtle is back in its original position before the function was called. + Right now, the `cake` function always returns 0. You will change this to calculate the total area of the two cake layers, and return that value from your `cake` function. The areas for the two cakes above are shown. + Why should the red cake above return 48000? The bottom layer has dimensions 80 x 400, which has area 32000. The top layer has dimensions 80 x 200, which as area 16000. 32000 + 16000 = 48000. ###How to test? You can test the `cake` function in the Shell. Here are two examples. Testing cake function in shell. First, cake(50, 100, "pink") is called. This prints two lines of output desribing the rectangles it draws, which are "A pink 100 by 50 rectangle centered at (0, 0) with a horizontal long axis," and "A 5-pensize pink 50.0 by 50.0 square centered at (0, 50)." Then it has a result value, which is 7500.0. Next, setupTurtle() is called, which resets the canvas. Then cake is called again, with 100, 275, and "green" as arguments. This time it prints "A green 275 by 100 rectangle centered at (0, 0) with a horizontal long axis," and "A green 137.5 by 100 rectangle centered at (0, 100) with a horizontal long axis," and the result value is 41250.0. _Hint_: if you see overlapping cakes in your Turtle canvas, you can use `setupTurtle()` to clear the canvas between calls to your `cake` function (as shown above). ## Task 2: Three layer cake with a candle
Partner B
Now, make changes to your `cake` function so that it has a third layer. The third layer is the same height as the first two layers, and half the width of the second layer. Here are some sample 3-layer cakes: `cake(80, 400, 'red')` draws this cake and should return __56000__: A red three layer cake. The bottom two layers are the same structure as the two layer cake described above. Another layer (i.e., rectangle) half the width of the previous top layer is added on top, and on top of that, a thin blue vertical filled rectangle topped by an orange vertically-oriented ellipse represents a candle with a flame on top. `cake(75, 200, 'blue'')` draws this cake and should return __26250__: A narrower blue three layer cake. The bottom layer of this cake is the same width as the middle layer of the red cake above, and the two layers above that are each half the width of the previous layer. Again there is a "candle" on top made out of a rectangle and an ellipse. __Task 2A:__ Draw the third cake layer, and make sure your turtle is back in its original starting position when done. You'll know because you'll see the turtle in the center of the bottom cake layer. __Task 2B:__ Re-calculate the total area of the cake by adding in the area of the third layer. Check that your `cake` function returns the numbers shown above. Note we are not counting the area of the candle, only the area of the cake layers. __Task 2C:__ Add the candle to your third cake layer. You are given the `candle` function which draws a blue candle with an orange flame. You specify the height of the candle; its width is fixed at 20. Here are two invocations of the candle function:
candle(45) candle(100)
The result of calling candle(45): a blue 20 by 45 rectangle with a smaller orange ellipse on top. The turtle is visible as an orange arrow facing to the right centered within the blue rectangle. The result of calling candle(100): a blue 20 by 100 rectangle with an orange ellipse on top. The ellipse is larger thatn before, but still smaller than the rectangle. The turtle is once more visible in the center of the blue rectangle, facing right.
Note 1. The `candle` function does **not** return a value; it merely draws on the canvas. 2. The `candle` function puts the turtle back where it was before it was invoked. ## Task 3: A triplet of three-layer cakes in a row
Partner A
Write a new function called `cakeRow` that draws 3 cakes in a row, left to right where the rightmost edge of the first cake overlaps with the leftmost edge of the second cake. All cakes will have the same dimensions, and `cakeRow` will **return** the total area of all 3 cakes. The cakes are always red, green and blue from left to right. `cakeRow(50, 280)` draws these cakes and should return __73500__: Three three-layer cakes in a row, in red, green, and then blue. The cakes' bottom layers are touching end to end. The turtle is in the center of the first cake, facing to the right (which is the direction the other two cakes are positioned in. `cakeRow(70, 100)` draws these cakes and should return __36750__: Another group of three three-layer cakes, but this time each cake is narrower. Check that your `cakeRow` function returns the turtle to its starting position (centered in the bottom layer of the leftmost cake). ## Task 4: A triplet of diminishing three-layer cakes
Partner B
Write a new function called `shrinkingCakeRow` that draws 3 cakes in a row, left to right. It is still the case that, within any cake, the layers decrease in width by 50% and the height of all layers is the same. However, across the 3 cakes in this row, now the width and height will decrease. The second cake's bottom layer is half the width and half the height of the first cake's bottom layer. And the third cake's bottom layer is half the width and half the height of the second cake's bottom layer. `shrinkingCakeRow(80, 300)` draws these cakes and should return __55125__: Another row of three three-layer cakes, but this time, the second and third cakes in the row have shorter and narrower bottom (and thus subsequent) layers, as described above. `shrinkingCakeRow(40, 100)` draws these cakes and should return __9187.5__: Another row of three shrinking three-layer cakes, much smaller than the first row above. The candles for the second and third cakes in this row are actually wider than the row's top player. Note: + Check that your `shrinkingCakeRow` function returns the turtle to its starting position (centered in the bottom layer of the leftmost cake). + Note how the cakes are no longer aligned on the bottom edge + Note that the fixed candle width of 20 is too wide for the shrinking cakes when the first width is 100 or less. ##Yay, you made it to the end of the lab! If you still have time left, there are some optional exercises you could work on: - [More turtle drawing](turtleFunctions) will give more practice using parameters and writing turtle functions. - [Knowledge check](check) linked at the bottom of the page in the Table of Contents. Take this fun quiz to check your knowledge. @include('/labs/lab03/_toc') @stop