What is the difference between atan,atan2? This article introduces the use and conditions of ATan2.
For tan (θ) = y/ x:
θ= ATan (y/ x) The range of θ values is [-PI/2, PI/2].
θ= ATan2 (y, x) The range of theta values is [-pi, PI].
When (x, y) in the first quadrant, 0 < θ< pi/2.
When (x, y) in the second quadrant PI/2 < θ≤PI.
When (x, y) in the third quadrant,-PI < θ<-pi/2.
When (x, y) in the fourth quadrant,-PI/2 < θ< 0.
When the point (x, y) is on the axis of the quadrant:
When Y is 0,x is non-negative, θ= 0.
When y is 0, x is negative, θ= PI.
When y is positive, X is 0,θ= PI/2.
When y is a negative value, X is 0,θ=-PI/2.
As a general rule, the Atan can be used and the ATan2 should be used when there are special requirements for the range of the angle to be obtained.
Atan and atan2 are all seeking the inverse tangent function, such as: There are two points (x1,y1), and Point (X2,y2);
Then the angle calculation method of the slope formed by these two points is:
float angle = Atan ((y2-y1)/(X2-X1));
Or
float angle = atan2 (y2-y1, x2-x1);
Atan and atan2 differences:
1: The parameters are filled in different ways;
The advantage of 2:atan2 is that if x2-x1 equals 0 can still be calculated, but the Atan function will cause the program error;
Conclusion: Atan and atan2 function are suggested with atan2 function.
The difference between Atan and atan2