At My Fingertips
Rapid Playground
Have a look at these graphics:
A | B | C | D | E | F |
---|---|---|---|---|---|
Here are some similarities:
Let's develop a function that embodies those similarities:
def ring(...) -> Graphic:
We can see that the graphics differ in:
We could encode these differences into the following parameters:
def ring(diameter: float, count: int, piece: Graphic) -> Graphic:
To arrange the pieces, we can offset a piece by composing it with a thin horizontal rectangle (a "spoke") that is as long as the ring's radius. Here is an example:
The left end of the spoke will be placed in the center of the ring. To achieve this, we pin the pushed piece on the left end of the spoke (we can see the pinning position in the above graphic in black).
Now let's implement our ring
function.
We need to compose count
pieces into one composite graphic.
This works by creating count
rotated spokes and composing them into a result.
Here is an example:
ring(90, 5, rectangle(20, 20, blue))
The resulting graphic should look like this (except that it should not show the green spokes):
We initialize a variable (result_so_far
) with an empty graphic,
and we iterate count
times through a for-loop.
In each iteration, we produce a piece rotated by the appropriate angle
and compose that piece with the result_so_far
.
Now that you have a working ring
function, it's time to create some rings.
Can you use your function to produce the six example rings shown at the top? Here is one to get started:
One ring is neat, but how about composing multiple rings?
The code below composes two rings. The red ring is rotated by half the angle between pieces.
A ring arranges several copies of a given graphic. Because a ring is itself a graphic, we can create a ring of rings:
You built a powerful abstraction:
a ring
function that can produce rings of arbitrary diameters
containing an arbitrary number of arbitrary graphics.
You also practiced the process of abstraction, going from a few concrete examples to the abstract function, by identifying the similarities and differences between the examples.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Simple Rings
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)