# The Law of Cosines

With the basics of trigonometry out of the way, lets delve into something that’s a little bit more advanced and totally points out that what I spouted about trig was disingenuous at best and a lie at worst; The Law of Cosines!

Before I even go any farther, I’m going to point out that in all likelihood the information that I’m going to present here is probably not really needed in order to do anything in game development. I spent some time trying to wrack my brain for some game-related task that would specifically require this stuff to find a solution, but nothing obvious presented itself.

So why am I talking about it, then? It’s building blocks, baby! Some more important things (such as the Dot Product of Vectors) derive themselves in part from the Law of Cosines. Although it’s possible to just apply some formulas to get the results that you want, I find that knowing the why of something can help you find uses for the how; maybe that’s just me. Anyway, Onward to math adventure!

The Law of Cosines says that sometimes when you want to get financing, you may in some situations require someone else to guaranty the loan by saying that they will become responsible for paying back the loan if you don’t.

Oh wait, that’s a co-signer. Nevermind.

The Law of Cosines (also known as the cosine rule) is yet another math topic which you may or may not remember from back when you were in school (assuming that you’re of an advanced and enfeebled age such as myself, that is, and have actually taken this in school at this point) as something trigonometry related that does a thing with a thing in a way for reasons.

What the law actually formalizes is the relationship between the lengths of the sides of a triangle to the cosine of one of its angles. In the page about basic trigonometry, I said:

“there is a ratio between the angles that make up a right triangle and the length of the sides of that triangle, and this ratio is fixed.”

This is the part where I admit that what I said in that page was somewhat disingenuous. In fact based on what I said, you might have already questioned that sentence. If you can double (for example) the lengths of all of the sides of a right triangle and the angles remain the same, surely the same is true for any triangle, right?

It turns out that yes, this is the case. The formula for the Law of Cosines states this:

$c^{2} = a^{2} + b^{2} - 2ab\cos \theta$

Well, that seems awfully familiar, doesn’t it? What this formula is telling us is that if you know the lengths of two sides of any triangle (right angle or otherwise) and the cosine of the angle between them, you can calculate the length of the third side of the triangle. This is basically the Pythagorean theorem for any triangle and not just a right triangle.

If this doesn’t make sense, think of it this way:

If I know the lengths of two sides of any triangle and the angle that is between them, then I can easily use some basic trig to determine exactly where each of those two sides ends (by calculating a rotation), and if I knew that, then I could easily apply Pythagoras to those two points to determine the distance between them, which would also be the length of the third side of the triangle.

In effect the Law of Cosines allow you to “roll up” the math into a single formula as opposed to having to manually determine where those two endpoints are using rotation first. Note also that if a triangle is a right triangle and you know the length of the two non-hypotenuse sides, then the angle that is between them is 90°, the cosine of which is 0, which makes the second half of this formula evaluate to zero, which means that for a right triangle the two formulas work out to the same value.

As I said, this is probably not something that you would find useful in the general case, but it’s integral to understanding some more advanced topics down the road, so it’s good to at least know that it exists, if nothing else.