Reputation: 362
Currently i am converting a Desktop application to Windows 8 application. To get a angle between 2 points in desktop application they use Vector.AngleBetween(vector1, vector2). Using "Point" i got the vector values in WinRT. Like this,
var vectorX = point1.X - point2.X;
var vectorY = point1.Y - point2.Y;
Point vector = new Point(vectorX , vectorY);
But i don't find any way to get a angle between 2 points in WinRT. I got this function from online,
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);
}
but it wont give the exact result like "Vector.AngleBetween". Is there any better way available to get a result like "Vector.AngleBetween" in WinRT...?
Upvotes: 0
Views: 331
Reputation: 3379
I don't think your math is right. You can calculate angle between vectors using dot product and arcus cosinus, pseudo-code below:
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);
If I'm correct this should give you roughly right answer. Details and better explenations can be found on internet, e.g. Dot product
Upvotes: 1