Trigonometry
Law of Cosines
Pythagoras for any triangle — c² = a² + b² − 2ab cos(C)
The law of cosines generalizes the Pythagorean theorem to non-right triangles. For any triangle with sides a, b, c and angle C opposite side c — c² = a² + b² − 2ab cos(C). When C = 90°, cos(C) = 0 and the formula reduces to Pythagoras. Used in surveying, navigation, GPS, and any geometry problem where you have two sides and the included angle.
- Formulac² = a² + b² − 2ab cos(C)
- Reduces to Pythagoras whenC = 90° (cos 90° = 0)
- SolvesSAS (two sides + included angle), SSS (three sides) for angles
- Sister theoremLaw of sines — a/sin(A) = b/sin(B) = c/sin(C)
- Year of formal statement~300 BCE (Euclid, geometric form); algebraic form 16th century
- Vector form|u + v|² = |u|² + |v|² + 2u·v
Interactive visualization
Press play, or step through manually. The visualization is yours to drive — try it before reading on.
Watch the 60-second explainer
A condensed visual walkthrough — narrated, captioned, under a minute.
The formula
For any triangle with sides a, b, c and angles A, B, C (where A is opposite side a, etc.):
c² = a² + b² − 2ab cos(C)
b² = a² + c² − 2ac cos(B)
a² = b² + c² − 2bc cos(A)
By symmetry, you can write the formula for any side as a function of the other two and the angle between them. The "−2ab cos(C)" is the correction term that handles non-right angles.
When C = 90°, cos(C) = 0 and you get the Pythagorean theorem — c² = a² + b². When C is acute (less than 90°), cos(C) is positive, so you subtract — c is shorter than the right-triangle hypotenuse would be. When C is obtuse (greater than 90°), cos(C) is negative, so you add (subtracting a negative) — c is longer.
Two main use cases
SAS — Side-Angle-Side
Given two sides and the included angle, find the third side.
Example — a = 5, b = 7, C = 60°. Find c.
c² = 5² + 7² − 2·5·7·cos(60°)
= 25 + 49 − 70·(0.5)
= 74 − 35 = 39
c = √39 ≈ 6.24
SSS — Side-Side-Side
Given all three sides, find any angle. Rearrange the formula to solve for cos(C):
cos(C) = (a² + b² − c²) / (2ab)
Example — a = 8, b = 11, c = 13. Find angle C (opposite side c = 13).
cos(C) = (64 + 121 − 169) / (2·8·11)
= 16 / 176 = 0.0909
C = arccos(0.0909) ≈ 84.8°
If cos(C) comes out negative, the angle is obtuse. If it comes out greater than 1 or less than −1, the three sides don't form a valid triangle (they fail the triangle inequality).
Vector form — same equation, deeper meaning
Let u and v be two vectors with magnitudes a, b and angle θ between them. The vector u − v has magnitude c, the third side of the triangle they form. Then:
|u − v|² = |u|² + |v|² − 2 u · v
= a² + b² − 2ab cos θ
The dot product u · v = |u||v|cos θ encodes both magnitudes and the angle. The law of cosines is the dot product formula expanded in terms of triangle sides. This is why every linear algebra course derives angles between vectors using essentially the law of cosines.
Proof from coordinates
Place the triangle with one vertex at the origin and another at (b, 0). The third vertex is at (a cos C, a sin C). The third side has length:
c = √((a cos C − b)² + (a sin C − 0)²)
c² = (a cos C − b)² + (a sin C)²
= a² cos²C − 2ab cos C + b² + a² sin²C
= a²(cos²C + sin²C) − 2ab cos C + b²
= a² + b² − 2ab cos C
Using sin²C + cos²C = 1 in the third-to-last step. The law of cosines is just coordinate geometry plus Pythagoras.
Law of cosines vs law of sines
| Law of cosines | Law of sines | |
|---|---|---|
| Formula | c² = a² + b² − 2ab cos(C) | a/sin A = b/sin B = c/sin C |
| Solves | SAS, SSS | ASA, AAS, SSA (ambiguous) |
| Reduces to Pythagoras | Yes (when C = 90°) | No |
| Ambiguous case | No | SSA can give 0, 1, or 2 solutions |
| Best for finding | Sides given angle, angles given sides | Sides given two angles + opposite side |
Most triangle problems can be solved with either, but one is usually shorter. Memorize when to reach for which.
JavaScript: triangle solver
// Find third side given two sides + included angle (in radians)
function thirdSide(a, b, C) {
return Math.sqrt(a*a + b*b - 2*a*b*Math.cos(C));
}
// Find angle opposite given side, given all three sides
function angleFromSides(a, b, c) {
// Returns angle C in radians, opposite side c
const cosC = (a*a + b*b - c*c) / (2*a*b);
return Math.acos(cosC);
}
// Validate triangle inequality
function validTriangle(a, b, c) {
return a + b > c && b + c > a && a + c > b;
}
// Solve for all unknown angles given three sides
function solveSSS(a, b, c) {
if (!validTriangle(a, b, c)) throw new Error('Invalid triangle sides');
const A = angleFromSides(b, c, a);
const B = angleFromSides(a, c, b);
const C = Math.PI - A - B; // angles sum to π
return { A, B, C, degrees: { A: A*180/Math.PI, B: B*180/Math.PI, C: C*180/Math.PI } };
}
solveSSS(8, 11, 13);
// { A: 0.673, B: 0.999, C: 1.470, degrees: { A: 38.6°, B: 57.2°, C: 84.2° } }
// Distance between two GPS points using law of cosines (spherical)
function gpsDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Earth's radius in km
const φ1 = lat1 * Math.PI/180;
const φ2 = lat2 * Math.PI/180;
const Δλ = (lon2 - lon1) * Math.PI/180;
return R * Math.acos(Math.sin(φ1)*Math.sin(φ2) + Math.cos(φ1)*Math.cos(φ2)*Math.cos(Δλ));
}
More worked examples
Surveyor finding a distance across a lake
Two points A and B on shore are 300m apart. From A, the angle to a third point C (across the lake) is 73°. From B, the angle to C is 58°. Find AC.
Use law of sines first to find AC. The angle at C is 180° − 73° − 58° = 49°. Then:
AC / sin(58°) = 300 / sin(49°)
AC = 300 · sin(58°) / sin(49°) ≈ 300 · 0.848 / 0.755 ≈ 337m
Or, using law of cosines with sides AB = 300 and the two computed sides — verify by computing AC directly. Both approaches agree.
Verify a triangle with sides 7, 24, 25 is right-angled
Compute cos C where C is opposite side 25:
cos(C) = (7² + 24² − 25²) / (2·7·24) = (49 + 576 − 625) / 336 = 0/336 = 0
C = arccos(0) = 90°
It's a right triangle. The 7-24-25 is a Pythagorean triple, confirming the right angle at C.
Where the law of cosines is essential
- Surveying. Triangulation — measure two sides and the included angle, compute the third side. Used in land surveying since at least 1600.
- Navigation and GPS. Ship and aircraft course corrections, distance to a destination given known reference points. Spherical law of cosines for great-circle distances on Earth.
- Computer graphics. Computing angles between vectors (lighting, normals, view direction) — the dot product is the law of cosines under the hood.
- Robotics — inverse kinematics. Given a desired end-effector position and link lengths, compute joint angles. Each joint's angle solves a triangle problem.
- Crystallography. Determining lattice parameters from X-ray diffraction angles. Bragg's law combines with the law of cosines for crystal structure determination.
- Sports analytics. Computing angles for shots in soccer, basketball, golf — given player position, target position, and obstacles, what's the available angle?
Common mistakes
- Using law of cosines on the wrong side. The angle C must be opposite side c (and adjacent to sides a and b). If you mix up which angle is between which sides, you'll plug in wrong values.
- Forgetting the minus sign on cos(C). The formula has −2ab cos(C). Substituting +2ab cos(C) gives wrong answers and is geometrically nonsensical (would make c larger when C is acute).
- Confusing degrees and radians. Math.cos in JavaScript expects radians. cos(60°) is not what you want; cos(60 * π/180) = cos(π/3) = 0.5 is.
- Triangle inequality violated. If you compute sides that don't satisfy a + b > c (or any cyclic permutation), no real triangle exists. arccos returns NaN. Validate inputs before computing.
- Using law of cosines for SSA. SSA (two sides + non-included angle) is the ambiguous case best handled by law of sines (which detects 0, 1, 2 solutions). Forcing law of cosines gives a quadratic — solve it carefully.
- Wrong cos sign for obtuse angles. cos is negative for angles between 90° and 180°. Forgetting this when C is obtuse — c² = a² + b² + 2ab|cos C| (note + because cos is negative — the minus and the negative cancel). Just trust the formula; the algebra handles signs.
Frequently asked questions
How does the law of cosines reduce to Pythagoras?
When the angle C is 90°, cos(C) = 0, so the −2ab cos(C) term vanishes. What's left is c² = a² + b² — exactly the Pythagorean theorem. So Pythagoras is the special case of the law of cosines applied to right triangles. The law of cosines is more general; Pythagoras is more famous because right triangles are more common in elementary geometry.
What kinds of triangle problems does it solve?
Two main cases. SAS (Side-Angle-Side) — given two sides and the included angle, find the third side. SSS (Side-Side-Side) — given all three sides, find any angle (rearrange the formula to solve for cos(C)). For other configurations (ASA, AAS), use the law of sines instead.
How is the law of cosines related to the dot product?
They're algebraically the same. For vectors u, v with angle θ between them — u · v = |u||v|cos θ. Apply this to the difference u − v — |u − v|² = |u|² + |v|² − 2u · v = |u|² + |v|² − 2|u||v|cos θ. The law of cosines is the dot-product formula with the third side as the difference vector, and a, b as the magnitudes.
When do I use law of cosines vs law of sines?
Law of cosines for SAS or SSS. Law of sines for ASA, AAS, or SSA (latter is the ambiguous case). Sometimes both work; pick whichever has fewer steps. SSS — only law of cosines (sines doesn't give angles directly without sides). SAS — law of cosines first, then sines.
What's the ambiguous SSA case?
Given two sides and a non-included angle, sometimes there are zero, one, or two valid triangles. The classic example — given a = 5, b = 7, A = 30°. The law of sines gives sin(B) = 7 sin(30°)/5 = 0.7. So B = arcsin(0.7) ≈ 44.4° or B = 180° − 44.4° = 135.6°. Both could form valid triangles depending on the third angle. Law of cosines avoids this — it's unambiguous.
How do GPS and surveying use the law of cosines?
Triangulation — measure distances from known reference points and angles between observations. The law of cosines reconstructs the unknown vertex's position. In navigation, given two known distances (from satellites or beacons) and the angle between them, you compute the third distance to a target. GPS combines this with timing measurements from multiple satellites.
Are there generalizations beyond triangles?
Yes. For polygons — the law of cosines applies to each face/edge configuration but doesn't simplify to a single formula. For higher-dimensional simplices (tetrahedra in 3D, n-simplices in nD) — generalizations exist using volumes and dihedral angles. Spherical and hyperbolic geometries have their own laws of cosines with different functions (cos→cosh in hyperbolic; cos→cos with spherical-distance arguments).