At My Fingertips
Rapid Playground
Have a look at these graphics:
A | B | C | D | E | F |
---|---|---|---|---|---|
Here are some similarities:
The graphics all look like simple beach umbrellas seen from the top.
Let's develop a function that embodies those similarities:
def simple_beach_umbrella(...) -> Graphic:
We can see that the graphics differ in:
We could encode these differences into the following parameters:
def simple_beach_umbrella(triangle_pairs: int, color1: Graphic, color2: Color) -> Graphic:
Note that the number of triangles has to be even
so that we can alternate between the two colors.
For this reason we do not provide a triangles
parameter,
but a triangle_pairs
parameter.
This makes it impossible to provide an illegal argument
(except, of course, if one provided a negative number).
The number of triangles is simply twice the number of triangle_pairs
.
How would you decompose an umbrella?
We could produce it from two sets of triangles:
a set of triangle_pairs
triangles of the first color,
and a set of triangle_pairs
triangles of the second color.
First set of triangles | Second set of triangles | Second set of triangles, rotated |
---|---|---|
Each set of triangles of course is composed of... triangles. So let's first focus on building the necessary triangles.
Here are some of the triangles needed to compose the above umbrellas:
A | B | C | D | E | F | G |
---|---|---|---|---|---|---|
We want to program a function that creates a single triangle. Instead of calling it a triangle, let's call it a "sector".
As we noted before, all of them are isosceles triangles with the same leg length. Moreover, they all are oriented so that the base is vertical on the right, and the tip is on the left.
They differ in their color, the lengths of their bases, and the angles. We can simply focus on the angle at the tip: it fully defines the triangle given that we know the fixed length of its two legs.
If you already have an isosceles_triangle
function in your toolbox, import that.
Otherwise, import the general triangle function.
rotate the triangle appropriately,
and pin it at its tip (center_left).
Given the call to show_graphic with True
,
you should see the bounding box and the pin.
The pin should be at the center left of the sector.
We now need to compose sectors into umbrellas. As discussed above, we can compose two sets of sectors, one for each color. A set of triangles of a single color can be seen as a ring.
Ring of dots | Ring of sectors |
---|---|
To get the umbrella, the radius of the ring (that is, the inner radius) needs to be 0, so that the sectors touch each other at their tips.
If you don't already have one, implement the ring
function in the Simple Rings activity.
Now we have the key ingredients to build umbrellas!
Now that you have a working simple_beach_umbrella
function,
it's time to create some umbrellas.
Pick or create your favorite color combinations
(we provide some colors to get you started).
Can you use it to create an umbrella with a single color?
Feel free to add extra features: a tip (e.g., small white circle) in the center, decorations on the sectors, or even composite sectors of smaller and smaller radius. Design your very own unique umbrella!
You practiced building abstractions, specifically, the sector
and the simple_beach_umbrella
functions.
You did this by going from a few concrete examples to the abstract function,
by identifying the similarities and differences between the examples.
Moreover, you reused an abstraction:
the ring
function you developed in a prior activity.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Simple Beach Umbrellas
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version 19a6bb6 (Mon, 27 Jan 2025 12:33:26 GMT)