What is the difference between atan
and atan2
in C++?
在c++中,atan和atan2有什么区别?
10 个解决方案
#1
82
std::atan2
allows calculating the arctangent of all four quadrants. std::atan
only allows calculating from quadrants 1 and 4.
atan2允许计算所有四个象限的反正切。atan只允许从象限1和象限4进行计算。
#2
242
From school mathematics we know that the tangent has the definition
从学校数学中我们知道正切有定义
tan(α) = sin(α) / cos(α)
and we differentiate between four quadrants based on the angle that we supply to the functions. The sign of the sin
, cos
and tan
have the following relationship (where we neglect the exact multiples of π/2
):
我们根据供给函数的角度来区分四个象限。罪恶的符号,因为和褐色有以下关系(我们忽视的π/ 2的倍数):
Quadrant Angle sin cos tan
-------------------------------------------------
I 0 < α < π/2 + + +
II π/2 < α < π + - -
III π < α < 3π/2 - - +
IV 3π/2 < α < 2π - + -
Given that the value of tan(α)
is positive, we cannot distinguish, whether the angle was from the first or third quadrant and if it is negative, it could come from the second or fourth quadrant. So by convention, atan()
returns an angle from the first or fourth quadrant (i.e. -π/2 <= atan() <= π/2
), regardless of the original input to the tangent.
鉴于tan(α)是积极的价值,我们不能区分,从第一或第三象限角是否如果是负的,它可能来自第二或第四象限。所以按照惯例,每股()返回一个角度从第一或第四象限(即-π/ 2 < =每股()< =π/ 2),不管最初的切线的输入。
In order to get back the full information, we must not use the result of the division sin(α) / cos(α)
but we have to look at the values of the sine and cosine separately. And this is what atan2()
does. It takes both, the sin(α)
and cos(α)
and resolves all four quadrants by adding π
to the result of atan()
whenever the cosine is negative.
为了得到完整的信息,我们不能使用部门的结果sin(α)/ cos(α),但我们必须考虑单独的正弦和余弦值。这就是atan2()的作用。罪(α)和需要,因为(α)和解决所有四个象限加上π作用的结果:()当cos是负的。
Remark: The atan2(y, x)
function actually takes a y
and a x
argument, which is the projection of a vector with length v
and angle α
on the y- and x-axis, i.e.
备注:量化(y,x)函数实际上y、x的论点,这是一个向量的投影长度为v和角α在y轴,即。
y = v * sin(α)
x = v * cos(α)
which gives the relation
这使的关系
y/x = tan(α)
Conclusion: atan(y/x)
is held back some information and can only assume that the input came from quadrants I or IV. In contrast, atan2(y,x)
gets all the data and thus can resolve the correct angle.
结论:atan(y/x)保留了一些信息,只能假设输入来自象限I或IV。相反,atan2(y,x)获取所有数据,从而可以解决正确的角度。
#3
17
Another thing to mention is that atan2
is more stable when computing tangents using an expression like atan(y / x)
and x
is 0 or close to 0.
另外要注意的是,当使用atan(y / x)和x = 0或接近0的表达式计算切线时,atan2更加稳定。
#4
15
The actual values are in radians but to interpret them in degrees it will be:
实际值是用弧度表示的,但要用度数来表示:
-
atan
= gives angle value between -90 and 90 - atan =给出-90和90之间的夹角值
-
atan2
= gives angle value between -180 and 180 - atan2 =给出-180和180之间的角值
For my work which involves computation of various angles such as heading and bearing in navigation, atan2
in most cases does the job.
对于我的工作涉及到计算不同的角度,如导航中的航向和方位,atan2在大多数情况下都是这样做的。
#5
8
atan(x) Returns the principal value of the arc tangent of x, expressed in radians.
atan(x)返回以弧度表示的arctanx的主要值。
atan2(y,x) Returns the principal value of the arc tangent of y/x, expressed in radians.
atan2(y,x)返回arctany /x的主值,用弧度表示。
Notice that because of the sign ambiguity, a function cannot determine with certainty in which quadrant the angle falls only by its tangent value (atan alone). You can use atan2 if you need to determine the quadrant.
注意,由于符号不明确,函数不能确定在哪个象限中,角度仅由其切值(atan单独)决定。如果需要确定象限,可以使用atan2。
#6
3
With atan2 you can determine the quadrant as stated here.
用atan2你可以确定象限如这里所述。
You can use atan2 if you need to determine the quadrant.
如果需要确定象限,可以使用atan2。
#7
3
I guess the main question tries to figure out: "when should I use one or the other", or "which should I use", or "Am I using the right one"?
我猜主要的问题是:“我什么时候应该用一个或另一个”,“我应该用哪个”,或者“我用对了那个”?
I guess the important point is atan only was intended to feed positive values in a right-upwards direction curve like for time-distance vectors. Cero is always at the bottom left, and thigs can only go up and right, just slower or faster. atan doesn't return negative numbers, so you can't trace things in the 4 directions on a screen just by adding/subtracting its result.
我想重点是atan的目的仅仅是在一个向右向上的方向曲线中提供正值就像时间-距离向量一样。Cero总是在左下角,而这些igs只能上下移动,要么慢一点,要么快一点。atan不会返回负数,所以你不能仅仅通过增加或减少结果来跟踪屏幕上的四个方向。
atan2 is intended for the origin to be in the middle, and things can go backwards or down. That's what you'd use in a screen representation, because it DOES matter what direction you want the curve to go. So atan2 can give you negative numbers, because its cero is in the center, and its result is something you can use to trace things in 4 directions.
atan2的目的是让原点在中间,物体可以向后或向下。这就是你在屏幕表示法中要用到的,因为它决定了曲线的走向。所以atan2可以给你负数,因为它的cero在中间,它的结果是你可以用来追踪四个方向的东西。
#8
2
Consider a right angled triangle. We label the hypotenuse r, the horizontal side y and the vertical side x. The angle of interest @ is the angle between x and r.
考虑一个直角三角形。我们把斜边r标出来,水平边y标出来,竖直边x标出来。兴趣角@是x和r之间的夹角。
c++ atan2(y, x) will give us the value of angle @ in radians. atan is used if we only know or are interested in y/x not y and x individually. So if p = y/x then to get @ we'd use atan(p).
c+ atan2(y, x)将给出弧度内的角@值。如果我们只知道或只对y/x感兴趣,则使用atan,而不单独使用y和x。如果p = y/x,我们就可以用atan(p)
You cannot use atan2 to determine the quadrant, you can use atan2 only if you already know which quadrant your in! In particular positive x and y imply the first quadrant, positive y and negative x, the second and so on. atan or atan2 themselves simply return a positive or a negative number, nothing more.
您不能使用atan2来确定象限,您可以使用atan2,除非您已经知道哪个象限属于您!特别是正的x和y表示第一象限,正的y和负的x,第二象限等等。atan或atan2本身只是返回一个正数或负数,仅此而已。
#9
0
Mehrwolf below is correct, but here is a heuristic which may help:
下面的Mehrwolf是对的,但是这里有一个启发式可以帮助我们:
If you are working in a 2-dimensional coordinate system, which is often the case for programming the inverse tangent, you should use definitely use atan2. It will give the full 2 pi range of angles and take care of zeros in the x coordinate for you.
如果你在一个二维坐标系中工作,这通常是编程的逆切线,你应该使用atan2。它会给出完整的2个角的范围并为你处理x坐标中的0。
Another way of saying this is that atan(y/x) is virtually always wrong. Only use atan if the argument cannot be thought of as y/x.
另一种说法是atan(y/x)实际上总是错的。只有当参数不能被认为是y/x时才使用atan。
#10
0
atan2(y,x)
is generally used if you want to convert cartesian coordinates to polar coordinates. It will give you the angle, while sqrt(x*x+y*y)
or, if available, hypot(y,x)
will give you the size.
如果要将笛卡尔坐标转换为极坐标,通常使用atan2(y,x)。它会给你角度,而sqrt(x*x+y*y)或者,如果有的话,假设(y,x)会给你大小。
atan(x)
is simply the inverse of tan. In the annoying case you have to use atan(y/x)
because your system doesn't provide atan2
, you would have to do additional checks for the signs of x
and y
, and for x=0
, in order to get the correct angle.
atan(x)就是tan的倒数。在恼人的情况下,你必须使用atan(y/x)因为你的系统不提供atan2,你必须做额外的检查x和y的符号,以及x=0的符号,以得到正确的角度。
Note: atan2(y,x)
is defined for all real values of y
and x
, except for the case when both arguments are zero.
注意:atan2(y,x)是为y和x的所有实际值定义的,除了两个参数为零的情况外。
#1
82
std::atan2
allows calculating the arctangent of all four quadrants. std::atan
only allows calculating from quadrants 1 and 4.
atan2允许计算所有四个象限的反正切。atan只允许从象限1和象限4进行计算。
#2
242
From school mathematics we know that the tangent has the definition
从学校数学中我们知道正切有定义
tan(α) = sin(α) / cos(α)
and we differentiate between four quadrants based on the angle that we supply to the functions. The sign of the sin
, cos
and tan
have the following relationship (where we neglect the exact multiples of π/2
):
我们根据供给函数的角度来区分四个象限。罪恶的符号,因为和褐色有以下关系(我们忽视的π/ 2的倍数):
Quadrant Angle sin cos tan
-------------------------------------------------
I 0 < α < π/2 + + +
II π/2 < α < π + - -
III π < α < 3π/2 - - +
IV 3π/2 < α < 2π - + -
Given that the value of tan(α)
is positive, we cannot distinguish, whether the angle was from the first or third quadrant and if it is negative, it could come from the second or fourth quadrant. So by convention, atan()
returns an angle from the first or fourth quadrant (i.e. -π/2 <= atan() <= π/2
), regardless of the original input to the tangent.
鉴于tan(α)是积极的价值,我们不能区分,从第一或第三象限角是否如果是负的,它可能来自第二或第四象限。所以按照惯例,每股()返回一个角度从第一或第四象限(即-π/ 2 < =每股()< =π/ 2),不管最初的切线的输入。
In order to get back the full information, we must not use the result of the division sin(α) / cos(α)
but we have to look at the values of the sine and cosine separately. And this is what atan2()
does. It takes both, the sin(α)
and cos(α)
and resolves all four quadrants by adding π
to the result of atan()
whenever the cosine is negative.
为了得到完整的信息,我们不能使用部门的结果sin(α)/ cos(α),但我们必须考虑单独的正弦和余弦值。这就是atan2()的作用。罪(α)和需要,因为(α)和解决所有四个象限加上π作用的结果:()当cos是负的。
Remark: The atan2(y, x)
function actually takes a y
and a x
argument, which is the projection of a vector with length v
and angle α
on the y- and x-axis, i.e.
备注:量化(y,x)函数实际上y、x的论点,这是一个向量的投影长度为v和角α在y轴,即。
y = v * sin(α)
x = v * cos(α)
which gives the relation
这使的关系
y/x = tan(α)
Conclusion: atan(y/x)
is held back some information and can only assume that the input came from quadrants I or IV. In contrast, atan2(y,x)
gets all the data and thus can resolve the correct angle.
结论:atan(y/x)保留了一些信息,只能假设输入来自象限I或IV。相反,atan2(y,x)获取所有数据,从而可以解决正确的角度。
#3
17
Another thing to mention is that atan2
is more stable when computing tangents using an expression like atan(y / x)
and x
is 0 or close to 0.
另外要注意的是,当使用atan(y / x)和x = 0或接近0的表达式计算切线时,atan2更加稳定。
#4
15
The actual values are in radians but to interpret them in degrees it will be:
实际值是用弧度表示的,但要用度数来表示:
-
atan
= gives angle value between -90 and 90 - atan =给出-90和90之间的夹角值
-
atan2
= gives angle value between -180 and 180 - atan2 =给出-180和180之间的角值
For my work which involves computation of various angles such as heading and bearing in navigation, atan2
in most cases does the job.
对于我的工作涉及到计算不同的角度,如导航中的航向和方位,atan2在大多数情况下都是这样做的。
#5
8
atan(x) Returns the principal value of the arc tangent of x, expressed in radians.
atan(x)返回以弧度表示的arctanx的主要值。
atan2(y,x) Returns the principal value of the arc tangent of y/x, expressed in radians.
atan2(y,x)返回arctany /x的主值,用弧度表示。
Notice that because of the sign ambiguity, a function cannot determine with certainty in which quadrant the angle falls only by its tangent value (atan alone). You can use atan2 if you need to determine the quadrant.
注意,由于符号不明确,函数不能确定在哪个象限中,角度仅由其切值(atan单独)决定。如果需要确定象限,可以使用atan2。
#6
3
With atan2 you can determine the quadrant as stated here.
用atan2你可以确定象限如这里所述。
You can use atan2 if you need to determine the quadrant.
如果需要确定象限,可以使用atan2。
#7
3
I guess the main question tries to figure out: "when should I use one or the other", or "which should I use", or "Am I using the right one"?
我猜主要的问题是:“我什么时候应该用一个或另一个”,“我应该用哪个”,或者“我用对了那个”?
I guess the important point is atan only was intended to feed positive values in a right-upwards direction curve like for time-distance vectors. Cero is always at the bottom left, and thigs can only go up and right, just slower or faster. atan doesn't return negative numbers, so you can't trace things in the 4 directions on a screen just by adding/subtracting its result.
我想重点是atan的目的仅仅是在一个向右向上的方向曲线中提供正值就像时间-距离向量一样。Cero总是在左下角,而这些igs只能上下移动,要么慢一点,要么快一点。atan不会返回负数,所以你不能仅仅通过增加或减少结果来跟踪屏幕上的四个方向。
atan2 is intended for the origin to be in the middle, and things can go backwards or down. That's what you'd use in a screen representation, because it DOES matter what direction you want the curve to go. So atan2 can give you negative numbers, because its cero is in the center, and its result is something you can use to trace things in 4 directions.
atan2的目的是让原点在中间,物体可以向后或向下。这就是你在屏幕表示法中要用到的,因为它决定了曲线的走向。所以atan2可以给你负数,因为它的cero在中间,它的结果是你可以用来追踪四个方向的东西。
#8
2
Consider a right angled triangle. We label the hypotenuse r, the horizontal side y and the vertical side x. The angle of interest @ is the angle between x and r.
考虑一个直角三角形。我们把斜边r标出来,水平边y标出来,竖直边x标出来。兴趣角@是x和r之间的夹角。
c++ atan2(y, x) will give us the value of angle @ in radians. atan is used if we only know or are interested in y/x not y and x individually. So if p = y/x then to get @ we'd use atan(p).
c+ atan2(y, x)将给出弧度内的角@值。如果我们只知道或只对y/x感兴趣,则使用atan,而不单独使用y和x。如果p = y/x,我们就可以用atan(p)
You cannot use atan2 to determine the quadrant, you can use atan2 only if you already know which quadrant your in! In particular positive x and y imply the first quadrant, positive y and negative x, the second and so on. atan or atan2 themselves simply return a positive or a negative number, nothing more.
您不能使用atan2来确定象限,您可以使用atan2,除非您已经知道哪个象限属于您!特别是正的x和y表示第一象限,正的y和负的x,第二象限等等。atan或atan2本身只是返回一个正数或负数,仅此而已。
#9
0
Mehrwolf below is correct, but here is a heuristic which may help:
下面的Mehrwolf是对的,但是这里有一个启发式可以帮助我们:
If you are working in a 2-dimensional coordinate system, which is often the case for programming the inverse tangent, you should use definitely use atan2. It will give the full 2 pi range of angles and take care of zeros in the x coordinate for you.
如果你在一个二维坐标系中工作,这通常是编程的逆切线,你应该使用atan2。它会给出完整的2个角的范围并为你处理x坐标中的0。
Another way of saying this is that atan(y/x) is virtually always wrong. Only use atan if the argument cannot be thought of as y/x.
另一种说法是atan(y/x)实际上总是错的。只有当参数不能被认为是y/x时才使用atan。
#10
0
atan2(y,x)
is generally used if you want to convert cartesian coordinates to polar coordinates. It will give you the angle, while sqrt(x*x+y*y)
or, if available, hypot(y,x)
will give you the size.
如果要将笛卡尔坐标转换为极坐标,通常使用atan2(y,x)。它会给你角度,而sqrt(x*x+y*y)或者,如果有的话,假设(y,x)会给你大小。
atan(x)
is simply the inverse of tan. In the annoying case you have to use atan(y/x)
because your system doesn't provide atan2
, you would have to do additional checks for the signs of x
and y
, and for x=0
, in order to get the correct angle.
atan(x)就是tan的倒数。在恼人的情况下,你必须使用atan(y/x)因为你的系统不提供atan2,你必须做额外的检查x和y的符号,以及x=0的符号,以得到正确的角度。
Note: atan2(y,x)
is defined for all real values of y
and x
, except for the case when both arguments are zero.
注意:atan2(y,x)是为y和x的所有实际值定义的,除了两个参数为零的情况外。