Skip to content

Commit 0ab5f46

Browse files
committed
Added area_triangle_two_sides_included_angle function
This adds yet another way to calculate the area of triagnle, in this case using the length of two sides and their included angle.
1 parent 3c88735 commit 0ab5f46

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

maths/area.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Wikipedia reference: https://en.wikipedia.org/wiki/Area
44
"""
55

6-
from math import pi, sqrt, tan
6+
from math import pi, sin, sqrt, tan
77

88

99
def surface_area_cube(side_length: float) -> float:
@@ -312,6 +312,50 @@ def area_triangle(base: float, height: float) -> float:
312312
return (base * height) / 2
313313

314314

315+
def area_triangle_two_sides_included_angle(
316+
side1: float, side2: float, included_angle_degrees: float
317+
) -> float:
318+
"""
319+
Calculate the area of a triangle given the length of any two sides and
320+
their included angle.
321+
322+
>>> round(area_triangle_two_sides_included_angle(10, 10, 30), 1)
323+
25.0
324+
325+
>>> round(area_triangle_two_sides_included_angle(10, 10, -30), 1)
326+
25.0
327+
328+
>>> round(area_triangle_two_sides_included_angle(10, 10, 330), 1)
329+
25.0
330+
331+
>>> round(area_triangle_two_sides_included_angle(10, 10, 180), 1)
332+
0.0
333+
334+
>>> round(area_triangle_two_sides_included_angle(10, 10, 0), 1)
335+
0.0
336+
337+
>>> round(area_triangle_two_sides_included_angle(0, 10, 120), 1)
338+
0.0
339+
340+
>>> area_triangle_two_sides_included_angle(1, -1, 1)
341+
Traceback (most recent call last):
342+
...
343+
ValueError: area_triangle_two_sides_included_angle() only accepts \
344+
non-negative lengths
345+
346+
>>> area_triangle_two_sides_included_angle(-1, 1, 1)
347+
Traceback (most recent call last):
348+
...
349+
ValueError: area_triangle_two_sides_included_angle() only accepts \
350+
non-negative lengths
351+
"""
352+
if side1 < 0 or side2 < 0:
353+
raise ValueError(
354+
"area_triangle_two_sides_included_angle() only accepts non-negative lengths"
355+
)
356+
return 0.5 * side1 * side2 * abs(sin(included_angle_degrees * pi / 180))
357+
358+
315359
def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float:
316360
"""
317361
Calculate area of triangle when the length of 3 sides are known.

0 commit comments

Comments
 (0)