How do you use math.Sin, math.Cos, math.Tan effectively in scripting and knowing when to use them.
For example I see in many scripts they use cos and sin ALOT, but what I don't understand is how they use it in scripting and when they know how to use it.
It took me a while to start using them as well, simply because schools teach you about trigonometry way too late(at least for me). I will try to show you few basics of trigonometry, that should get you going.
So trigonometry focuses on right triangles. Here is a picture that you should keep open while reading further information: http://puu.sh/aYOgL.png Since I cannot use symbol that defines angle on here, I'll refer to it as alpha(which is full name of that symbol).
So the basic idea is that you can find out length of any side, by knowing at least one side, and angle. To do so, you must know three trigonometric equations, which are:
sin alpha = a/c
cos alpha = b/c
tan alpha = a/b
Now if you look at the previous picture, you can see that ? is one of the two acute angles, c is the longest side(also known as hypotenuse) and C will always be the corner with right angle.
Let's say we know how big is alpha and we know length of c side. So to acquire side a, we have to transform our sin alpha = a/c
formula, which would go like:
sin alpha = a/c
sin alpha * c = a
a = sin alpha * c
That's the basic idea of trigonometry, but there is one more cool use of it, especially useful in programming and for visual things.
There is this thing called as trigonometric function unit circle, which looks like this: http://puu.sh/aYPch.png
Now that we know the trigonometric functions, we can apply them to find out coordinates of point P by knowing current angle. This is very useful for some neat GUIs and object placement generally.
x = cos alpha * r
y = sin alpha * r
So that's about it.
Some noteworthy statements:
* sin alpha
will always be a value between -1 and 1. Essentially -1 >= sin alpha >= 1
. Same goes with cos alpha
.
* Angle alpha
must be in radians, so make sure to use math.rad to convert degrees to radians.
* In case you want to find out angle, by knowing two sides of triangle, you have to use arc functions, that will find out angle that trigonometric formula is representing. Example:
Let's say we know sides a
and c
, and you want to know angle alpha
.
First we find appropriate formula, which is: sin alpha = a/c
Now we know that sinus of alpha equals to a/c
. To find angle in radians, we use this function:
asin
So our formula now looks like: alpha = asin(a / c)