Atan and atan2 in C language and atanatan2 in C Language
The content of this article is reproduced, is to read the RTKLIB source code when aware of this problem, the original address: https://www.cnblogs.com/dutlei/archive/2013/01/14/2860332.html
In the C language math. in h or C ++, The cmath contains two antitangent functions: atan (double x) and atan2 (double y, double x) the returned values are radians that need to be converted to degrees and then processed by themselves.
The former accepts a tangent value (the slope of the straight line) to obtain the angle, but because of the tangent regularity, there can be two angles, but it returns only one, because the atan value ranges from-90 ~ 90 means that it only processes One quadrant, so it is generally not used.
In the second atan2 (double y, double x), y represents the Y coordinate of a known point, which is the same as x. The return value is the angle between the point and the distant point and the positive direction of the x axis, in this way, it can handle any situation of the four quadrants, and its value range is-180 ~ 180
For example:
Example 1: angle of a straight line with a slope of 1
Cout <atan (1.0) * 180/PI; // 45 °
Cout <atan2 (1.0, 1.0) * 180/PI; // The First quadrant of 45 °
Cout <atan2 (-1.0,-1.0) * 180/PI; // third quadrant of-135 °
The last two slopes are both 1, but atan can only find one 45 °.
Example 2: The slope is the angle of the-1 straight line.
Cout <atan (-1.0) * 180/PI; //-45 °
Cout <atan2 (-1.0, 1.0) * 180/PI; //-45 ° y is negative in the Fourth quadrant
Cout <atan2 (1.0,-1.0) * 180/PI; // 135 ° x is negative in the second quadrant
The commonly used method is not to find the angle of a straight line from the origin point. It is usually to find the angle of a line segment. This is even better for atan2.
For example, calculate the angle between A (1.0, 1.0) B (3.0, 3.0) and the positive direction of the X axis.
Represented as atan2 (y2-y1, x2-x1) as atan2 (3.0-1.0, 3.0-1.0)
The principle of it is equivalent to the point to the origin B point corresponding to B '(x2-x1, y2-y1) point so that it returns to the previous
Example 3:
A (0.0, 5.0) B (5.0, 10.0)
The angle of line AB is
Cout <atan2 (5.0, 5.0) * 180/PI; // 45 °