Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion maths/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Wikipedia reference: https://en.wikipedia.org/wiki/Area
"""

from math import pi, sqrt, tan
from math import pi, sin, sqrt, tan


def surface_area_cube(side_length: float) -> float:
Expand Down Expand Up @@ -312,6 +312,41 @@ def area_triangle(base: float, height: float) -> float:
return (base * height) / 2


def area_triangle_two_sides_included_angle(
side1: float, side2: float, included_angle_degrees: float
) -> float:
"""
Calculate the area of a triangle given the length of any two sides and
their included angle.

>>> round(area_triangle_two_sides_included_angle(10, 10, 30), 1)
25.0
>>> round(area_triangle_two_sides_included_angle(10, 10, -30), 1)
25.0
>>> round(area_triangle_two_sides_included_angle(10, 10, 330), 1)
25.0
>>> round(area_triangle_two_sides_included_angle(10, 10, 180), 1)
0.0
>>> round(area_triangle_two_sides_included_angle(10, 10, 0), 1)
0.0
>>> round(area_triangle_two_sides_included_angle(0, 10, 120), 1)
0.0
>>> area_triangle_two_sides_included_angle(1, -1, 1)
Traceback (most recent call last):
...
ValueError: ...
>>> area_triangle_two_sides_included_angle(-1, 1, 1)
Traceback (most recent call last):
...
ValueError: ...
"""
if side1 < 0 or side2 < 0:
raise ValueError(
"area_triangle_two_sides_included_angle() only accepts non-negative lengths"
)
return 0.5 * side1 * side2 * abs(sin(included_angle_degrees * pi / 180))


def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float:
"""
Calculate area of triangle when the length of 3 sides are known.
Expand Down