c++中的atan和atan2有什么区别?


当前回答

在atan2中,输出为:-pi < atan2(y,x) <pi 在atan中,输出是:-pi/2 < atan(y/x) < pi/2 //它不考虑四分之一。 如果你想要得到0到2*pi之间的方向(就像高中数学一样),我们需要使用atan2,对于负值,加上2*pi来得到0到2*pi之间的最终结果。 下面是Java源代码来解释清楚:

System.out.println(Math.atan2(1,1)); //pi/4 in the 1st quarter
System.out.println(Math.atan2(1,-1)); //(pi/4)+(pi/2)=3*(pi/4) in the 2nd quarter

System.out.println(Math.atan2(-1,-1 ));//-3*(pi/4) and it is less than 0.
System.out.println(Math.atan2(-1,-1)+2*Math.PI); //5(pi/4) in the 3rd quarter

System.out.println(Math.atan2(-1,1 ));//-pi/4 and it is less than 0.
System.out.println(Math.atan2(-1,1)+2*Math.PI); //7*(pi/4) in the 4th quarter

System.out.println(Math.atan(1 ));//pi/4
System.out.println(Math.atan(-1 ));//-pi/4

其他回答

Std::atan2允许计算所有四个象限的arctan。Std::atan只允许从象限1和4计算。

考虑一个直角三角形。我们把斜边标记为r,水平线为y,垂直线为x。兴趣角α是x和r之间的角。

c++ atan2(y, x)会给出角α的值,单位为弧度。 如果我们只知道或只对y/x感兴趣,而不是y和x单独,则使用Atan。如果p = y/x 然后用atan(p)求α。

您不能使用atan2来确定象限,只有当您已经知道您在哪个象限时才能使用atan2 !特别是正的x和y表示第一象限,正的y和负的x,第二象限等等。Atan或atan2本身只是返回一个正数或负数,仅此而已。

使用atan2,您可以确定这里所述的象限。

如果需要,可以使用atan2 确定象限。

Atan2 (y,x)通常用于将直角坐标转换为极坐标。它会给你角度,而根号(x*x+y*y)或者,如果有的话,hypot(y,x)会给你大小。

Atan (x)就是tan的逆。在烦人的情况下,你必须使用atan(y/x)因为你的系统不提供atan2,你必须做额外的检查x和y的符号,以及x=0,为了得到正确的角度。

注意:atan2(y,x)定义为y和x的所有实值,除非两个实参都为零。

下面的Mehrwolf是正确的,但这里有一个启发式可能会有所帮助:

如果你在一个二维坐标系中工作,这是编程反切线的常见情况,你肯定应该使用atan2。它会给出完整的2范围的角度并为你照顾到x坐标中的0。

另一种说法是,atan(y/x)总是错的。只有当参数不能被认为是y/x时才使用atan。