Angle between two Vectors 2D

前端 未结 8 1733
深忆病人
深忆病人 2021-02-05 17:29

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 = Mat         


        
8条回答
  •  情话喂你
    2021-02-05 18:01

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

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

    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.

    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

提交回复
热议问题