@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__:
`cake(75, 200, 'blue'')` draws this cake and should return __22500__:
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.
_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__:
`cake(75, 200, 'blue'')` draws this cake and should return __26250__:
__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)
|
|
|
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__:
`cakeRow(70, 100)` draws these cakes and should return __36750__:
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__:
`shrinkingCakeRow(40, 100)` draws these cakes and should return __9187.5__:
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