View Single Post
Old 28th April 2003, 05:47   #72
AVS Axer
AVS Axer's Avatar
Join Date: Feb 2003
Location: Ozshtrayleeuh
Posts: 98
somone asked how atan2 works:

in case you didnt already know.. asin, acos and atan are the inverse functions of sin cos and tan.

dont be confused however and think that asin(sin(x)), will neccessarily return x. Thnk of a circle and acknowledge that no matter how many times you got around the circle, sin(x) will always be the same at the same point of the circle. Now imagine that x is more than 2*pi (the circumference), this is just like going around a circle more than once, so all the values will be the same.

The trouble with asin, acos and atan is that they do not even return the right values of x all the way around a circle!! Dont get me wrong, they have their uses. Consider sin(x) again, this finction will return 0 in two different positions while 0>x>2pi. one at 0 and one at pi. Now you can see that asin(sin(x)) will definatly not return the right values of x, because sin(x) has a pattern, but x does not.

In reality, asin(sin(x)) will return x for only the first quadrant,
After that x is reset to 0 at the start of every new quadrant. And with these rules.
First quadrant: Asin(sin(x)) = x
second quadrant:Asin(sin(x)) = 2pi-x
third quadrant: Asin(sin(x)) = -x
fourth quadrant:Asin(sin(x)) = -2pi+x

Look at a unit circle or play around in avs and youll get how this works. Try this in a superscope:
and try replacing atan and tan with asin and sin. etc.

To counter this, atan2 was invented.. pretty much you input two coordinates of a point (x,y) and it does something similar to atan(x/y) but it acknowledges that they are coodinates of the same point and works it out in some clever way that i dont yet know.

So, atan2(x,y) returns the angle of the point x,y in radians from center (0,0).
If you look at alot of code however, people in avs have tended to use atan2(x,-y). This is because the y axis in avs is fliped. In trig we start from the bottom and goto top. I dont know why..

It is also convention to start from the right at -1 and got anti clockwise around the circle.. (first quadrant is at top left). So i dont know why we arnt using atan2(-x,-y) but i wont argue because fsk and unconed use the other way, and they seem to know their shit.

Correct me on anyof this please.. cause i could have easily have got confused. Oh, and if someone knew exactly how the function is calculated, it would be most helpful.

Last edited by AVS Axer; 28th April 2003 at 06:28.
AVS Axer is offline   Reply With Quote