为什么 atan2 的参数是 Y,X 而不是 X,Y?
在 C 中,atan2 函数具有以下签名:
double atan2( double y, double x );
其他语言也这样做。 这是我所知道的唯一一个以 Y,X 顺序而不是 X,Y 顺序获取参数的函数,它经常把我搞砸,因为当我想到坐标时,我想到 (X,Y)。
有谁知道为什么atan2的参数顺序约定是这样的?
In C the atan2 function has the following signature:
double atan2( double y, double x );
Other languages do this as well. This is the only function I know of that takes its arguments in Y,X order rather than X,Y order, and it screws me up regularly because when I think coordinates, I think (X,Y).
Does anyone know why atan2's argument order convention is this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为它与arctan(y/x)有关,所以y先出现。
这是一个很好的链接,对此进行了一些讨论:角度和方向
Because it is related to arctan(y/x), so y appears first.
Here's a nice link talking about it a bit: Angles and Directions
我的假设一直是,这是因为三角定义,即
当使用来自原点的正则角度时,相对始终是 Y,相邻始终是 X,所以:
即,它是这样做的,因此不会出现顺序混乱关于数学定义。
My assumption has always been that this is because of the trig definition, ie that
When working with the canonical angle from the origin, opposite is always Y and adjacent is always X, so:
Ie, it was done that way so there's no ordering confusion with respect to the mathematical definition.
假设一个矩形三角形,其对边称为 y,邻边称为 x:
tan(angle) = y/x
arctan(tan(angle)) = arctan(y/x)
Suppose a rectangle triangle with its opposite side called y, adjacent side called x:
tan(angle) = y/x
arctan(tan(angle)) = arctan(y/x)
因为在学校里,计算梯度的助记符
是上升与运行,或者换句话说dy/dx,或者更简单地说是y/x。
这个顺序已经渗透到反正切函数的参数中。
所以它是一个历史文物。 对我来说这取决于我的想法
关于我何时使用
atan2
。 如果我考虑差异,我就猜对了如果我考虑的是坐标对,我就错了。
It's because in school, the mnemonic for calculating the gradient
is rise over run, or in other words dy/dx, or more briefly y/x.
And this order has snuck into the arguments of arctangent functions.
So it's a historical artefact. For me it depends on what I'm thinking
about when I use
atan2
. If I'm thinking about differentials, I get it rightand if I'm thinking about coordinate pairs, I get it wrong.
excel中的顺序是atan2(X,Y),所以我认为相反的顺序是一个编程问题。 通过在“n”和“(”之间放置“2”,并将“/”替换为“,”,只需 2 次操作,即可轻松将 atan(Y/X) 更改为 atan2(Y,X)。相反的顺序需要 4 次操作,并且某些操作会更复杂(剪切和粘贴),
我经常在 Excel 中计算出数学结果,然后将其移植到 .NET,所以有时会挂在 atan2 上,这将是最好的。如果atan2可以以一种或另一种方式标准化。
The order is atan2(X,Y) in excel so I think the reverse order is a programming thing. atan(Y/X) can easily be changed to atan2(Y,X) by putting a '2' between the 'n' and the '(', and replacing the '/' with a ',', only 2 operations. The opposite order would take 4 operations and some of the operations would be more complex (cut and paste).
I often work out my math in Excel then port it to .NET, so will get hung up on atan2 sometimes. It would be best if atan2 could be standardized one way or the other.
如果将 atan2 的参数颠倒过来会更方便。 这样您就无需担心在计算极角时翻转参数。 Mathematica 的等效项就是这样做的: https://reference.wolfram.com/language/ref /ArcTan.html
早在很久以前,FORTRAN 就有一个 ATAN2 函数,其参数顺序不太方便,在 本参考手册,被(有些不准确地)描述为 arctan(arg1 / arg2)。
最初的创建者可能专注于 atan2(arg1, arg2)(或多或少)arctan(arg1 / arg2),并且该决定被盲目地从 FORTRAN 复制到 C、C++、Python、Java 和 JavaScript。
It would be more convenient if atan2 had its arguments reversed. Then you wouldn't need to worry about flipping the arguments when computing polar angles. The Mathematica equivalent does just that: https://reference.wolfram.com/language/ref/ArcTan.html
Way back in the dawn of time, FORTRAN had an ATAN2 function with the less convenient argument order that, in this reference manual, is (somewhat inaccurately) described as arctan(arg1 / arg2).
It is plausible that the initial creator was fixated on atan2(arg1, arg2) being (more or less) arctan(arg1 / arg2), and that the decision was blindly copied from FORTRAN to C to C++ and Python and Java and JavaScript.