At My Fingertips

Rapid Playground

Documentation

Striped Watermelon

In this activity you learn to compose multiple graphics and you practice creating functions.

Asking for a Graphic

Assume you have a friend who does not see the figure below:

image.png

You might have said something like "a striped watermelon". Not bad!

But what if you wanted to describe this to someone who has never seen a striped watermelon? In that case, maybe you could say something like:

"A tall but very narrow dark green ellipse overlaid over a slightly wider light green ellipse, overlaid over a slightly wider dark green ellipse, overlaid over a slightly wider light green ellipse, overlaid over a slightly wider dark green ellipse. All ellipses have the same height. The overall graphic has a width of 90 and a height of 100."

Green Ellipses

Let's first define the two green colors:

Loading...

RUN the above code cell. You get no output. Why? Because you simply create two colors and assign them to the names dark_green and light_green. You don't create any graphics, and, most importantly, you don't show any graphics.

So let's call PyTamaro iconshow_graphic! But to do that, we first need a graphic. Let's create a 200-by-100 dark green ellipse and pass it as an argument to show_graphic. Then create a 100-by-200 light green ellipse, and pass that as an argument to another call to show_graphic.

Loading...

Great! We got the colors right, and we know how to draw ellipses of different widths and heights.

Overlaying Several Ellipses

Now let's compose two ellipses using the function PyTamaro iconoverlay. Let's start inside-out: we place a dark green very narrow ellipse over a light green slightly wider ellipse. Let's make the heights 100. The width of the narrower ellipse is 10, the wider is 30.

Loading...

Great! Now let's overlay what we have on top of an even wider ellipse:

Loading...

Now let's do this two more times, to get a graphic that looks like the one at the top of the page.

Loading...

As you see above, you don't need to define a new name for each intermediate step. You can provide a rather complex expression as an argument to overlay. The outer call to overlay receives two arguments: the result from the inner call to overlay, and the result of the bottom call to ellipse.

We can go extreme and "inline" everything into a single expression (we should not overdo this, complex, deeply nested expressions can be hard to understand). Complete the following expression by replacing the ... with the appropriate widths.

Loading...

Let's Abstract!

You just created a striped watermelon, with a very specific size (a width of 90 and a height of 100). Wouldn't it be nice if we could create similar watermelons with different widths and heights?

Of course we can. We just have to write the same expression, with some different numbers.

But wouldn't it be nice if we could write this complex expression only once, give it a name, and then just use that name whenever we want to construct a striped watermelon? And if we could provide the desired width and height of the watermelon to be constructed?

We can! We can define a function that can produce striped watermelons given a desired width and height. Complete the function below, by replacing the ... with the appropriate numbers:

Loading...

What You Learned

You now know how to overlay graphics made of more than two elements. The overlay function can only overlay two graphics, but either of those two graphics could be composed by overlaying two other graphics. Graphics are composed from graphics, which are composed from graphics, and so on. Until you hit an ellipse, a rectangle, or another "primitive" graphic.

In terms of the Python programming language, you practiced:

  • defining a name (e.g., two_ellipses) to refer to a computed value (e.g., a graphic)
  • defining a function with parameters (e.g., striped_watermelon)
  • writing a complex computation either as one complex (deeply nested) expression, or as a sequence of statements, each statement computing a part and assigning the result to a name

Whenever you write an expression that contains literal values (like 90), you should consider abstracting the expression into a function with parameters. The expression will become the body of the function, and instead of the literal values, you will use parameters, so that the function can be called with any desired values.


This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Striped Watermelon

Logo of PyTamaro

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

Privacy PolicyPlatform Version 754785f (Fri, 03 May 2024 12:21:44 GMT)