C++中的atan和atan2有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/283406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between atan and atan2 in C++?
提问by yesraaj
What is the difference between atan
and atan2
in C++?
在 C++ 中atan
和atan2
在 C++ 中有什么区别?
采纳答案by Chris Jester-Young
std::atan2
allows calculating the arctangent of all four quadrants. std::atan
only allows calculating from quadrants 1 and 4.
std::atan2
允许计算所有四个象限的反正切。std::atan
只允许从象限 1 和 4 计算。
回答by Mehrwolf
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
):
我们根据提供给函数的角度来区分四个象限。的符号sin
,cos
和tan
具有以下关系(我们忽略 的精确倍数π/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(α)
正,我们无法区分角度是来自第一象限还是第三象限,如果为负,则可能来自第二或第四象限。因此,按照惯例,atan()
从第一象限或第四象限(即-π/2 <= atan() <= π/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()
它的作用。当余弦为负时,它同时需要sin(α)
和cos(α)
并且通过添加π
到 的结果来解析所有四个象限atan()
。
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.
备注:该atan2(y, x)
函数实际上带有一个y
和一个x
参数,即一个有长度v
和角度的向量α
在y轴和x轴上的投影,即
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)
获取所有数据从而可以解析正确的角度。
回答by Laserallan
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.
另一件要提到的事情是,atan2
当使用像atan(y / x)
and x
is 0 或接近于 0的表达式计算切线时更稳定。
回答by Keugyeol
The actual values are in radians but to interpret them in degrees it will be:
实际值以弧度表示,但以度数来解释它们将是:
atan
= gives angle value between -90 and 90atan2
= gives angle value between -180 and 180
atan
= 给出 -90 和 90 之间的角度值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
在大多数情况下可以完成这项工作。
回答by RomanM
atan(x) Returns the principal value of the arc tangent of x, expressed in radians.
atan(x) 返回 x 的反正切的主值,以弧度表示。
atan2(y,x) Returns the principal value of the arc tangent of y/x, expressed in radians.
atan2(y,x) 返回 y/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。
回答by sergio
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 总是在左下角,而 thigs 只能向上和向右,只是更慢或更快。atan 不返回负数,因此您无法仅通过添加/减去其结果来在屏幕上的 4 个方向上跟踪事物。
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 在中心,它的结果是你可以用来在 4 个方向上追踪事物的东西。
回答by bheks
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)
将以弧度为单位给出角度 α 的值。
atan
如果我们只知道或对 y/x 而不是 y 和 x 感兴趣,则使用。所以如果 p = y/x 那么为了得到 α 我们会使用atan(p)
.
You cannot use atan2
to determine the quadrant, you can use atan2
only if you already knowwhich 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
他们自己只是返回一个正数或负数,仅此而已。
回答by Burkhard
回答by Nick Mulgan
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 pi 角度范围,并为您处理 x 坐标中的零点。
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。
回答by user3303328
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)
或者,如果有的话,hypot(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
,除情况下,当两个参数是零。