Java:切线方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3326668/
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
Java: Tangent method
提问by sixtyfootersdude
What is the opposite of the Math.tan(double x)function of java?
Math.tan(double x)java函数的反义词是什么?
I know that Tan(X) = oppositeSideLength/AdjacentSideLength
我知道 Tan(X) = oppositeSideLength/AdjacentSideLength
but I have the opposite and adjacent sides so I want to do the opposite operation.
但我有相反的和相邻的边,所以我想做相反的操作。
ie: x = Tan^-1(oppositeSideLenght/AdjacentSideLength)(that is how I would enter it in a calculator.
即:(x = Tan^-1(oppositeSideLenght/AdjacentSideLength)这就是我在计算器中输入它的方式。
I just looked in the Math classand I know that there is:
我刚刚看了数学课,我知道有:
Math.atan(Math.atan2
Math.atan(Math.atan2
but I don't think that either of these is what I am looking for.
但我认为这两个都不是我要找的。
回答by
Yes, you do indeed want atan, or sometimes, atan2. The difference between the two is that atan will fail under some circumstances when one of the side lengths are zero. While that may be unlikely for triangles, it is a possibility for some other, more general uses of atan. In addition, the atan function gives you an angle limited to the interval [-pi/2,pi/2]. So if you think about the atan function as a function of two inputs, (x,y), atan(y/x) will yield the same result as atan((-y)/(-x)). This is a serious flaw in some circumstances.
是的,您确实需要 atan,或者有时需要 atan2。两者的区别在于,当其中一个边长为零时,atan 在某些情况下会失败。虽然这对于三角形来说不太可能,但对于 atan 的其他一些更普遍的用途来说是可能的。此外,atan 函数为您提供了一个限制在区间 [-pi/2,pi/2] 内的角度。因此,如果您将 atan 函数视为两个输入 (x,y) 的函数,则 atan(y/x) 将产生与 atan((-y)/(-x)) 相同的结果。在某些情况下,这是一个严重的缺陷。
To solve these problems, the atan2 is defined such that it yields the correct result for all values of x and y, in any quadrant. One would use it as
为了解决这些问题,定义了 atan2,以便它在任何象限中为 x 和 y 的所有值产生正确的结果。一个人会用它作为
atan2(oppositesidelength,adjacentsidelength)
to yield a consistent result.
以产生一致的结果。
Of course, for use in a non-degenerate triangle, the simple call to atan(opposite/adjacent) should be entirely adequate for your purposes.
当然,对于在非退化三角形中使用,对 atan(opposite/adjacent) 的简单调用应该完全满足您的目的。
回答by John Kugelman
Yep, you want atan, the arc tangent. This is the inverse of tangent; same thing with sin and arc sine, cosine and arc cosine, etc. These are alternative mathematical terminology for these functions' inverses.
是的,你想要atan,反正切。这是切线的倒数;正弦和反正弦、余弦和反余弦等也是如此。这些是这些函数的反函数的替代数学术语。
Notice how atanreturns angles from -π/2 to π/2, by the way. That's a hint that it's an inverse function (tangent takes angles and spits out ratios, arc tangent takes ratios and spits out angles). It is also important to recognize the restricted range. You won't necessarily get back your original angle, since tangents repeat every π radians (every 180°) — tan(π) = 0, but atan(0) = 0, not π.
atan顺便注意一下如何将角度从 -π/2 返回到 π/2。这暗示它是一个反函数(切线取角度并吐出比率,反正切取比率并吐出角度)。识别受限范围也很重要。您不一定会恢复原始角度,因为切线每隔 π 弧度(每 180°)重复一次 — tan(π) = 0,但 atan(0) = 0,而不是 π。
回答by Andrei Fierbinteanu
Math.atan is indeed the opposite of tangent. It's called arctangent.
Math.atan 确实是切线的反面。它被称为反正切。
Basically x = arctan(tan(x)). Well, it's a tad more complex than that seeing that tangent is a repetitive function (it needs some adjustments by adding k*pi). You should check out the wikipedia article about the details.
基本上 x = arctan(tan(x))。好吧,这比看到切线是一个重复函数(它需要通过添加 k*pi 进行一些调整)要复杂一些。您应该查看有关详细信息的维基百科文章。
Anyway, you can indeed compute x (the angle) by doing Math.atan(opposite/adjacent). Take note though that the angle will be in radians, so make sure you convert to other units if that's not what your using.
无论如何,您确实可以通过执行Math.atan(opposite/adjacent). 请注意,角度将以弧度为单位,因此如果您不使用该单位,请确保将其转换为其他单位。
回答by Chris Taylor
Math.atan and Math.atan2 should work just fine.
Math.atan 和 Math.atan2 应该可以正常工作。
angle = Math.atan(Math.Tan(radians));
angle = Math.atan(oppositeSideLength/adjacentSideLength)
angle = Math.atan2(oppositeSideLength, adjacentSideLength)

