get angle of 2 vector2 in csharp

To get the angle between two Vector2 in C#, you can use trigonometric functions provided by the Math class. Here's an example code snippet that calculates the angle between two Vector2:

main.cs
using System;
using System.Numerics;

public class Program
{
    static void Main()
    {
        Vector2 vector1 = new Vector2(3, 4);
        Vector2 vector2 = new Vector2(-1, 2);

        float dotProduct = Vector2.Dot(vector1, vector2);
        float angle = (float)Math.Acos(dotProduct / (vector1.Length() * vector2.Length()));

        Console.WriteLine($"Angle between vector1 and vector2: {angle} radians");
    }
}
423 chars
17 lines

In the code above, we first calculate the dot product of the two vectors using the Vector2.Dot method. Then, we apply the formula for calculating the angle between two vectors using the dot product and the lengths of the vectors. Finally, we use the Math.Acos method to get the angle in radians.

Note that the angle is returned in radians. If you want to convert it to degrees, you can use the Math.Rad2Deg constant:

main.cs
float angleInDegrees = angle * (180 / Math.PI);
Console.WriteLine($"Angle between vector1 and vector2: {angleInDegrees} degrees");
131 chars
3 lines

gistlibby LogSnag