21 Functions II: Calculating Triangles
21.1 Functions within functions
Python functions can not only process Python statements, but they can also call user-defined functions. The implementation of Heron’s formula will serve as an example.
21.1.1 Calculating a Triangle’s Area Using Heron’s Formula
To calculate the area \(T\) of a triangle, we use Heron’s formula
\[ T = \sqrt{s(s-a)(s-b)(s-c)} \]
where \(s\) denotes the semiperimeter. The semiperimeter is calculated by adding the lengths of the triangle’s sides and dividing the sum by two.
\[ s = \frac{1}{2} \cdot (a + b + c) \]
Coding Task
Implement a function area() to calculate the area of a triangle from its sides. Use a subfunction semiperimeter() to simplify the logic.
Function to calculate the area of a triangle.
def semiperimeter(a: float, b: float, c: float) -> float:
"""Calculates the semiperimeter of a triangle.
Args:
a: Side a of the triangle.
b: Side b of the triangle.
c: Side c of the triangle.
Returns:
s: The semiperimeter of the triangle.
"""
return (a + b + c) / 2
def area(a: float, b: float, c: float) -> float:
"""Calculates the area of a triangle.
The triangle's area is calculated from its sides using Heron's
formula.
Args:
a: Side a of the triangle.
b: Side b of the triangle.
c: Side c of the triangle.
Returns:
T: The Area of the triangle."""
s = semiperimeter(a, b, c)
T = (s*(s-a)*(s-b)*(s-c))**(1/2)
return T
print(area(3, 4, 5))
# should print 6.0 to the command line6.0
21.1.2 The Altitude of a Triangle
The altitude \(h\) of a triangle is calculated by the formula
\[ h_a = \frac{2}{a}\sqrt{s(s-a)(s-b)(s-c)} \]
where \(s\) denotes the semiperimeter. To calculate the altitude \(h_b\) and \(h_c\),switch the respective variables in the formula accordingly.
Coding Task
Implement a function altitude() to calculate the altitude of a triangle from its sides. Use as many subfunctions as possible.
Function to calculate the altitude of a triangle.
def altitude(a: float, b: float, c: float, base: str = 'c') -> float:
"""Calculates the altitude of a triangle.
The altitude is calculated to the base c by default. The base can
be overridden if needed.
Args:
a: Side a of the triangle.
b: Side b of the triangle.
c: Side c of the triangle.
base: The base of the altitude. The default value is 'c'
Returns:
h: The altitude of the triangle."""
T = area(a, b, c)
if base == 'c':
h = (2*T)/c
elif base == 'a':
h = (2*T)/a
else:
h = (2*T)/b
return h
print(altitude(3, 4, 5, 'a'))If you have to draw the altitude of a triangle, you most likely would draw it from the horizontal side as a base line. Therefore, the base line will frequently be side \(c\) of your triangle. To account for this probability, Python offers a default value option for function arguments, as demonstrated in the altitude() function above.
The syntax requires that arguments with a default value must be placed at the end of the argument list. The default value is assigned using the \(=\) sign. This default value can be overridden either implicitly as a positional argument by providing a full list of arguments (e.g., altitude(3, 4, 5, 'a')), or explicitly as a keyword argument by directly assigning a new value to the parameter (e.g., altitude(3, 4, 5, base='a')).
21.1.3 Circumcircle of a Triangle
The radius \(r\) of the circumcircle of a triangle is calculated by the formula
\[ r = \frac{abc}{4T} \]
where \(T\) denotes the area of the triangle.
Coding Task
Implement a function circumcircle() to calculate the circumcircle radius of a triangle from its sides. Use as many subfunctions as possible.
Function to calculate the radius of the circumcircle of a triangle.
def circumcircle(a: float, b: float, c: float) -> float:
"""Calculates the circumcircle radius of a triangle.
Args:
a: Side a of the triangle.
b: Side b of the triangle.
c: Side c of the triangle.
Returns:
r: The radius of the circumcircle of a triangle.
"""
T = area(a, b, c)
n = a * b * c
d = 4 * T
r = n / d
return r
print(circumcircle(3, 4, 5))21.1.4 The Incircle of a Triangle
The incircle radius \(\varrho\) (small greek letter rho) of a triangle is calculated by the formula
\[ \varrho = \frac{T}{s} \]
where \(T\) denotes the area of the triangle and \(s\) the semiperimeter respectively.
Coding Task
Implement a function incircle() to calculate the incircle radius of a triangle from its sides. Use as many subfunctions as possible.
Function to calculate the radius of the incircle of a triangle.
def incircle(a: float, b: float, c: float) -> float:
"""Calculates the incircle radius.
Args:
a: Side a of the triangle.
b: Side b of the triangle.
c: Side c of the triangle.
Returns:
rho: Incircle radius of the triangle.
"""
T = area(a, b, c)
s = semiperimeter(a, b, c)
rho = T/s
return rho
print(incircle(3, 4, 5))