C# 获取点和原点之间的角度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17530169/
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
Get angle between point and origin
提问by Dave
This might have been answered before, sorry if it has. I basically need to get the angle from origin to point. So lets say Origin is (0, 0)and my target point is (3, 0).
之前可能已经回答过这个问题,如果有的话,抱歉。我基本上需要从原点到点的角度。所以假设Origin 是 (0, 0)而我的目标点是 (3, 0)。
3 O'clock = 90 degrees
3 点钟 = 90 度
6 O'clock = 180 degrees
6 点钟 = 180 度
9 O'clock = 270 degrees
9 点钟 = 270 度
12 O'clock = 0 degrees
12 点钟 = 0 度
Somehow, I gotta do some math magic, and find out that the angle is 90 degrees (Top is 0). The origin can vary, so I need a method with two parameters, Origin, and TargetPoint, which returns double Angle in Degrees.
不知何故,我得做一些数学魔术,发现角度是 90 度(顶部是 0)。 原点可能会有所不同,因此我需要一个具有两个参数的方法,Origin 和 TargetPoint,它返回以度为单位的双角度。
Yea, I realize this looks short and nonconstructive, but I made the question as simple and understandable as possible. All the other questions were closed -.-
是的,我意识到这看起来很短而且没有建设性,但我使问题尽可能简单易懂。所有其他问题都已关闭-.-
Thanks
谢谢
采纳答案by Sebastian Negraszus
The vector between two points A and B is B-A = (B.x-A.x, B.y-A.y). The angle between two vectors can be calculated with the dot productor atan2.
两点 A 和 B 之间的向量是 BA = (Bx-Ax, By-Ay)。可以使用点积或atan2计算两个向量之间的角度。
var vector2 = Target - Origin;
var vector1 = new Point(0, 1) // 12 o'clock == 0°, assuming that y goes from bottom to top
double angleInRadians = Math.Atan2(vector2.Y, vector2.X) - Math.Atan2(vector1.Y, vector1.X);
See also Finding Signed Angle Between Vectors
另请参阅查找向量之间的有符号角
回答by mirabilos
Assuming xis positive, something like this:
假设x是积极的,是这样的:
angle = Math.Atan(y / x) * 180 / Math.PI + 90
Edited to allow for negative xvalues:
编辑以允许负值x:
If it can be negative, just do it by case. Something like this:
如果它可以是负面的,就根据情况来做。像这样的东西:
if (x < 0) {
angle = 270 - (Math.Atan(y / -x) * 180 / Math.PI);
} else {
angle = 90 + (Math.Atan(y / x) * 180 / Math.PI);
}
回答by Ahmed KRAIEM
public static double GetAngleDegree(Point origin, Point target) {
var n = 270 - (Math.Atan2(origin.Y - target.Y, origin.X - target.X)) * 180 / Math.PI;
return n % 360;
}
static void Main(string[] args)
{
Console.WriteLine(GetAngleDegree(new Point(0, 0), new Point(0, 3)));//0
Console.WriteLine(GetAngleDegree(new Point(0, 0), new Point(3, 0)));//90
Console.WriteLine(GetAngleDegree(new Point(0, 0), new Point(0, -3)));//180
Console.WriteLine(GetAngleDegree(new Point(0, 0), new Point(-3, 0)));//270
}
回答by Elo
Actually there is no way to find out the angle between point and origin due to the fact that the origin is (0,0). We can calculate the angle between 2 points beacuse they are treated like vectors and as such they have a direction, but the origin has no direction. For that reason, if you want to find the angle using the example of a clock you can calculate the angle between the point and (1,0) for example being that point at 0 degrees.
实际上,由于原点是(0,0),因此无法找出点和原点之间的角度。我们可以计算两点之间的角度,因为它们被视为向量,因此它们有方向,但原点没有方向。出于这个原因,如果您想使用时钟示例找到角度,您可以计算该点与 (1,0) 之间的角度,例如该点位于 0 度。
I'm sorry I'm not versed in C# but you can have a look at this java code which is similar:
对不起,我不精通 C#,但你可以看看这个类似的 java 代码:
double getAngle2PointsRad(double p1_x, double p1_y, double p2_x, double p2_y) {
return Math.acos((((p1_x * p2_x) + (p1_y * p2_y)) / (Math.sqrt(Math.pow(p1_x, 2) + Math.pow(p1_y, 2)) * Math.sqrt(Math.pow(p2_x, 2) + Math.pow(p2_y, 2)))));
}
double getAngle2PointsDeg(double p1_x, double p1_y, double p2_x, double p2_y) {
return Math.acos((((p1_x * p2_x) + (p1_y * p2_y)) / (Math.sqrt(Math.pow(p1_x, 2) + Math.pow(p1_y, 2)) * Math.sqrt(Math.pow(p2_x, 2) + Math.pow(p2_y, 2))))) * 180 / Math.PI;
}
If you try to calculate using (0,0) you will get NaN beacuse it tries to divide by zero.
如果您尝试使用 (0,0) 进行计算,您将得到 NaN,因为它试图除以零。

