At My Fingertips

Documentation

Abstraction

Have a look at these geometric figures:

ABC
ABC

We could call them rounded rectangles. Here are some similarities:

  • They all are rectangles.
  • They all have four rounded corners.

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:

Decomposition

Here is one way to decompose a rounded rectangle:

decomposed rectangle

The Rounded Edge

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 PyTamaro iconRounded Edge to create one.

Loading...

The Rounded Rectangle

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.

Loading...

Play With Parameters

Play with your rounded_rectangle function, creating rounded rectangles with different sizes, corner radii, and colors.

Loading...

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).

Protect Against Unacceptable Arguments

You just played with some rather extreme argument values. What happens if you go beyond those?

Loading...

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.

Save in Toolbox

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

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)