C# 两个向量 2D 之间的角度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13458992/
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-10 08:38:45  来源:igfitidea点击:

Angle between two Vectors 2D

c#vectorxna

提问by Sorashi

I'm trying to compute the angle between two vectors. I tried this, but it always returns zero:

我正在尝试计算两个向量之间的角度。我试过这个,但它总是返回零:

public double GetAngle(Vector2 a, Vector2 b)
{
double angle = Math.Atan2(b.Y, b.X) - Math.Atan2(a.Y, a.X);
return angle;
}

GetAngle(new Vector2(1,1), new Vector2(50,50));

VectorsThe angle I need

向量我需要的角度

采纳答案by emartel

You should take a look at the documentation of atan2(here).

您应该查看atan2(这里)的文档。

What you're looking of is finding the difference between B (your upper left vector) and A (your bottom right vector), then pass this as a parameter to atan2

您正在寻找的是找到 B(您的左上角向量)和 A(您的右下角向量)之间的差异,然后将其作为参数传递给 atan2

return Math.Atan2(b.Y - a.Y,b.X - a.X);

What your code currently does is find the angle of the vector bin reference to 0,0and subtract the angle of the vector ain reference to 0,0.

你的代码目前所做的是找到矢量的角度b参考0,0,并减去矢量的角度a参考0,0

The reason you always get 0 is because 1,1and 50,50are on the same line that crosses 0,0(both calls return something approx. 0.785398), so subtracting them will result in 0

你总是得到 0 的原因是因为1,1并且50,50在交叉的同一条线上0,0(两个调用都返回大约0.785398),所以减去它们将导致0

回答by Shmiddty

You have to use the difference in x and y inside of the Atan2 method:

您必须在 Atan2 方法中使用 x 和 y 的差异:

Math.Atan2(b.Y - a.Y,b.X - a.X);

Math.Atan2(b.Y - a.Y,b.X - a.X);

Also, I believe this will give you the angle from 0to the hypotenuse of the triangle you've provided (not entirely sure).

另外,我相信这会给你0提供的三角形斜边的角度(不完全确定)。

I'd suggest trying Math.PI - angle.

我建议尝试Math.PI - angle

回答by doctor killer

tan(angle) = opposite/adjascent

tan(角度)=相反/相邻

arctan(opposite/adjascent) = angle

arctan(相反/相邻)= 角度

opposite = a.y - b.y

相反 = 是 - 通过

adjascent = b.x - a.x

相邻 = bx - ax

Math.Atan((a.Y - b.Y) / (b.X - a.X));

回答by urlreader

since you use vector2 class, I guess you can use

由于您使用 vector2 类,我想您可以使用

a-b

AB

to get the vector from a to b.

得到从 a 到 b 的向量。

so the angle you need is: Pi - angle(a-b).

所以你需要的角度是:Pi - 角度(ab)。

回答by Alexei

A simple solution should be this:

一个简单的解决方案应该是这样的:

Vector2 a_normalized = normalize(a);
Vector2 b_normalized = normalize(b);
double angle = arccos(dot(a_normalized,b_normalized));

http://simple.wikipedia.org/wiki/Dot_product

http://simple.wikipedia.org/wiki/Dot_product

This is Pseudo-code, because C# is not my world. Sorry

这是伪代码,因为 C# 不是我的世界。对不起

回答by RunHolt

if you are looking for the "angle between vectors a and b", you want the delta of the angle for vector a and the angle for vector b:

如果您正在寻找“向量 a 和 b 之间的角度”,您需要向量 a 的角度和向量 b 的角度的增量:

Math.Atan2(b.Y, b.X) - Math.Atan2(a.Y, a.X)

But the diagram doesn't match "angle between vectors". The answer for the diagram is indeed the previous answer given:

但该图与“向量之间的角度”不匹配。图的答案确实是之前给出的答案:

Math.Atan2(b.Y - a.Y, b.X - a.X)

回答by lisency

I think code show as below copy from .NET source code could help you.

我认为从 .NET 源代码中显示的代码如下所示可以帮助您。

reference: http://referencesource.microsoft.com/#WindowsBase/Base/System/Windows/Vector.cs,102

参考:http: //referencesource.microsoft.com/#WindowsBase/Base/System/Windows/Vector.cs,102

/// <summary>
/// AngleBetween - the angle between 2 vectors
/// </summary>
/// <returns>
/// Returns the the angle in degrees between vector1 and vector2
/// </returns>
/// <param name="vector1"> The first Vector </param>
/// <param name="vector2"> The second Vector </param>
public static double AngleBetween(Vector vector1, Vector vector2)
{
    double sin = vector1._x * vector2._y - vector2._x * vector1._y;  
    double cos = vector1._x * vector2._x + vector1._y * vector2._y;

    return Math.Atan2(sin, cos) * (180 / Math.PI);
}

回答by Darren

I'm a bit late to the party, but how about the static method on the Vector class:

我参加聚会有点晚了,但是 Vector 类上的静态方法如何:

Vector.AngleBetween(vector1, vector2)