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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:53:53  来源:igfitidea点击:

How do I find the inverse tangent of a line?

javamathgeometry

提问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().

这篇文章2 点之间角度有一个使用 atan() 的例子。

回答by Josh Lee

Use the Math.atan2function. 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 的范围不会给出某些线的正确答案.

回答by ggg

The atan2function helps solve this problem while avoiding boundary conditions such as division by zero.

ATAN2功能有助于同时通过避免零边界条件,如分裂解决这个问题。

Math.atan2(y2-y1, x2-x1)