At My Fingertips

Documentation

Simple Rings

Abstraction

Have a look at these graphics:

ABCDEF
redorangegreenbrownyellowblue

Here are some similarities:

  • All figures exhibit rotational symmetry
  • They all arrange identical pieces into a ring
  • They are all rotated so one of the pieces is on the right

Let's develop a function that embodies those similarities:

def ring(...) -> Graphic:

We can see that the graphics differ in:

  • the diameter of the ring
  • the number of pieces
  • the kind of piece they arrange

We could encode these differences into the following parameters:

def ring(diameter: float, count: int, piece: Graphic) -> Graphic:

Offset by Radius

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:

piece and spoke

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

Loading...
Loading...

Form a Ring

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

ring with spokes visible

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.

Loading...

Play!

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:

Loading...

Multiple Rings

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.

Loading...

Rings of Rings

A ring arranges several copies of a given graphic. Because a ring is itself a graphic, we can create a ring of rings:

ring of rings

Loading...

What You Learned

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

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 19a6bb6 (Mon, 27 Jan 2025 12:33:26 GMT)