Reputation:
I am using UIAcceleration
for rotation. I have opposite side, adjacent side, but I want to calculate tan-1(y/x) inverse tan.
Upvotes: 0
Views: 7664
Reputation: 5654
atan2(y, x)
is the same thing as atan(y/x)
, but it can deal properly with the case where x = 0
(i.e. a vertical line going up or down) without having do deal with positive vs. negative infinity.
Upvotes: 2
Reputation: 43452
The standard C math.h functions are available:
#include <math.h>
...
float theta = atan2f(y, x);
...
Upvotes: 4
Reputation: 189626
Always use atan2(y,x) instead of atan(y/x), for two reasons. One is mentioned by David Maymudes (problems with x=0). The other is that atan2() deals with the full range from -π to +π, whereas atan() only gives you an output between -π/2 and +π/2, and cannot distinguish between (x,y)=(2,2) and (x,y)=(-2,-2) since you lose the sign information when you do the division and pass only the quotient y/x into atan().
Upvotes: 8