In C or C++ cmath, there are two functions that find arctangent atan(double x) and atan2(double y, double x) that they return is radians and they need to convert them into angles and then handle them yourself.
The former accepts a tangent value (the slope of the line) to obtain the included angle, but due to the regularity of the tangent, there could have been two angles, but it only returns one, because the range of atan is from -90~90, which means it only processes one and four quadrants, so it is generally not used.
The second atan2(double y, double x) where y represents the Y coordinate of a known point. Similarly, x, the return value is the angle between the line connecting this point and the far point and the positive direction of the x-axis, so that it can handle any situation in the four quadrants, and its value range corresponds to -180~180.
For example:
Example 1: The 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;//45° First Quadrant
cout<<atan2(-1.0,-1.0)*180/PI;//-135° Third Quadrant
The last two slopes are 1, but atan can only find one 45°
Example 2: The 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;//-45° y is negative in the fourth quadrant
cout<<atan2(1.0,-1.0)*180/PI; //135° x is negative in the second quadrant
Commonly used is not to find the angle of a straight line passing through the origin, but often to find the angle of a line segment. This is even more like a fish in water for atan2.
For example, find the angle between A(1.0, 1.0) B(3.0, 3.0) and the positive direction of the x-axis
Denoted by atan2 as atan2(y2-y1,x2-x1), that is, atan2(3.0-1.0,3.0-1.0)
Its principle is equivalent to shifting point A to origin point B to correspondingly become point B' (x2-x1, y2-y1) and so it returns to the previous one
Example 3:
A(0.0,5.0) B(5.0,10.0)
The angle of line segment AB is
cout<<atan2(5.0,5.0)*180/PI;//45°
The above is the relevant content I have compiled, and I hope it can help everyone.