Oblique Triangles: Laws of Sines and Cosines
Most triangles have no right angle. Such triangles are called oblique, and SOH-CAH-TOA alone cannot solve them. Two laws, the law of sines and the law of cosines, solve every triangle from three suitable pieces of information.
Notation
Label the angles and the sides opposite them . So side is across from angle , and so on. The angles always add to 180° ().
The law of sines
Why it is true. Drop the height from onto side . The two right triangles you get share :
Dropping a height from another vertex brings in the same way. (If the height falls outside the triangle, the same algebra works with .) The common value is in fact the diameter of the circle through the three vertices.
Use the law of sines when you know an angle and its opposite side, plus one more piece: AAS, ASA, or SSA.
The law of cosines
When , and this is Pythagoras' theorem, so the law of cosines is Pythagoras with a correction term. Proof with coordinates: put at the origin, at and at . Then
Use it for SAS (two sides and the angle between them) and SSS (three sides). Solved for the angle:
Solving triangles in code
import math
def solve_sas(a, b, C_deg):
C = math.radians(C_deg)
c = math.sqrt(a*a + b*b - 2*a*b*math.cos(C))
A = math.degrees(math.acos((b*b + c*c - a*a) / (2*b*c)))
return c, A, 180 - C_deg - A
def solve_sss(a, b, c):
A = math.degrees(math.acos((b*b + c*c - a*a) / (2*b*c)))
B = math.degrees(math.acos((a*a + c*c - b*b) / (2*a*c)))
return A, B, 180 - A - B
c, A, B = solve_sas(8, 11, 47)
print(f"SAS a=8, b=11, C=47°: c = {c:.4f}, A = {A:.3f}°, B = {B:.3f}°")
print("SSS 4, 5, 6: angles =", [round(x, 3) for x in solve_sss(4, 5, 6)])
print("check law of sines:", round(8 / math.sin(math.radians(A)), 6), round(c / math.sin(math.radians(47)), 6))SAS a=8, b=11, C=47°: c = 8.0603, A = 46.542°, B = 86.458°
SSS 4, 5, 6: angles = [41.41, 55.771, 82.819]
check law of sines: 11.021057 11.021057
Getting from acos rather than from the law of sines is deliberate: acos returns angles up to 180°, so it never picks the wrong one of two possible angles. asin returns at most 90°, which leads to the next topic.
The ambiguous case (SSA)
Given two sides and an angle not between them, there may be zero, one or two triangles. The law of sines gives , and both and have that sine. Each candidate is valid if the angles still add to less than 180°.
import math
def solve_ssa(a, b, A_deg):
"""Known: side a, side b and angle A opposite a. Returns all triangles."""
s = b * math.sin(math.radians(A_deg)) / a
if s > 1:
return []
sols = []
for B in {math.degrees(math.asin(s)), 180 - math.degrees(math.asin(s))}:
C = 180 - A_deg - B
if C > 1e-9:
c = a * math.sin(math.radians(C)) / math.sin(math.radians(A_deg))
sols.append((round(B, 3), round(C, 3), round(c, 4)))
return sorted(sols)
print("a=7, b=10, A=35°:", solve_ssa(7, 10, 35))
print("a=12, b=10, A=35°:", solve_ssa(12, 10, 35))
print("a=4, b=10, A=35°:", solve_ssa(4, 10, 35))a=7, b=10, A=35°: [(55.024, 89.976, 12.2041), (124.976, 20.024, 4.1789)]
a=12, b=10, A=35°: [(28.554, 116.446, 18.732)]
a=4, b=10, A=35°: []
With the side can swing to two positions; with the second candidate would make the angles add to more than 180°; with the side is too short to reach.
Area of any triangle
The first follows from with height . For the 4-5-6 triangle, and the area is .
In software
- Angle between two vectors. Put vectors and tail to tail. The third side is , and the law of cosines gives . Expanding the left side with dot products leaves , the formula every game engine and every embedding search uses (angle between vectors, the dot product).
- Triangulation. Two sensors a known distance apart each measure the angle to a target; the law of sines gives the distances, so you get a position. Surveyors, old radio direction finders and stereo depth cameras work this way.
- Inverse kinematics. A two-bone arm (upper arm, forearm) that must reach a target at distance : the law of cosines gives the elbow angle directly.
import math
# Two stations 10 km apart on the x-axis see a ship at interior angles 50° and 60°.
base, A, B = 10.0, math.radians(50), math.radians(60)
C = math.pi - A - B
dist_A = base * math.sin(B) / math.sin(C) # law of sines: side opposite B
ship = (dist_A * math.cos(A), dist_A * math.sin(A))
print(f"distance from station A: {dist_A:.4f} km")
print(f"ship at ({ship[0]:.4f}, {ship[1]:.4f}) km")
# Two-bone IK: elbow angle for bones 3 and 2 reaching a target 4 away
L1, L2, d = 3.0, 2.0, 4.0
elbow = math.degrees(math.acos((L1**2 + L2**2 - d**2) / (2 * L1 * L2)))
print(f"elbow interior angle: {elbow:.3f}°")distance from station A: 9.2160 km
ship at (5.9240, 7.0599) km
elbow interior angle: 104.478°
Exercises
Exercise 1: ASA
In a triangle, , and . Find , and .
Answer
, , .
Solution
import math
s = lambda d: math.sin(math.radians(d))
print(round(12 * s(40) / s(65), 3), round(12 * s(75) / s(65), 3))8.511 12.789
Exercise 2: SSS
A triangle has sides 7, 8 and 13. Find its largest angle exactly.
Answer
120°.
Solution
The largest angle is opposite the longest side, 13:
Exercise 3: How many triangles?
Given , and , how many triangles exist? Find the possible values of .
Answer
Two: or .
Solution
import math
s = 12 * math.sin(math.radians(40)) / 9
B1 = math.degrees(math.asin(s))
print(round(s, 4), round(B1, 2), round(180 - B1, 2))0.8571 58.99 121.01
Exercise 4: Area two ways
Find the area of the triangle with sides 7, 8, 13 using Heron's formula, and again with using the angle from Exercise 2.
Answer
both ways.
Solution
Exercise 5: Angle between vectors
Use the law of cosines (not a dot product function) to find the angle between and , then confirm with math.atan2.
Answer
About 57.53°.
Solution
The triangle has sides , and .
import math
u, v = (3, 1), (1, 4)
a, b = math.hypot(*u), math.hypot(*v)
c = math.hypot(u[0] - v[0], u[1] - v[1])
theta = math.degrees(math.acos((a*a + b*b - c*c) / (2*a*b)))
check = math.degrees(math.atan2(v[1], v[0]) - math.atan2(u[1], u[0]))
print(round(theta, 2), round(check, 2))57.53 57.53
Test yourself
You know two sides and the angle between them. Which law do you start with?
- Law of sines
- Law of cosines
- Pythagoras
- Either; it makes no difference
What does the law of cosines reduce to when ?
- The law of sines
- Nothing; it does not apply
Why is SSA called the ambiguous case?
- Because the law of cosines fails
- Because has two solutions between 0° and 180°, so there can be 0, 1 or 2 triangles
- Because the angles can add to more than 180°
- Because the sides must be equal