## Using atan2 to find angle between two vectors

angle between two points

angle between two lines (vectors)

vector angle

sine of angle between two vectors

matlab angle between two vectors 2d

java angle between two vectors

angle between two vectors positive negative

I understand that:

`atan2(vector.y, vector.x)`

= the angle between the **vector and the X axis**.

But I wanted to know how to get the angle between **two vectors** using atan2. So I came across this solution:

atan2(vector1.y - vector2.y, vector1.x - vector2.x)

My question is very simple:

Will the two following formulas produce the same number?

`atan2(vector1.y - vector2.y, vector1.x - vector2.x)`

`atan2(vector2.y - vector1.y, vector2.x - vector1.x)`

If not: **How do I know what vector comes first in the subtractions?**

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

**Using atan2 to calculate angle between two vectors,** The Red is: atan2(vectorA.y - vectorB.y, vectorA.x - vectorB.x). The Green is: atan2(vectorB.y - vectorA.y, vectorB.x - vectorA.x). The Blue which I 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 π):

The proper way to do it is by find the sine of the angle using the cross product, and the cosine of the angle using the dot product and combine 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 test case above in GeoGebra.

**Using atan2 to find an angle between two points,** Using atan2 to find an angle between two points One possible issue I see might be the order of operations of your relative vector calculation. To find the angle between two vectors, we use a formula for cosine of the angle in terms of the dot product of the vectors and the magnitude of both vectors. The magnitude of each vector is given by the formula for the distance between points.

I think a better formula was posted here: http://www.mathworks.com/matlabcentral/answers/16243-angle-between-two-vectors-in-3d

angle = atan2(norm(cross(a,b)), dot(a,b))

So this formula works in 2 or 3 dimensions. For 2 dimensions this formula simplifies to the one stated above.

**Maths - angle between vectors - Martin Baker,** How do we calculate the angle between two vectors? vector is ahead, then we probably need to use the atan2 function (as explained on this page). using:. Scalar (dot) product of two vectors lets you get the cosinus of the angle between them. To get the 'direction' of the angle, you should also calculate the cross product, it will let you check (via z coordinate) is angle is clockwise or not (i.e. should you extract it from 360 degrees or not).

Nobody pointed out that if you have a single vector, and want to find the angle of the vector from the X axis, you can take advantage of the fact that the argument to atan2() is actually the slope of the line, or (delta Y / delta X). So if you know the slope, you can do the following:

given:

A = angle of the vector/line you wish to determine (from the X axis).

m = signed slope of the vector/line.

then:

A = atan2(m, 1)

Very useful!

**Angle Between Vectors,** it is often necessary to compute the angle between two vectors. For years, I did that using the familiar formula for angle in terms of the dot Incidentally, using atan2 also means that we need not worry about division by zero. I'd like to see a more convincing demonstration that this last formula is better. How to Find the Angle Between Two Vectors. In mathematics, a vector is any object that has a definable length, known as magnitude, and direction. Since vectors are not the same as standard lines or shapes, you'll need to use some special

If you care about accuracy for small angles, you want to use this:

angle = 2*atan2(|| ||b||a - ||a||b ||, || ||b||a + ||a||b ||)

Where "||" means absolute value, AKA "length of the vector". See https://math.stackexchange.com/questions/1143354/numerically-stable-method-for-angle-between-3d-vectors/1782769

However, that has the downside that in two dimensions, it loses the sign of the angle.

**atan2,** The function atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} {\displaystyle \operatorname {atan2} (y,x)} or arctan2 ( y , x ) {\displaystyle \operatorname {arctan2} (y,x)} {\displaystyle \operatorname {arctan2} (y,x)} (from "2-argument arctangent") is defined as the angle in the Euclidean plane In addition, an attempt to find the angle between the x axis In most math libraries acos will usually return a value between 0 and π which is 0° and 180°. If we want a + or - value to indicate which vector is ahead, then we probably need to use the atan2 function (as explained on this page). using: angle of 2 relative to 1= atan2(v2.y,v2.x) - atan2(v1.y,v1.x)

**How to calculate the angle between two vectors in 3D space using ,** You mean MATLAB's atan2 function , which gives the angle from to in the correct direction from origin to on the plane. The function gives an angle with the right The can still be expressed as the angle between two vectors. The first vector is p 2-p 1 and the other is a vector in the horizontal direction, (0, 1). Feed those two into AngleBetweenVectors and you have your answer. If you want to measure angle to vertical, then you can use the same idea.

**math Using atan2 to find angle between two vectors?,** C# public struct Vector2 { public double X, Y; /// <summary> /// Returns the angle between two vectos /// </summary> public static double GetAngle(Vector2 A, The main difference between these two methods is the fact that we get a scalar value as a result through the first method, while the result obtained by using the second technique is also a vector in nature. For the sake of only knowing how to find the angle between two vectors, we will look at only the scalar product for now.

**JavaScript: Find the angle between two points · GitHub,** You mean angle between two vectors. Find the angle between three points : I used Math.atan2(v.e(2), v.e(1)) to get the angle of a Sylvester js unit vector / direction was using for a design tool here, if you need the angle adjusted by all

##### Comments

- @andand no,
`atan2`

can be used for 3D vectors :`double angle = atan2(norm(cross_product), dot_product);`

and it's even more precise then`acos`

version. - This doesn't take into account angles greater than 180; I'm looking for something that can return a result 0 - 360, not limited to 0 - 180.
- Thanks for the help. So what you suggest, is getting the angle between vector2 and the x axis (let's call it angle1), getting the angle between vector1 and the x axis (lets call it angle2), and then subtracting the two. Correct? If so: What would be the difference between angle1 and angle2? Would angle1 be
`x`

and angle2 be`-x`

? More importantly, how do I choose which angle comes first in the subtraction? - @user3150201: It depends on what you want, because there are two angles between the vectors. - The above method gives an angle a such that if you turn vector1 counter-clockwise by this angle, then the result is vector2.
- @user3150201: Or do you want the
*smaller*of the two possible angles between the vectors, i.e. a result in the range 0 .. Pi ? - One may want to normalize the range to
`-PI..PI`

, to which this implementation doesn't comply as is. - @PeterHayman: For a normalization to (-π, π] I would do
`if (angle > M_PI) { angle -= 2 * M_PI } else if (angle <= -M_PI) { angle += 2 * M_PI }`

- That's a very non-standard definition of cross product. The more traditional definition of the cross product is given at en.wikipedia.org/wiki/Cross_product.
- @andand: See (8), (9) in mathworld.wolfram.com/CrossProduct.html. As far as I know, it
*is*often called cross-product of two vectors in the plane. - Here's a javascript version: bl.ocks.org/shancarter/1034db3e675f2d3814e6006cf31dbfdc
- Thanks for mentioning GeoGebra, an awesome tool!
- @ChronoTrigger - I make a distinction between clockwise and counterclockwise rotations based on the order of the operands.
- That is what @ja72 suggested in his/her answer stackoverflow.com/a/21486462/1187415.