At My Fingertips

Documentation

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.

PyTamaro's triangle Function

The 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

PyTamaro triangle function

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

Kinds of Triangles

Many triangles you see in practice are special cases.

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

By Side

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

By Angle

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

Special Triangle Functions

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

triangles

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

The Equilateral Triangle

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.

The Isosceles Triangle

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.

The Right Triangle

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.

What You Learned

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

Logo of PyTamaro

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

Privacy Policy • Platform Version 0d7866a (Fri, 20 Dec 2024 07:46:38 GMT)