from math import acos, asin, atan, cos, degrees, radians, sin, sqrt, tan
def triangle(short, long, hypotenuse=0, angle=0):
if (hypotenuse, short, long, angle).count(0) != 2:
raise ValueError("Exactly two parameters must be zero.")
radians_angle = radians(angle)
if short:
if long:
hypotenuse = sqrt(short ** 2 + long ** 2)
angle = degrees(atan(short / long))
elif hypotenuse:
long = sqrt(hypotenuse ** 2 - short ** 2)
angle = degrees(asin(short / hypotenuse))
else: # short and angle
long = short / tan(radians_angle)
hypotenuse = short / sin(radians_angle)
elif long:
if hypotenuse:
short = sqrt(hypotenuse ** 2 - long ** 2)
angle = degrees(acos(long / hypotenuse))
else: # long and angle
short = long * tan(radians_angle)
hypotenuse = long / cos(radians_angle)
else: # hypotenuse and angle
short = hypotenuse * sin(radians_angle)
long = hypotenuse * cos(radians_angle)
short, long, hypotenuse = sorted([short, long, hypotenuse])
return float(short), float(long), float(hypotenuse), float(angle)
# The result for each of these should be (3.0, 4.0, 5.0, 36.8698976458440x)
print(triangle(3, 4)) # short and long
print(triangle(3, 0, 5)) # short and hypotenuse
print(triangle(0, 4, 5)) # long and hypotenuse
print(triangle(0, 0, 5, 36.86989764584402)) # hypotenuse and angle