atan和atan2反正切计算

typedef struct point {
	double x, y;
}point;
//给定两个点
point a(x1,y1),b(x2,y2);
  1. 使用反三角函数atan求斜率,原型如下
float       atan( float arg );	
double      atan( double arg );
long double atan( long double arg );
double      atan( Integral arg );
double angle=atan((y2-y1)/(x2-x1));

返回值
若不出现错误,则返回 arg 在$ [- π/2 ; +π/2]$ 弧度范围中的弧(反)正切( $arctan(arg) $)。值域有限,一四象限,斜率不存在不能求

  1. 使用反三角函数atan2求斜率,原型如下
float       atan2( float y, float x );
double      atan2( double y, double x );
long double atan2( long double y, long double x );
Promoted    atan2( Arithmetic1 y, Arithmetic2 x );

返回值
若不出现错误,则返回 y/x 在 $(-π ; +π] $弧度范围中的弧(反)正切( arctan(y/x) )。值域扩展到四个象限。
atan2(y,x)所表达的意思是坐标原点为起点,指向(y,x)的射线x轴正方向形成角的角度。在x=0的时候:
1.当y>0时,指的是绕逆时针到达射线所旋转的角的角度;
2.而当y<0时,指的是绕顺时针达到射线所旋转的角的角度。

这样就可以求两个点表示的线段(向量)和x轴正向的角度,如下

double angle=atan2((y2-y1),(x2-x1));
原文地址:https://www.cnblogs.com/FlyerBird/p/9326102.html