C# WinRT中的Vector.AngleBetween
目前我正在将桌面应用程序转换为Windows 8应用程序。为了在桌面应用程序中获得两点之间的角度,他们使用Vector.AngleBetween(vector1,vector2)。使用“点”我得到了WinRT中的向量值。像这样,C# WinRT中的Vector.AngleBetween,c#,winrt-xaml,desktop-application,point,angle,C#,Winrt Xaml,Desktop Application,Point,Angle,目前我正在将桌面应用程序转换为Windows 8应用程序。为了在桌面应用程序中获得两点之间的角度,他们使用Vector.AngleBetween(vector1,vector2)。使用“点”我得到了WinRT中的向量值。像这样, var vectorX = point1.X - point2.X; var vectorY = point1.Y - point2.Y; Point vector = new Point(vectorX , vectorY); 但我找不到任何方法来获得WinRT中
var vectorX = point1.X - point2.X;
var vectorY = point1.Y - point2.Y;
Point vector = new Point(vectorX , vectorY);
但我找不到任何方法来获得WinRT中两点之间的角度。我从网上得到这个功能
public double GetAngleOfLineBetweenTwoPoints(Point p1, Point p2)
{
var xDiff = p2.X - p1.X;
var yDiff = p2.Y - p1.Y;
return Math.Atan2(yDiff , xDiff) * (180 / Math.PI);
}
但它不会给出像“Vector.AngleBetween”这样的精确结果。有没有更好的方法可以得到WinRT中的“Vector.AngleBetween”这样的结果?我认为你的数学不正确。您可以使用点积和弧余弦计算向量之间的角度,伪代码如下:
double vectorALength = sqrt(vectorA.x * vectorA.x + vectorA.y * vectorA.y);
double vectorBLength = sqrt(vectorB.x * vectorB.x + vectorB.y * vectorB.y);
double dotProduct = vectorA.x * vectorB.x + vectorA.y + vectorB.y
double cosAngle = dotProduct / (vectorALength * vectorBLength);
double angle = Math.Acos(cosAngle) * (180 / Math.PI);
如果我是对的,这应该给你大致正确的答案。详情和更好的解释可以在互联网上找到,例如