Java 我如何找到一条线的反正切?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3449826/
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
How do I find the inverse tangent of a line?
提问by Skizit
I've got a line (x1,y1) and (x2,y2). I'd like to use tan inverse to find the angle of that line, how would I do so in java?
我有一条线 (x1,y1) 和 (x2,y2)。我想使用 tan inverse 来找到那条线的角度,我将如何在 Java 中这样做?
I'd like to see what angle the line makes in relation to x1,y1
我想看看这条线与 x1,y1 的夹角
采纳答案by Gedrox
You need
你需要
Math.toDegrees(Math.atan((y2-y1)/(x2-x1)))
Math.toDegrees(Math.atan((y2-y1)/(x2-x1)))
Do notice exception when x1=x2.
当 x1=x2 时注意异常。
回答by stacker
This post Angle between 2 pointshas an example using atan().
回答by Josh Lee
Use the Math.atan2
function. It is like arctan but knows about x and y coordinates, so it can handles lines that are horizontal, vertical, or pointing in other directions -- arctan's range of -pi/2 through pi/2 will not give the correct answer for some lines.
使用该Math.atan2
功能。它类似于 arctan 但知道 x 和 y 坐标,因此它可以处理水平、垂直或指向其他方向的线——arctan 的 -pi/2 到 pi/2 的范围不会给出某些线的正确答案.