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:

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:

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:

Table of Contents