两个向量 2D 之间的角度 [英] Angle between two Vectors 2D

查看:51
本文介绍了两个向量 2D 之间的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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));

推荐答案

你应该看看 atan2 的文档(此处).

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

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

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

您的代码当前所做的是找到参考 0,0 的矢量 b 的角度并减去矢量 a 的角度> 参考 0,0.

What your code currently does is find the angle of the vector b in reference to 0,0 and subtract the angle of the vector a in reference to 0,0.

你总是得到 0 的原因是因为 1,150,50 位于与 0,0 交叉的同一行上(两个调用都返回大约 0.785398),因此减去它们将导致 0

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

这篇关于两个向量 2D 之间的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆