# The Cosine Law

The lengths of the sides of a triangle (in Euclidean space) fully determine (the magnitudes of) the angles of the triangle. Conversely, given two sides of a triangle and the angle between them, it is possible to compute the length of the third side (which is said to be opposite to the angle given); the formula (known as the cosine law) for this is a simple generalisation of Pythagoras' law governing the sides of a right-angle triangle.

Suppose we are given a triangle with vertices A, B and C and we know the interior angle at C and the lengths of the sides AC and CB; we wish to know the length of the side AB. Drop a perpendicular from C onto the line of which the edge AB is a portion; let F be the point where the perpendicular meets this line. Let x and y be the displacements along this line, from A to F and from F to B respectively, measured positive if in the direction from A to B, else negative. If the interior angle at A is obtuse (greater than a right angle) then x shall be negative; likewise for B and y. In any case, the sum x+y shall be the length of edge AB. Since the triangles CAF and CBF are right angle triangles, we can infer that x = AC.Cos(CAB) and y = BC.Cos(CBA); while AC.Sin(CAB) = CF = BC.Sin(CBA). From this we can infer

AB.AB
= (x+y).(x+y)
= (AC.Cos(CAB) +BC.Cos(CBA)).(AC.Cos(CAB) +BC.Cos(CBA))
= AC.Cos(CAB).AC.Cos(CAB) +AC.Cos(CAB).BC.Cos(CBA) +BC.Cos(CBA).AC.Cos(CAB) +BC.Cos(CBA).BC.Cos(CBA)
= 2.AC.BC.Cos(CAB).Cos(CBA) +AC.AC −AC.Sin(CAB).AC.Sin(CAB) +BC.BC − BC.Sin(CBA).BC.Sin(CBA)
= 2.AC.BC.Cos(CAB).Cos(CBA) +AC.AC −AC.Sin(CAB).BC.Sin(CBA) +BC.BC − AC.Sin(CAB).BC.Sin(CBA)
= AC.AC +BC.BC +2.AC.BC.(Cos(CAB).Cos(CBA) −Sin(CAB).Sin(CBA))
= AC.AC +BC.BC +2.AC.BC.Cos(CAB+CBA)
= AC.AC +BC.BC +2.AC.BC.Cos(turn/2 −ACB)
= AC.AC +BC.BC −2.AC.BC.Cos(ACB)

since CAB, CBA and ACB are the three interior angles of a triangle, so sum to turn/2; and Cos(turn/2 −θ) = −Cos(θ) for any angle θ. The formula just obtained is the cosine law: it determines the length of a side of a triangle given the opposite angle and the lengths of the other two sides; it may equally be re-arranged as Cos(ACB) = (AC.AC +BC.BC −AB.AB)/AC/BC/2, which suffices to determine ACB (given that it lies between zero and half a turn, an interval on which Cos is monotonicly decreasing) when the lengths of the sides are given. Thus the cosine rule solves both of the problems introduced above; and it reduces to the pythagorean law when ACB is a right angle, since Cos(turn/4) = 0.

One may equally arrive at the cosine law by considering AC, CB and AB as vectors (the above considered them only as lengths) so that AC+CB = AB (and CB = −BC has the same length as BC but opposite direction); lengths are then encoded by a metric, g, which consumes two vectors and yields their inner product; which is the product of their lengths multiplied by the cosine of the angle through which we must rotate one to make it parallel to the other; when the two vectors are equal, this is just the square of the vector's length. Then g(AB,AB) = g(AC,AC) +g(CB,CB) +g(AC,CB) +g(CB,AC); the last two terms are equal (due to symmetry of the metric) and each is just the product of the lengths of AC and CB multiplied by the angle through which AC must be rotated to make it parallel to CB; this angle is just the external angle of the triangle at C, which is equal to CAB+CBA, so its cosine is −Cos(ACB).

## Rational triangles

In particular, we can infer, from the cosine law, that the cosine of any interior angle of a triangle whose sides are all rationally commensurate (i.e. whole multiples of some shared unit in length) must be a rational (that is, a ratio of whole numbers). It is worth noting that the sine and cosine of any angle which is a rational multiple of the whole turn (or, equivalently, of the degree) are necessarily algebraic (that is, roots of some polynomial equation whose coefficients are all whole numbers; in particular, all rationals are algebraic). Consequently it may be worth exploring whether all internal angles of integer-sided triangles are necessarily rational multiples of a turn.

One can also exploit the cosine law to study the analogue, for angles other than the right angle, of the problem of seeking triangles with rationally commensurate sides and some prescribed angle. Using the largest unit of length of which all three sides are integer multiples, we can describe the edges by integers a, b and c which we can use in place of the lengths involved in the cosine rule. By using, as our unit of length, the largest length of which each side of our triangle is a whole multiple, we effectively ensure that a, b and c are coprime. Any whole scaling applied to all three integers alike will yield another solution to our problem; but this is equivalent to the un-scaled solution from which it is derived by simply selecting a different unit of length. Consequently, we can afford to ignore solutions having a common factor between the three integers.

As an example, consider the cases of turn/3 and turn/6 (i.e. 120 and 60 degrees); these have cosines −1/2 and +1/2, respectively, so that the cosine law reduces to: a.a ±a.b +b.b = c.c, which we may re-arrange as a.(a±b) = (c+b).(c−b).

Any common factor of a and b is necessarily a repeated factor of c.c and thus a factor of c. Any common factor of c and b is necessarily a repeated factor of a.(a±b); if it is a factor of a±b then it will necessarily also be a factor of a; thus the only way for it to not be a factor of a is for it to be a product of coprime integers, one of whose squares is a factor of a while the other's square is a factor of a±b; but then the first factor is a factor of a and the second is a shared factor of b and a±b, hence also of a, making a a multiple of both coprime factors and, hence, of their product. Since the equation is symmetric between a and b, we may thus infer that any common factor of any two of our integers is necessarily a common factor of the third; the three integers are collectively coprime only if they are pair-wise coprime.

In particular we can ignore any case where more than one of our edges has even length; in particular, either a or b must be odd; given symmetry between them, we may suppose a is odd. Then if b is even, so is b.(a+±b); if b is odd, so are both b.b and a.b, so b.b±a.b is even; either way, adding odd a.a we find that c.c is odd, hence so is c. Thus, c+a and c−a are both even; and they differ by 2.a, which isn't a multiple of 4, so in fact one of them is a multiple of 4, so b.(b±a) = (c+a).(c−a) is a multiple of 8. Thus either b or a±b is a multiple of 8, the other being odd (since these two must also be coprime, any common factor of them being a common factor of a and b). Thus, if b has any factors of two, it has at least three of them; otherwise, it is odd and a±b is a multiple of 8.

We can arrive at the same conclusion another way. Modulo 8, all odd squares are 1; multiples of four have 0 as their square and other even numbers have 4 as their square. The product a.b need not be a perfect square, however. We know c and a are odd; thus, modulo 8, we require (b±a).b = c.c −a.a = 0; as a is odd, one of b±a and b must be odd and the other even; whence the even one must in fact be a multiple of 8; so either b or b±a is a multiple of 8.

Any common factor of c+b and c−b is necessarily a common factor of their sum and product, 2.c and 2.b; thus, if c and b are coprime, the highest common factor of c+b and c−b is 1 or 2; the common factor will only be two if both c and b are odd, in which case a±b is a multiple of 8.

Aside from the case where all three sides are 1 (the equilateral triangle, with interior angle turn/6), being coprime implies that the three sides must be distinct; since the nearest perfect squares to b.b are b.b±2.b+1, we can exclude the case a = 1 since b.b±b+1 is too close to b.b to be a perfect square, like c.c; consequently, with a odd, we may assert that a > 2; and, by kindred argument, that b > 1.

If b is a multiple of 8 it is 2.n for some n divisible by 4; yielding c.c = a.a ±2.n.a +4.n.n = (a±n).(a±n) +3.n.n whence (c+a±n).(c−(a±n)) = 3.n.n. Since a, b and c are the sides of a triangle, we know a+c ≥ b > n whence c+a±n > 0, whence our other factor of 3.n.n > 0 must also be positive, so c > a±n. As a and c are odd, c−a is even; being > ±n, which is also even, we may infer that c−(a±n) ≥ 2 whence 3.n.n/2 ≥ c+a±n > 2.(a±n) yielding 3.n.n/4 > a±n. Thus c.c = (a±n).(a±n) +3.n.n < 3.n.n.(1 +3.n.n/16) and c < n.√(3 +9.n.n/16).

For the turn/6 (i.e. 60 degree) case, ± is − and the case where b−a is zero (a particularly convenient multiple of 8) is simply the familiar equilateral triangle, with b = a and c.c = a.a −a.b +b.b = a.a so c = a also, so that the coprime form has a = b = c = 1. Otherwise either b or b−a must be a non-zero multiple of 8. For b = 8 we have c < 4.√12 < 14, 12 > a−4, whence a < 16, and c > a−4, with a and c odd, so c in {a−2, a, a+2, a+4, ...}; we require (c+a−4).(c+4−a) = 48; the case c = a necessarily yields b = a, the equilateral case already considered; any c > a+4 would yield c+4−a > 8 whence 2.a < c+a−4 < 48/8 = 6, making a < 3 but we already know a > 2, so c > a+4 can yield no solutions; the remaining cases, c = a−2, a+2 and a+4, yield solutions [a,c] = [15,13], [5,7] and [3,7], respectively.

Written by Eddy.