calculation of angles

Lev Gorenstein lev at
Fri Dec 17 20:37:07 EST 1999

On Fri, 17 Dec 1999, Mark wrote:

M> I see that in many modelling programs
M> where an angle theta is to be calculated, it is very often
M> calculated as atan2(sin(theta),cos(theta)), where atan2,
M> according to the manual,
M> is a FORTRAN function approximately equal to
M> atan(sin(theta/cos(theta)).
M> I don't understand:
M> (a) why atan2(y,x) is preferred to atan(y/x);

Probably, atan2 does better job in determining the sign of the
angle (see below).
M> (b) why one couldn't directly take acos(cos(theta)) or
M> asin(sin(theta)), instead of having to obtain both sine and cosine.
M> (the cosine of angle is easy to obtain, but sine is not).

Think trig.  Let's say the angle that you're calculating is 60
degrees.  You calculate cosine (from the structure), take acos(0.5)
and get 60 degrees.  Everything is fine.  Now imegine, that the
angle is in fact -60.  You calculate cosine, but cos(-60) = cos(60), 
so the cosine is 0.5, and when you take acos, you would end up with
60 degrees, rather than -60.  So you are in trouble because of trig
equality cos(-x) = cos(x).

Same thing if you do it with with sine.  The fact that 
sin(x) = sin(180-x) would not allow you to distinguish between,
say, 30 and 150 degrees, or 210 and 330.

Therefore, you have to analize BOTH sine and cosine of your angle,
and assign a value according to the signs of both sine and cosine:

  0 < x < 90:	sin(x) positive, cos(x) positive
 90 < x < 180:	sin(x) positive, cos(x) negative
180 < x < 270:	sin(x) negative, cos(x) negative
270 < x < 360:	sin(x) negative, cos(x) positive

Thus, if you use atan(sine(x)/cosine(x)), and, for example, the
argument is positive, you would not know, whether it's because
the angle is in the first quadrant (both sine and cosine are
positive), or it is in the third quadrant (both are negative, thus
giving a positive tangent).  The atan() function will return a
value from the first quadrant.  It's my understanding that atan2
function does the above mentioned analisys of signs (since the
sine and cosine are supplied separately), and thus returns
the true value of angle.


186,282 miles per second: 
It isn't just a good idea, it's the law!

More information about the Molmodel mailing list