Trigonometry 101
Tutorial on Basic Trig Concepts to Help the Advanced Scripter
By ZephyrsFury
What is Trigonometry
Trigonometry is a type of maths that deals with triangles. Trigonometry literally means “triangle measure” and deals with finding angles and lengths in a triangle.
Trigonometry (from Greek
trigōnon "triangle" +
metron "measure"[1]), informally called trig, is a branch of mathematics that deals with triangles, particularly triangles in a plane where one angle of the triangle is 90 degrees (right angled triangles). It specifically deals with the relationships between the sides and the angles of triangles; the trigonometric functions, and calculations based upon them. The insights of trigonometry permeate other branches of geometry, such as the study of spheres using spherical trigonometry. Trigonometry has important applications in many branches of pure mathematics as well as of applied mathematics and, consequently remains applicable in natural sciences.
Using Trigonometry, you can solve any triangle (find all lengths and angles) provided that you have at least two lengths and an angle. Since I’ll only be teaching you basic stuff (solving right angled triangles) you only need two sides to find an angle or one angle and one side to find another side.
Why Is Trigonometry Important?
To us scripters, because we use a graphical computer and SCAR and because we script for Runescape, Trigonometry is extremely important. This is because computer screens use a Cartesian coordinate plane to specify each pixel. That is any point on the screen has an x coordinate and a y coordinate. I’ll be explaining the Cartesian coordinate plane later. SCAR also uses a Cartesian plane to move your mouse, store the coordinates of a colour and to find distances and angles between two points. Since the x and y axis are at right angles to each other, we can use simple trig to find distances and angles between points.
Another reason why trig is important to us is because some inbuilt SCAR and SRL functions use angles to specify search areas for colours and DTMs. For example, the famous RadialWalk requires you to specify two angles and a radius for it to search for the colour in. RadialWalk then converts this sector into a search area where it searches for a colour in. It does this by using trigonometry to convert the angle and radius into an x, y coordinate for use with FindColorTolerance. Also, FindDTMRotated which is commonly used to find a rotated DTM on the minimap requires two angles that specify how much the function rotates the DTM by. These angles aren’t in the usual degrees but are in radian measure which is a better method of angle measurement when working with circles.
The Three Trigonometric Ratios
Before we get down and dirty with all the calculations we need to understand the basis on which trig is based on. We need to understand the three common trig ratios: Sine, Cosine and Tangent, or sin, cos, tan. These are the ratios between the sides of a right angled triangle. There are more complex ones but they are useless to what we are trying to do. Before we can discuss these ratios you need to understand the way the sides of a right angle triangle is named:
The side directly opposite to the triangle is called the opposite for good reason. The opposite the right angle is the hypotenuse. The side next to the angle but not the hypotenuse is called the adjacent.
Now that you understand that we can go and look at what the trig ratios are:
O is opposite, H is hypotenuse, A is adjacent.
Now you might ask why these ratios are so important.... well read on....
Finding Distances and Angles using Trig
So I’ll now be showing you how to find angles and distances using basic trig methods. I’m hoping that you know how to transpose equations (moving the values around so you can make something equal to something else). Anyway... by transposing any of the equations above (the trig ratios), you can manipulate it to find any the angle or either of the two sides provided that you know what at least two of the three are.
Lets look at an example (please excuse my crap Paint diagrams):
Solve for x in this diagram:
- We must find x by using one of the trig ratios.
- To decide which one, we must look at the information given to us.
- We can see that we are given the hypotenuse and an angle and we are trying to find the opposite side.
- When we look at the trig ratios we see that the one that uses both opposite and hypotenuse is sin.
- We write down the sin ratio:
- Substitute in the values we are given:
- Rearrange the equation to give us x:
- Shove that into the calculator to get the value of x:
So now you’ve just found the length of a side without measuring it . The same method occurs with the other trig ratios. Just remember to check what information you have then choose the correct ratio. Also remember to transpose stuff correctly. For example if we were give the opposite side and were asked to find the hypotenuse:
When we rearrange we get a different equation:
If we are given the length of two sides and were asked to find an angle, the basic procedure is the same:
- We must find angle X by using one of the trig ratios.
- We are given the opposite and the adjacent.
- When we look at the trig ratios we see that the one that uses both opposite and adjacent is tan.
- We write down the sin ratio:
- Substitute in the values we are given:
- Now that we have the equation equal to tanX, we need to find the angle X. We do this by using inverse tan or Arc Tan.
- By using the calculator (or SCAR) we can see the angle is 48.59 degrees rounded to two decimal places.
Once again the same method applies to sin and cos.
Finding Distance – Pythagoras’ Theorem
Now that you’ve had your crash course in the trig ratios you might want to use a different method of finding distances between two points. To use this method, we need two sides of the right angle triangle, usually the opposite and adjacent sides.
In the diagram above we want to find X. To do this we need to know the Pythagorean Theorem:
Sound confused?:
O being opposite, A being adjacent and H being hypotenuse. This is the basis for SCAR’s Distance function.
To solve for the X in the diagram above we take the following procedure:
- Write out the formula:
- Sub in the values we know:
- Solve for H:
Ok I realised that you might be confused. After all, I don’t see any right angle triangles with random letters all over my screen – certainly not in RS anyway. Let me try to explain to you why the coordinate plane can be used in trigonometry:
This diagram shows the two points and their coordinates:
The distance between those points would be the black line:
Now to use Pythagoras, we need to draw a triangle:
Since we know the coordinates of the points, we can take the smaller x coordinate from the greater x coordinate to give use the horizontal distance between the points. We can do the same with y coordinates to find the vertical distance.
Now that we have the two sides, we can find the length of X:
For those people that are lazy, here’s a simplified formula for finding the distance between two points without having to work out horizontal and vertical distances:
The same principle with the coordinate plane applies to trig ratios.
Cartesian and Polar Coordinates
Since the computer and SCAR used the Cartesian coordinate plane, most SRL and SCAR functions use x and y variables such as FindColor which saves the x and y coordinates into your x, y variables and requires you to specify the search area using x1, y1, etc.
You should already be very familiar with the Cartesian plane. Cartesian coordinates are the most commonly used method of specifying the position of a point. Usually, the x coordinate is the horizontal distance from the origin. The Y coordinate is the vertical distance from the origin. The only problem with using Cartesian coordinate geometry with a computer is that the Y axis is inverted. That is as you go down the screen, the y coordinates increase instead of decreasing in a normal Cartesian plane. That means everything is flipped horizontally.
Another method of specifying the position of points is by using Polar coordinates. Polar coordinates do not use x and y but instead use radius (or distance from the origin) and angle (from the positive x axis and going anticlockwise). This method is used in RadialWalk and FindColorCircle where you are required to specify the centre, the radius and the angle. This method is usually useful in places where the graph is required to be round. This is why I personally think we should use polar coordinates on the minimap. .
Here’s what the polar coordinate plane looks like:
Basically it’s a series of concentric circles (they share the same centre) with lines sticking out to represent the angles. Notice the coordinate of the point in the graph above. When using Polar coordinates instead of the ordered pair being (x, y), it is (r, theta). R is the radius, theta is the angle from the positive x axis.
Converting Polar to Cartesian and Vice Versa
When using two different coordinate planes, it is important that you know how to convert it back and forth. To do this we need to take a look at our right angle triangle again:
We need to find the polar coordinate of the point A.
- Since R is the distance from the origin, we can use Pythagoras to find it:
- Since theta is the angle between the horizontal and the point, we can use tan to find theta:
- Therefore the Polar coord of the point (50, 70) is (86.023, 54.46)
Usually if the answer is a long decimal then we leave it in its exact value. i.e. with the square root (a surd).
To convert polar coordinates back into Cartesian coordinates we need to use the trig ratios above. We need to convert the polar point (2, 45°) into Cartesian coords:
- We need to find x and y. Remember when I talked about the trig ratios I said that you can find the length of any side provided you have one length and one angle. With the point above, the x coordinate is the adjacent side to the angle while y is the opposite side.
- By using the cos and sin ratios we can find x and y:
- Same thing with y except by using cos:
- So then cartestian coordinate of (2, 45°) is approx (1.414, 1.414).
To simplify:
Different Angle Measurements – Radians and Degrees
Most if not all or you would be familiar with degrees. You’ve probably been learning it since primary school. Now I’ll introduce you to another type of angle measurement – Radians. Radians are more powerful and work better with circles. This is because radians are based on Pi. You might know that the circumference of a circle is:
So the length if an arc (part of the radius) would be:
Θ is the angle (in degrees) that defines the arc.
If you are using radians however, the length of an arc would be:
This is because 360° is 2*Pi thus it cancels out.
The reason I’m explaining this is because SCAR’s native angle measurement is radians (all its inbuilt angle-related functions use radians). That’s why it helps to know how to use radians.
Here’s some common conversions:
From these we can convert any angle to radians then back to degrees:
- Convert 105° to radian measure:
Now the other way:
- Convert Pi/8 to degrees:
-
Inputting Maths into SCAR
So now that you’ve learn all the trig you must be wondering “how the hell am I supposed to put this into SCAR”. After all, I don’t see and fancy square root signs or cos keys on my keyboard.
I’ll go through the most common maths operators and functions that are the equivalent to the maths operators I used here.
Plus or subtraction is easy because we have the + and the – signs in the keyboard already. Multiplication and division uses the * and the / respectively. When you want to use the exact value of Pi (3.141592654...) we use constant “Pi” which is inbuilt into scar.
To square something we can go multiply the number together or we can use SCAR’s Sqr function:
SCAR Code:
function Sqr(X: Extended): Extended;
To get the square root of a number we use the sqrt function:
SCAR Code:
function Sqrt(e: Extended): Extended;
To use one of the trig ratios we must use these functions:
SCAR Code:
function Sin(e: Extended): Extended;
SCAR Code:
function Cos(e: Extended): Extended;
SCAR Code:
function Tan(e: Extended): Extended;
Where e is the angle IN RADIANS.
If you want to use degrees you must either convert it into radians by using the Radians function:
SCAR Code:
function Radians(Degrees: Extended): Extended;
or you can use SRL’s Cose and Sine functions. These functions use degrees instead. There is no Tan function though.
To get inverse tan, cos or sin we must use SCAR’s ArcTan, ArcSin and ArcCos functions:
SCAR Code:
function ArcTan(e: Extended): Extended
//same with ArcCos and ArcSin.
Remember that its returns the angle in RADIANS.
SCAR also has functions that do pretty much all that I covered above:
SCAR Code:
function Distance(x1, y1, x2, y2: Integer): Integer;
//Returns Distance between Points.
SCAR Code:
function Radians(Degrees: Extended): Extended;
//Converts Degrees to radians
SCAR Code:
function Degrees(Radians: Extended): Extended;
//Converts radians to degrees
They also have a range of polar to Cartesian and vice versa functions that I haven’t used. Check the SCAR Manual for a list.
So thats it for now. Hope you learnt something. Geez... my tutorials are long aren't they.
Zeph.