The Law of Cosines
The Pythagorean Theorem for triangles without a right angle.
1. Beyond Right Triangles
The Pythagorean Theorem finds the third side of a triangle from the other two, but only when the angle between them is a right angle. For any other angle, the third side is shorter or longer than $\sqrt{a^2 + b^2}$, depending on how wide the angle is.
The law of cosines covers every triangle. Take two sides of lengths $a$ and $b$, with the angle $\theta$ between them, and let $c$ be the length of the third side, opposite $\theta$.
The law of cosines states that
$$c^2 = a^2 + b^2 - 2ab\cos\theta$$The last term is the correction to the Pythagorean Theorem, and it depends on the angle. The rest of this page derives it.
2. Deriving the Law
Call the triangle $ABC$, with the angle $\theta$ at $C$, the side $CB$ of length $a$, the side $CA$ of length $b$, and the side $BA$ of length $c$. From $B$, draw a line straight down to $CA$, meeting it at a right angle at the point $D$. This splits the triangle into two right triangles, $CDB$ and $BDA$.
In the right triangle $CDB$, the hypotenuse is $CB$, of length $a$. By the definitions of sine and cosine,
$$\cos\theta = \frac{CD}{a} \qquad \sin\theta = \frac{BD}{a}$$so
$$CD = a\cos\theta \qquad BD = a\sin\theta$$The rest of the bottom side is
$$DA = CA - CD = b - a\cos\theta$$In the right triangle $BDA$, the hypotenuse is $BA$, of length $c$. By the Pythagorean Theorem,
$$\begin{align} c^2 &= BD^2 + DA^2 \\ &= (a\sin\theta)^2 + (b - a\cos\theta)^2 \end{align}$$For any two numbers $x$ and $y$,
$$(x - y)^2 = (x - y)(x - y) = x^2 - 2xy + y^2$$Expanding the second bracket this way, and putting the two $a^2$ terms side by side:
$$\begin{align} c^2 &= a^2\sin^2\theta + a^2\cos^2\theta + b^2 - 2ab\cos\theta \\ &= a^2(\sin^2\theta + \cos^2\theta) + b^2 - 2ab\cos\theta \end{align}$$By the Pythagorean identity, $\sin^2\theta + \cos^2\theta = 1$. So
Example 1
Two sides of lengths $a = 5$ and $b = 8$ meet at an angle of $60^\circ$. How long is the third side?
We need $\cos 60^\circ$. An equilateral triangle with sides of 2 has every angle equal to $60^\circ$. Cutting it in half gives a right triangle with a $60^\circ$ angle, hypotenuse 2 and adjacent side 1, so $\cos 60^\circ = \frac{1}{2}$. Then
$$c^2 = 5^2 + 8^2 - 2(5)(8)\left(\tfrac{1}{2}\right) = 25 + 64 - 40 = 49$$so $c = 7$.
3. Finding an Angle
The law also works the other way round. Move $2ab\cos\theta$ to the left side and $c^2$ to the right, then divide by $2ab$:
So the three sides of a triangle are enough to find any of its angles.
Example 2
A triangle has sides 5, 8 and 7. What is the angle between the sides 5 and 8?
$$\cos\theta = \frac{5^2 + 8^2 - 7^2}{2(5)(8)} = \frac{40}{80} = \frac{1}{2}$$so $\theta = 60^\circ$, the triangle from Example 1.
4. Angles Over 90°
Fig 1 draws $\theta$ smaller than 90°, so that $D$ lands between $C$ and $A$, and the definitions of sine and cosine on this site so far use the angles of a right triangle, which are all smaller than 90°. The law of cosines also holds when $\theta$ is 90° or more, once cosine is defined for such angles. A later page on the unit circle will do that.