At My Fingertips

Rapid Playground

In this activity you will implement functions to create different kinds of triangles. You will add these functions to your toolbox so you can reuse them in future activities.

`triangle`

FunctionThe PyTamaro library is easy to learn because it only contains a small number of functions. For example, it just provides one general function to draw a triangle, triangle:

`def triangle(side1: float, side2: float, angle: float, color: Color) -> Graphic`

To construct a triangle with that function,
you need to specify the lengths of two of its sides and the angle between them.
That's powerful enough to construct **every** possible triangle!

Try to create a triangle that roughly looks like the one above:

Loading...

Many triangles you see in practice are special cases.

You can classify triangles either based on their sides, or based on their angles.

Based on the lengths of their sides, we can distinguish between three possible kinds of triangles:

**equilateral**triangles â all sides have the same length**isosceles**triangles â two sides have the same length**scalene**triangles â all sides have different lengths

Let's create an example of each:

Loading...

Based on their angles, we can divide triangles into the following three categories:

**acute**triangles â all angles are less than 90 degrees**right**triangles â an angle is 90 degrees**obtuse**triangles â an angle is greater than 90 degrees

Remember: the internal angles of a triangle add up to 180 degrees.

Let's create an example of each:

Loading...

Let's create functions for some of these special cases. In particular, we are interested in cases where we can fully specify a triangle with fewer parameters (i.e., where we only need to provide one or two parameters, plus the color):

- equilateral triangles
- isosceles triangles
- right triangles

Implement the functions in the following sections, and store each of them in your toolbox. They should come in handy in the future.

An equilateral triangle has all sides of the same length and all angles of 60Â°.

Loading...

Does it look right?
Then add your `equilateral_triangle`

function to the toolbox
by clicking the toolbox icon at the bottom right of the above code cell.

An isosceles triangle has two sides with the same length.

Loading...

You may have to twist your head a bit to see that this triangle indeed is isosceles: the horizontal side, and the side going from the bottom left towards the top right should have the same lengths.

If you're convinced you got it right, add your `isosceles_triangle`

function to your toolbox.

A right triangle has an angle of 90Â°.

Loading...

Add your `right_triangle`

function to your toolbox.
It most probably will come in handy later.

You probably already know about all these different kinds of triangles. But you learned how to implement functions that allow someone to more easily create them. And you added all three functions to your toolbox, so your personal collection of useful functions should have grown quite a bit.

From now on, if you need to create an equilateral triangle, you can write...

`equilateral_triangle(side, color)`

...instead of...

`triangle(side, side, 60, color)`

If you need an isosceles triangle, you can write...

`isosceles_triangle(side, angle, color)`

...instead of...

`triangle(side, side, angle, color)`

And if you need a right triangle, you can write...

`right_triangle(side1, side2, color)`

...instead of...

`triangle(side1, side2, 90, color)`

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

Triangles

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

Privacy Policy â˘ Platform Version 79aacf0 (Fri, 08 Nov 2024 13:24:29 GMT)