Search code examples
c#mathvectorangle

Calculating angle between two vectors


I want to calculate the angle between two System.Numerics.Vector3 but I can't find any function for this. Google only finds results for 2d points. I want to implement this in c#.


Solution

  • var v = new Vector3(1, 2, 3);
    var u = new Vector3(4, 5, 6);
    var angleInRadians = Math.Acos(Vector3.Dot(v, u) / (v.Length() * u.Length()));
    

    Keep in mind that ACos returns radians, so you might need to convert it to degrees. The resulting value should be between 0 and 180 degrees. You should also ensure that the vectors have a non zero length.

    Source:

    https://math.stackexchange.com/questions/974178/how-to-calculate-the-angle-between-2-vectors-in-3d-space-given-a-preset-function