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:
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:
Let's create an example of each:
Based on their angles, we can divide triangles into the following three categories:
Remember: the internal angles of a triangle add up to 180 degrees.
Let's create an example of each:
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):
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°.
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.
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°.
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 c08406b (Wed, 20 Nov 2024 12:30:00 GMT)