At My Fingertips

Documentation

Popsicle

Abstraction

Have a look at these geometric figures:

ABCD
ABCD

We could call them popsicles. Here are some similarities:

  • The ice at the top consists of horizontal stripes.
  • The top and bottom edge of the ice are rounded.
  • Both corners of the top edge have the same radius.
  • Both corners of the bottom edge have the same radius.
  • The wood-colored stick at the bottom has a semicircle at the bottom.

Parameterize

The popsicles differ in:

  • the width and height of the stick
  • the width and height of the ice
  • the number of ice stripes
  • the colors of the various stripes
  • the top and bottom edge radii

We can encode this in parameters, for example as follows:

def popsicle(
  width: float, length: float, top_radius: float, bottom_radius: float,
  stripe_colors: list[Color], stick_width: float, stick_length: float
) -> Graphic:

Decompose

We could break a popsicle into the ice at the top and the stick at the bottom.

The ice can be broken into N horizontal stripes, each with the same height, but a possibly different color. The top and bottom stripes are special: they have the same overall heights, but they have a rounded edge.

The stick has a semicircle at the bottom.

Import Rounded Edge Function

To create the top and bottom stripe of ice, we need a rounded edge.

If you do not yet have a rounded_edge function in your toolbox, solve the PyTamaro iconRounded Edge activity to create one.

Then import your rounded_edge function from your toolbox.

Loading...

Implement

Now let's write some code!

Design Tip: You may want to create separate functions to compose the stick, the ice, and maybe even a stripe of ice.

Hint: To compose the ice, map the colors into stripe graphics, and then reduce the stripe graphics into a single ice graphic. You can use a loop, a for-comprehension, or the map function for the mapping. You can use a loop or the reduce function for reduction. If you prefer, you can fuse the two repetitions into a single repetition, in which you map and reduce in one pass.

Loading...

Play With Parameters

Play with the parameters of your function to create your favorite popsicles. Create your own colors, like one for grape ice, or one for white chocolate.

Loading...

Save in Toolbox

If you like your popsicle function, save it in your toolbox. Chances are you will use it as a component in another graphic at some time.


This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Popsicle

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 0d7866a (Fri, 20 Dec 2024 07:46:38 GMT)