At My Fingertips
Rapid Playground
The PyTamaro library includes a function to create a triangle given the lengths of two sides and the enclosed angle.
The PyTamaro triangle function is only one of many possible ways to uniquely define a triangle. For example, sometimes we want to create a triangle given its three side lengths, or given two angles and a height.
Here we focus on functions to create triangles given their sides and their angles (we do not consider other properties, such as heights, or areas).
There are 7 ways to uniquely define a triangle given known sides and angles:
We use the name "triangle", followed by an underscore, followed by a three-letter suffix. The letter "s" means that a side length (a, b, or c) is given, the letter "a" means that an angle (alpha, beta, or gamma) is given. The order of the letters matters: the first letter corresponds the side a or angle alpha, the second to side b or letter beta, the third to side c or letter gamma.
There are 8 arrangements of 's' and 'a' of length three (2 * 2 * 2 = 8).
The table only shows 7; aaa
is missing.
We could try to implement a function triangle_aaa
,
but we couldn't possibly succeed.
Three angles do not uniquely define a triangle;
they define the "shape" of the triangle, but not its size.
One could scale that triangle to arbitrary sizes,
producing any number of different but similar triangles.
That three angles do not uniquely define a triangle also makes sense from the perspective of the angle sum property: the sum of the angles of a triangle is 180 degrees; thus, we need to know only two angles to be able to determine the third angle. Providing all three angles thus does not really provide more information than only providing two angles.
triangle_ass
The easiest of the seven function to implement,
given that we have to use the underlying PyTamaro triangle function,
is triangle_ass
. This function is very similar to PyTamaro's triangle function.
triangle_ass(alpha, b, c, color)
triangle(side1, side2, angle, color)
Make sure that the triangle ends up being oriented the same way as shown in the above figure, with angle alpha on the left, and side c horizontally on the top.
If your implementation is correct, this should produce the following triangles:
In your implementation, call triangle_ass
,
and first determine the arguments needed for that.
Hint: Use the law of cosines to flip from side a to angle alpha. Use acos to compute the arccosine in radians. Use degrees to convert from radians to degrees.
If your implementation is correct, this should produce the following triangles:
We already implemented triangle_ass
,
but we still need the other two functions
for creating triangles based on two sides and one angle.
When mapping from triangle_sss
to triangle,
the law of cosines helped us to flip a side to an angle,
given the two other sides.
To implement triangle_sas
and triangle_ssa
,
let's use that law in reverse:
to flip an angle to a side,
given the other two sides.
Make sure you convert the given angle to radians before calling cos.
If your implementation is correct, this should produce the following triangles:
If your implementation is correct, this should produce the following triangles:
Now let's implement the three functions that create triangles based on one side and two angles. Here we can't just use the law of cosines (which served us to flip between an angle and a side, given two other sides), because we only know one side.
However, the law of sines and the angle sum property can help!
If your implementation is correct, this should produce the following triangles:
If your implementation is correct, this should produce the following triangles:
If your implementation is correct, this should produce the following triangles:
In this activity you had to use three properties from trigonometry to implement a full family of functions for creating triangles given side lengths and angles.
The properties were:
You probably first encountered the angle sum property in middle school. The two laws are probably covered in your high school math class.
Such properties and laws you learn in school come in pretty handy when programming graphics. The above three trigonometric properties are the essence of the seven functions you just developed:
These functions are generally useful. We highly recommend you add them to your Toolbox before you move on, so you can use them in future activities.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Triangles by Sides & Angles
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version 19a6bb6 (Mon, 27 Jan 2025 12:33:26 GMT)