At My Fingertips
Rapid Playground
Let's write a function to produce traffic cones of arbitrary sizes!
You might have identified five trapezoids and an orange rectangle at the bottom. The top trapezoid is orange, the one below is whitish (almost white), the next orange, and so on.
The trappezoids are isosceles (both legs have the same length).
PyTamaro does not have a function to create isosceles trapezoids.
You could write such a function right here
(by decomposing an isosceles trapezoid into smaller components),
but we recommend you complete the
Isosceles Trapezoid activity,
to create that function and to store it in your toolbox.
Once you are done with that,
import the isosceles_trapezoid
function from your toolbox.
Now let's implement a function that can produce a fixed-size traffic cone, with a total height of 220.
Here is a sketch showing the key measurements:
You need the length of the top and bottom bases and the height of each isosceles triangle. You also need the width and height of the rectangle.
We would like to have a function that can produce a traffic cone of any size. Assume we would like to provide the overall height of the cone as a parameter to the function:
def traffic_cone(height: float) -> Graphic:
Every other measure is derived from that height.
This is done in such a way that for traffic_cone(220)
we get the same graphic
as for fixed_size_traffic_cone
,
and all the measures scale proportionally for other heights.
Think how to determine the arguments for the isosceles_trapezoid
and rectangle
function calls based on the overall height.
If you like, you can store your traffic_cone
function in your toolbox.
Note that the traffic_cone
function is probably less generally useful
(it is quite specialized)
than the isosceles_trapezoid
function
(which is more abstract).
Lots of graphics can be decomposed into trapezoids.
But there are probably only a few graphics that contain traffic cones
with two white and three orange stripes
in the exact colors you picked.
The more generally useful a function is, the more valuable it is to have it in your library.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Simple Traffic Cone
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy ⢠Platform Version 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)