At My Fingertips
Rapid Playground
Have a look at these geometric figures:
A | B | C |
---|---|---|
We could call them rounded rectangles. Here are some similarities:
Now let's focus on their differences,
so we can figure out what parameters a rounded_rectangle
function should have.
def rounded_rectangle(...) -> Graphic:
We can see that the color differs,
so we want to have a parameter to specify the color
.
Besides the color, there's also the width, height, and corner radius.
def rounded_rectangle(width: float, height: float, corner_radius: float, color: Color) -> Graphic:
Here is one way to decompose a rounded rectangle:
The top and bottom components of the rounded rectangle are rounded edges.
If you don't have a rounded_edge
function in your toolbox,
complete Rounded Edge to create one.
Remember that the given width
and height
should be the total width and height of the rectangle.
Use the rounded_edge
function you just imported and the pytamaro functions rectangle, rotate and above.
Play with your rounded_rectangle
function,
creating rounded rectangles with different sizes, corner radii, and colors.
Yes, by passing the same value to width
and height
,
and half that value to corner_radius
.
Yes, by using a corner_radius
of 0.
Yes, by setting the corner_radius
to half the line thickness
(height of the rounded rectangle).
You just played with some rather extreme argument values. What happens if you go beyond those?
The code did not succeed. It failed with an exception.
Unfortunately, when someone who calls your function reads that error message,
it is not clear who to blame.
They might blame you!
They might say that your rounded_rectangle
function is buggy!
You can shift the blame to them! You can show them that it was their mistake! They called your function with the wrong arguments!
To do that, add assertions at the start of your function. Those assertions ensure that the argument values are acceptable. If the assertions fail, you can show the error message and tell the person who called your function that they violated your preconditions.
In our case, the rounded_rectangle
function
does not accept a corner radius
that is greater than half the shorter side.
Why?
Because it is unclear how such a shape should look like!
def rounded_rectangle(width: float, height: float, corner_radius: float, color: Color) -> Graphic:
assert width >= ...
assert height >= ...
...
Note: You really want fix that mistake now, because if you have any code cell in this activity that produces an error, you will not be able to move to the next activity.
Now save your rounded_rectangle
function in your toolbox.
It should come in handy as a component for more complex graphics in the future.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Rounded Rectangle
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)