'Angle between two Vectors 2D
Solution 1:[1]
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
Solution 2:[2]
I think code show as below copy from .NET source code could help you.
reference: http://referencesource.microsoft.com/#WindowsBase/Base/System/Windows/Vector.cs,102
/// <summary>
/// AngleBetween - the angle between 2 vectors
/// </summary>
/// <returns>
/// Returns the the angle in degrees between vector1 and vector2
/// </returns>
/// <param name="vector1"> The first Vector </param>
/// <param name="vector2"> The second Vector </param>
public static double AngleBetween(Vector vector1, Vector vector2)
{
double sin = vector1._x * vector2._y - vector2._x * vector1._y;
double cos = vector1._x * vector2._x + vector1._y * vector2._y;
return Math.Atan2(sin, cos) * (180 / Math.PI);
}
Solution 3:[3]
A simple solution should be this:
Vector2 a_normalized = normalize(a);
Vector2 b_normalized = normalize(b);
double angle = arccos(dot(a_normalized,b_normalized));
http://simple.wikipedia.org/wiki/Dot_product
This is Pseudo-code, because C# is not my world. Sorry
Solution 4:[4]
if you are looking for the "angle between vectors a and b", you want the delta of the angle for vector a and the angle for vector b:
Math.Atan2(b.Y, b.X) - Math.Atan2(a.Y, a.X)
But the diagram doesn't match "angle between vectors". The answer for the diagram is indeed the previous answer given:
Math.Atan2(b.Y - a.Y, b.X - a.X)
Solution 5:[5]
You have to use the difference in x and y inside of the Atan2 method:
Math.Atan2(b.Y - a.Y,b.X - a.X);
Also, I believe this will give you the angle from 0 to the hypotenuse of the triangle you've provided (not entirely sure).
I'd suggest trying Math.PI - angle.
Solution 6:[6]
I'm a bit late to the party, but how about the static method on the Vector class:
Vector.AngleBetween(vector1, vector2)
Solution 7:[7]
tan(angle) = opposite/adjascent
arctan(opposite/adjascent) = angle
opposite = a.y - b.y
adjascent = b.x - a.x
Math.Atan((a.Y - b.Y) / (b.X - a.X));
Solution 8:[8]
since you use vector2 class, I guess you can use
a-b
to get the vector from a to b.
so the angle you need is: Pi - angle(a-b).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Neuron - Freedom for Ukraine |
| Solution 2 | lisency |
| Solution 3 | Alexei |
| Solution 4 | |
| Solution 5 | Shmiddty |
| Solution 6 | Darren |
| Solution 7 | doctor killer |
| Solution 8 | urlreader |


