Geometry · Law of cosines

Law of cosines

In any triangle, c² = a² + b² − 2ab cos C, where C is the angle between the sides a and b. At a right angle the last term is 0 and this is the Pythagorean theorem.

Law of cosines in this visualization

Take a triangle with sides a=BCa = BC and b=CAb = CA and the angle CC between them. These three fix the third side c=ABc = AB, and the law of cosines says how: c2=a2+b2−2abcos⁡Cc^2 = a^2 + b^2 - 2ab\cos C. The last term corrects the Pythagorean theorem. When CC is a right angle, cos⁡C=0\cos C = 0 and the law is the Pythagorean theorem itself. When CC is acute, cos⁡C>0\cos C > 0 and c2c^2 is less than a2+b2a^2 + b^2. When it is obtuse, cos⁡C<0\cos C < 0 and c2c^2 is more. In the picture the correction is the two yellow rectangles beside CC.

Why: the squares cut by the altitudes

Each square as the sum of its two rectangles
a2=cacos⁡B+abcos⁡Cb2=bccos⁡A+abcos⁡Cc2=bccos⁡A+cacos⁡B\begin{aligned} a^2 &= ca\cos B + ab\cos C \\ b^2 &= bc\cos A + ab\cos C \\ c^2 &= bc\cos A + ca\cos B \end{aligned}

Drop the altitude from AA to the line CBCB and carry it on through the square on aa. It meets the line at DD, and CD=bcos⁡CCD = b\cos C, the projection of CACA on CBCB. So the rectangle by CC has area a⋅bcos⁡Ca \cdot b\cos C, and the one by BB has area a⋅ccos⁡Ba \cdot c\cos B for the same reason. Every one of the six rectangles is a side times the projection of a neighbouring side on it: the product of the two sides at that corner times the cosine of the angle there. So the two rectangles beside a corner have the same area, even when they differ in shape (3 by 2 and 4 by 1.5 at the start). Add the first two equations above and subtract the third: a2+b2−c2=2abcos⁡Ca^2 + b^2 - c^2 = 2ab\cos C. This is the area argument behind Euclid’s Elements II.12 and II.13, with signed areas so that one argument covers every case.

Obtuse angles: negative pieces

When CC is obtuse, the altitudes from AA and BB land on the sides extended beyond CC. The projection CDCD now points away from BB, so as a signed length it is bcos⁡C<0b\cos C < 0. The rectangle by BB reaches past the square on aa, and the piece by CC is the part outside, counted as negative. The three equations still hold, so the law still holds, and the negative 2abcos⁡C2ab\cos C makes c2c^2 larger than a2+b2a^2 + b^2. This is what a negative cosine is for: one formula, no separate case. The same happens at any obtuse corner. If BB is obtuse, the two dotted pieces are negative, and the law about CC is unchanged.

Two ways to use it

Given two sides and the angle between them, the law gives the third side: with 3, 4 and 60°, c2=9+16−12=13c^2 = 9 + 16 - 12 = 13, so c=13≈3.606c = \sqrt{13} \approx 3.606. Read backwards, it gives an angle from three sides: cos⁡C=a2+b2−c22ab\cos C = \dfrac{a^2 + b^2 - c^2}{2ab}. For the sides 3, 5 and 7, cos⁡C=(9+25−49)/30=−1/2\cos C = (9 + 25 - 49)/30 = -1/2, so C=120∘C = 120^\circ. The sign of a2+b2−c2a^2 + b^2 - c^2 alone tells whether CC is acute, right or obtuse, which extends the converse of the Pythagorean theorem.

Things to notice

  • There is one law for each corner: a2=b2+c2−2bccos⁡Aa^2 = b^2 + c^2 - 2bc\cos A and b2=c2+a2−2cacos⁡Bb^2 = c^2 + a^2 - 2ca\cos B. Each uses the angle between the two sides on the right.
  • With vectors u\mathbf u from CC to BB and v\mathbf v from CC to AA, the side cc is the length of u−v\mathbf u - \mathbf v, and the law reads ∣u−v∣2=∣u∣2+∣v∣2−2 u⋅v|\mathbf u - \mathbf v|^2 = |\mathbf u|^2 + |\mathbf v|^2 - 2\,\mathbf u \cdot \mathbf v: the dot product u⋅v\mathbf u \cdot \mathbf v is abcos⁡Cab\cos C.

Controls

  • Drag A to turn angle C; pulling it toward or away from C changes b. Drag B along CB to change a. The sides catch on whole numbers and the angle on 30°, 45°, 60°, 90°, 120°, 135° and 150°.
  • The fields take fractions and roots such as √2 for the sides, and degrees for the angle; the keys set 60°, 90° and 120°. Projections draws the feet of the altitudes from A and B and the projections b cos C and a cos C.
  • With the keyboard, Tab to A and use the arrow keys to turn the angle, with Shift for 10° and Page Up or Page Down for the next special angle. Tab to B and use left and right to change a.

Related