# Trigonometry: Part 1

This article originally appeared in Dev.Mag Issue 14, released in June 2007.

Trigonometry. The mere mention of it strikes terror into the minds of bewildered high school students. Confusing ratios, bizarre diagrams, trying to find the sine of the cosine of the tangent of theta – many people struggle to wrap their minds around it and eventually give up in desperation to do something easier, such as ending war or achieving cold fusion.

Well, despite what your high school teachers may have said, trigonometry can actually be useful! Yes, in the vast sea of unintelligible terms is a valuable and powerful mathematical tool that can aid you immensely in the humble task of game design. The aim of this series is to explain to you just how simple trigonometry really is, and how you can use it to solve some very common programming problems. Hopefully it will make the job of coding your next blockbuster game just that little bit easier.

## First Principles – The Triangle

Before we start writing any trig in our code, it’s necessary to understand all the nasty mathematical stuff behind it (just joking, it’s not that bad!). First, we should take a look at how trigonometry works at the most basic level. To do that, we’ll need a triangle. More specifically, we’ll need a right-angled triangle (a triangle that contains a single 90 degree corner). Here’s one I found lying around:

A boring right-angled triangle

Firstly, it’s important that we name the sides of the triangle so that we can identify them when we build our equations later. In trig, we name sides based on their relationships to the corners of the triangle. Take this triangle as an example:

Boring right-angled triangle with angle ?

In this triangle, we’ve defined one of the non-right-angles with the variable theta (?). For now, this is simply a variable to identify the corner – don’t worry about about how the numbers fit in just yet!

Now that we’ve defined ?, we can use it to identify our sides.

Right-angled triangle with angles and sides defined

Now we’ve designated our sides. In trig, sides are always designated relative to the angle that we’re working with, ? in this case. o is the side opposite to the specified angle. a is the angle adjacent (next to) the specified angle. h is the hypotenuse, which is always the side opposite to the 90 degree angle. Clear? Well, if not, let’s go through the same exercise with the other angle in the triangle. Let’s designate that one Alpha (?), and take a look at how this affects our sides.

Right-angled triangle with angle alpha, and sides

Note that this triangle is still the same triangle. We’ve just renamed the sides according to which corner we’re viewing them relative to, in exactly the same way that we did with angle ?. You’ll note that h is still assigned to the same side. As I mentioned, this is because the hypotenuse is always the side opposite to the 90 degree angle, and so won’t change if we switch corners. If this still seems a little confusing, don’t worry – next we’ll be seeing how all of this ties together!

## Trigonometric Ratios

Trigonometry is not a new thing. We owe a lot of our geometrical knowledge to ancient Greek mathematicians, such as Pythagoras, who dedicated a lot of time to figuring out the relationships between the sides and angles of shapes. One of the many things that the Greeks managed to determine was that the lengths of the sides of right-angled triangles were subject to certain ratios, extrapolated relative to a triangle’s corners in the way that was discussed in the previous section. From this, they derived common mathematical functions to use these ratios for their calculations. For ease of use, they gave names to the six functions that they discovered. We know them as sine (sin), cosine (cos), tangent (tan), cotangent (cot), secant (sec), and cosecant (csc).

The ratios that the names describe are as follows. Note that the ? after the ratio names denotes which angle we are taking these sides relative to.

$$\sin \theta = \frac{o}{h}$$

$$\cos\theta = \frac{a}{h}$$

$$\tan \theta = \frac{o}{a}$$

$$\sec \theta = \frac{h}{a}$$

$$\csc \theta = \frac{h}{o}$$

$$\cot \theta = \frac{a}{o}$$

This is all good and fine, but the real magic comes in when we slot numbers into those variables. Time for some good old-fashioned maths!

## Basic Calculations

Let’s take the triangle that we’ve been working on and plug some numbers into it:

Triangle

Now we have numbers to work with! However, you’ll notice that side o and a haven’t been given numbers. We have no idea what the lengths of those sides are. That means, of course, that we’ll have to calculate them!

Calculating anything using trigonometry requires three steps:

1. Determine what numbers you have, and what numbers you want.
2. Determine which ratio to use, and build your equation.
3. Substitute the known values into the equation and solve for the unknown.

That may seem like quite a brainful, but once you see it in action you’ll appreciate its simplicity!

First, we take a look at what we have: we have values for ? (45°) and h (6). We want to calculate o and a. Next, we determine which ratios we need to use. Generally, we decide that based on the formula below.

$$\rm{ratio\:angle} = \frac{unknown\:side}{known\:side}$$

Let’s calculate o first. We’ll use h as our known value. As a result, we have to calculate . Looking familiar? Look back to the table where I outlined the ratios. If you read carefully, you’ll see that corresponding ratio is sin. Our equation now looks like this:

$$\sin 45^\circ = \frac{o}{h}.$$

And when we plug our numbers in and solve for o:

$$\sin 45^\circ = \frac{o}{6}$$

$$0.707 = \frac{o}{6}$$

$$4.243 = o.$$

Note: You can easily use the Windows calculator in Scientific mode to work with trig ratios. Simply ensure that the calculator is set to “degrees”, then punch in the angle (45 in this case) and click the “sin” button to get the result above (0.707). I’ve rounded the answer off to three decimals for convenience.

And there you have it! We’ve calculated o to be 4.243 units long! Now try calculating a (hint: you’ll be using cos as your ratio – check the list again to see why). You should get an answer of about 4.243, the same as side o. The magic of 45 degree angles!

## What? Out of space already?

That’s all for this month, but despair not! Next month, we’ll be looking at how to calculate unknown angles, and you’ll also get a taste of how you can start using trig methods in your games. Until then, practice calculating unknown sides using different ratios and values. I’ll see you next month!