At My Fingertips
Rapid Playground
Have a look at these geometric figures:
A | B | C | D |
---|---|---|---|
We could call them popsicles. Here are some similarities:
The popsicles differ in:
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:
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.
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 Rounded Edge activity
to create one.
Then import your rounded_edge
function from your toolbox.
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.
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.
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
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy ⢠Platform Version c08406b (Wed, 20 Nov 2024 12:30:00 GMT)