Trigonometry is a really useful tool to have in your coder's utility belt, and lots of people have a hard time grasping Sine and Cosine (and Tangent), so here's a crash course tutorial on their use.
Trigonometry is the math of
right triangles and
First, let's look at SOH, CAH, TOA:
- SOH = Sine equals Opposite over Hypotenuse
- CAH = Cosine equals Adjacent over Hypotenuse
- TOA = Tangent equals Opposite over Adjacent
But what does this mean?
The Hypotenuse of a right triangle is the longest of the three sides. It's always on the opposite end of the right angle. The other two sides aren't uniquely named.
So depending on our situation, we can use one of the three equations to find out all we need to know about a right triangle. All we need is the length of one of the three sides and the degree of one of the two non-right angles. Refer to this diagram (not to scale):
If we replace those sides with
b, with the Hypotenuse being
c, with the angle we know being
ang, we can use this equation to find the length of side
b. Here is a Lua example:
Believe it or not, these basic right triangle calculations have SO MANY USES.
2200 years ago, mathematicians used these functions to calculate the radius of Earth. It's used literally every second of every day in GPS software (hence the term triangulation). It's used by police to track the location of a phone call. SOH CAH TOA is the foundation of Trigonometry.
When we investigate the origins of Sine and Cosine, we can learn a thing or two about the fundamental properties of a circle. Consider the following:
We have a circle with radius 1. In this circle, we have radius line which is tilted 30 degrees (counterclockwise, always counterclockwise) from the circle's right horizontal axis. Because it is a radius line, its length is equal to the radius of the circle, which is 1 in this case.
So what is the length of b? If we recall SOH, we see that Sin( ang ) = Opposite / Hypotenuse. Plug in our values, and Sin( 30 ) = b / 1. Seeing as how any number divided by 1 is itself, we can conclude that:
In a circle of radius 1, the Y-value of a point on a circle relative to the center of the circle is equal to the sine of the angle. In a circle of radius 1, the X-value of a point on a circle relative to the center of the circle is equal to the cosine of the angle.
Which we can see here:
But not all circles are of radius 1. When we have a circle that is larger than radius 1, we just multiply.
Furthermore, we've been assuming that our circle is centered at point (0,0). If our circle's center is (100,453), we add. This is our complete formula:
This is useful in GLua for drawing circles, arranging props in circles, and basically any other circle-based code. Here is an example which draws squares:
WOW! SO USEFUL!
Radial menus, circular health bars, chairs arranged around a circular table, ellipses, sound waves, and
MUCH MORE are possible with Trigonometry.
The inverse function of a Trigonometric function is called the arc-function.
Arc functions work like so:
These are useful for doing reverse math, for example:
These functions are useful if the unknown variable in question is your angle. There is a special Arc-Tangent function for programming called math.atan2.
Think of tan(ang) as giving the slope - the rise over the run - of a line. So:
However, when performing y/x, calculating 3/4 gives the same answer as calculating -3/-4. Likewise -3/4 gives the same answer as 3/-4. So we have atan2(y,x) that handles the individual signs correctly and prevents a divide-by-zero/infinity error.
atan2's use is to get the angle of a line with a slope
m as in the equation
Trigonometry is required to create any circular structure, and to do math with right triangles. It is also useful to find the angle of a line given its slope.
I hope you learned something