If you come from Stack Overflow, using atan2 might be a simpler solution for you.
Let ,
. If
is the "oriented" angle from
to
(that is, rotating
by
gives
), then:
In Matlab, this is equivalent to wrapToPi(angle(x2+i*y2) - angle(x1+i*y1)).
How to calculate the angle between two 3D vectors?
linear algebra - Calculate the angle between two vectors - Mathematics Stack Exchange
How do you prove the angle between two vectors formula?
math - Using atan2 to find angle between two vectors - Stack Overflow
How do I calculate the angle between two vectors in 2D?
To calculate the angle between two vectors in a 2D space:
- Find the dot product of the vectors.
- Divide the dot product by the magnitude of the first vector.
- Divide the resultant by the magnitude of the second vector.
Mathematically, angle α between two vectors [xa, ya] and [xb, yb] can be written as:
α = arccos[(xa xb + ya yb) / (√(xa² + ya²) × √(xb² + yb²))].
How do I calculate the angle between two vectors in 3D?
To calculate the angle between two vectors in a 3D space:
- Find the dot product of the vectors.
- Divide the dot product by the magnitude of the first vector.
- Divide the resultant by the magnitude of the second vector.
Mathematically, angle α between two vectors [xa, ya, za] and [xb, yb, zb] can be written as:
α = arccos[(xa xb + ya yb + za zb) / (√(xa² + ya² + za²) × √(xb² + yb² + zb²) )].
How to define the angle formed by two vectors?
The angle formed between two vectors is defined using the inverse cosine of the dot products of the two vectors and the product of their magnitudes.
Videos
If you come from Stack Overflow, using atan2 might be a simpler solution for you.
Let ,
. If
is the "oriented" angle from
to
(that is, rotating
by
gives
), then:
In Matlab, this is equivalent to wrapToPi(angle(x2+i*y2) - angle(x1+i*y1)).
I assume and
are both nonzero.
Let
modulo
be the oriented angle between
and
.
Using
$$
\cos\theta=\frac{(u,v)}{\|u\|\|v\|}
$$
you can find the value of .
Taking of the latter will, you get
in
such that
Now to determine the orientation of
, you must compute the
determinant of the matrix whose first column is
, and second column is
.
If this is , this means
and
are parallel. Write
. If
, then
mod
. If
, then
mod
.
If the determinant is positive, this means modulo
.
If the determinant is negative, you have modulo
.
I like to know how the formulas I use are derived.
The cosine of the angle of two vectors is equal to the ratio between the dot product of those vectors and the product between their lengths.
How do you prove it? (Geometrically, formally, etc. all the ways in which I can get a better intuition for it)
atan2(vector1.y - vector2.y, vector1.x - vector2.x)
is the angle between the difference vector (connecting vector2 and vector1) and the x-axis, which is problably not what you meant.
The (directed) angle from vector1 to vector2 can be computed as
angle = atan2(vector2.y, vector2.x) - atan2(vector1.y, vector1.x);
and you may want to normalize it to the range [0, 2 π):
if (angle < 0) { angle += 2 * M_PI; }
or to the range (-π, π]:
if (angle > M_PI) { angle -= 2 * M_PI; }
else if (angle <= -M_PI) { angle += 2 * M_PI; }
A robust way to do it is by finding the sine of the angle using the cross product, and the cosine of the angle using the dot product and combining the two with the Atan2() function.
In C# this is:
public struct Vector2
{
public double X, Y;
/// <summary>
/// Returns the angle between two vectos
/// </summary>
public static double GetAngle(Vector2 A, Vector2 B)
{
// |A·B| = |A| |B| COS(θ)
// |A×B| = |A| |B| SIN(θ)
return Math.Atan2(Cross(A,B), Dot(A,B));
}
public double Magnitude { get { return Math.Sqrt(Dot(this,this)); } }
public static double Dot(Vector2 A, Vector2 B)
{
return A.X*B.X+A.Y*B.Y;
}
public static double Cross(Vector2 A, Vector2 B)
{
return A.X*B.Y-A.Y*B.X;
}
}
class Program
{
static void Main(string[] args)
{
Vector2 A=new Vector2() { X=5.45, Y=1.12};
Vector2 B=new Vector2() { X=-3.86, Y=4.32 };
double angle=Vector2.GetAngle(A, B) * 180/Math.PI;
// angle = 120.16850967865749
}
}
See the test case above in GeoGebra.
