计算两点之间的角度 - java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26076656/
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
Calculating angle between two points - java
提问by David Lasry
I need to calculate the angle in degrees between two points, with a fixed point that is connected with the given two points by a line.
我需要计算两点之间的角度(以度为单位),其中一个固定点通过一条线与给定的两点相连。
Here is an image that illustrates what I need:
这是一张说明我需要的图像:
Here is what I have tried so far:
这是我迄今为止尝试过的:
public static float GetAngleOfLineBetweenTwoPoints(float x1, float x2, float y1, float y2) {
float xDiff = x2 - x1;
float yDiff = y2 - y1;
return (float) (Math.atan2(yDiff, xDiff) * (180 / Math.PI));
}
It's pointless to say that it doesn't provide the correct answer.
说它没有提供正确答案是毫无意义的。
回答by Voicu
You can have the following method that calculates the angle in radians using the Math.atan2
method:
您可以使用以下方法使用以下方法计算弧度角Math.atan2
:
public static double angleBetweenTwoPointsWithFixedPoint(double point1X, double point1Y,
double point2X, double point2Y,
double fixedX, double fixedY) {
double angle1 = Math.atan2(point1Y - fixedY, point1X - fixedX);
double angle2 = Math.atan2(point2Y - fixedY, point2X - fixedX);
return angle1 - angle2;
}
And call it with three points (using Math.toDregrees
to transform resulting angle from radians to degrees):
并用三个点调用它(Math.toDregrees
用于将产生的角度从弧度转换为度数):
System.out.println(Math.toDegrees(
angleBetweenTwoPointsWithFixedPoint(0, 0, // point 1's x and y
1, 1, // point 2
1, 0 // fixed point
)));
Output: 90.0
输出:90.0
Feel free to use Java's standard Point
or Line2D
classes in your solution though. This was just to demonstrate it works.
不过,您可以随意在您的解决方案中使用 Java 的标准Point
或Line2D
类。这只是为了证明它有效。
回答by user2288580
Here is a code snippet from my Android Gesture library. It works and is fully tested.
这是我的 Android Gesture 库中的代码片段。它有效并经过全面测试。
public double getAngleFromPoint(Point firstPoint, Point secondPoint) {
if((secondPoint.x > firstPoint.x)) {//above 0 to 180 degrees
return (Math.atan2((secondPoint.x - firstPoint.x), (firstPoint.y - secondPoint.y)) * 180 / Math.PI);
}
else if((secondPoint.x < firstPoint.x)) {//above 180 degrees to 360/0
return 360 - (Math.atan2((firstPoint.x - secondPoint.x), (firstPoint.y - secondPoint.y)) * 180 / Math.PI);
}//End if((secondPoint.x > firstPoint.x) && (secondPoint.y <= firstPoint.y))
return Math.atan2(0 ,0);
}//End public float getAngleFromPoint(Point firstPoint, Point secondPoint)
回答by Srikant Jakilinki
I don't know @user2288580 but even for simple, test-cases your code is failing.
我不知道@user2288580,但即使对于简单的测试用例,您的代码也失败了。
firstPoint = (0,0) secondPoint = (0, 5), (5,5), (5,0), (5, -5) (0, -5) (-5, -5), (-5, 0)
firstPoint = (0,0) secondPoint = (0, 5), (5,5), (5,0), (5, -5) (0, -5) (-5, -5), (-5 , 0)
Please see if this works for you @David -
请看看这是否适合你@David -
public double angleBetween2CartesianPoints(double firstX, double firstY, double secondX, double secondY) {
double angle = Math.atan2((secondX - firstX), (secondY - firstY)) * 180 / Math.PI;
if (angle < 0) {
return (360 + angle);
} else {
return (angle);
}
}