At My Fingertips
Rapid Playground
Have a look at these five graphics:
Yes, they are similar because they all are ladybugs. Each ladybug has two red wings, a black head, and a black body. Each wing consists of a red semicircle containing three black dots, and the head includes two eyes.
The difference is the angle between the wings.
Let's create a function to create a ladybug. The function takes the wing angle as a parameter.
def ladybug(wing_angle: float) -> Graphic:
Let's also make it possible to create a ladybug of an arbitrary size by adding another parameter for the diameter
.
def ladybug(diameter: float, wing_angle: float) -> Graphic:
A ladybug is basically made up of four components: a head, a body, and two wings.
The simplest part is the body. It's simply a black circle.
The head can be further decomposed: it consists of the black circle and two eyes. Each eye consists of a black circle inside a white circle. Let's create separate functions to create the specific components.
The pupil diameter should be 0.6 of the eye diameter.
The eye diameter should be 0.35 of the head diameter.
Let's first focus on an individual wing:
The dot diameter should be 0.35 times the wing radius.
Hint: Compose the dots by creating two rows: the top row contains two dots, and a dot-sized gap in-between, the bottom row contains a single dot.
Now let's compose two wings, with a desired angle between them.
The wing_angle
specifies the rotation angle of an individual wing;
the angle between the two wings is twice that.
Hint: Pin the top wing at its bottom left, and the bottom wing at its top left. Then rotate each wing by +/- the given angle. Then compose the two wings. Use compose for that, so the pinning position of the composition remains at the hinge between the two wings.
Let's compose the components into a complete ladybug!
Call the head
, body
, and wings
functions to produce the corresponding parts.
The ladybug diameter corresponds to the diameter of its body.
The head diameter is half the ladybug diameter,
and the wing radius is half the ladybug diameter, too.
You practiced decomposition by breaking down the problem into multiple functions, and you practiced abstraction, because the functions have parameters and thus a function can produce an entire family of similar graphics.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Ladybug
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version e1c4550 (Sun, 23 Feb 2025 15:42:54 GMT)