Ma · Mathematics

How Do You Find the Signed Angle Between Two Vectors?

Published By the 9to5Science editorial team

At a glance

  1. 1. Check the setting

    A signed angle is well-defined in an oriented plane; in 3D, choose a plane normal or rotation axis.

  2. 2. Compute dot and determinant

    The dot product gives the cosine-related component; the 2D determinant gives the direction of rotation.

  3. 3. Use atan2

    Apply atan2 to the determinant and dot product, in that order, to get the directed angle.

  4. 4. Check edge cases

    A zero vector has no angle, and exactly opposite vectors have an ambiguous sign at 180 degrees.

The ordinary angle between two vectors is unsigned: it measures how far apart their directions are, from 0° to 180°. A signed angle also records which way you rotate from the first vector to the second. In a 2D coordinate system, counterclockwise rotation is conventionally positive and clockwise rotation negative.

For nonzero vectors u=(ux,uy)u=(u_x,u_y) and v=(vx,vy)v=(v_x,v_y), calculate the signed angle from uu to vv with

θ=atan2⁡(uxvy−uyvx,  uxvx+uyvy).\theta=\operatorname{atan2}\left(u_xv_y-u_yv_x,\;u_xv_x+u_yv_y\right).

The result is in radians unless you convert it to degrees. The order matters: this formula measures the rotation from uu to vv.

Why the formula works

The dot product and the 2D determinant encode the cosine and sine components of the angle:

u\cdot v=\|u\|\|v\|\cos\theta,$$

u_xv_y-u_yv_x=|u||v|\sin\theta.$$

The function atan2⁡(y,x)\operatorname{atan2}(y,x) returns the angle whose sine-related input is yy and cosine-related input is xx, while keeping track of the correct quadrant. Here, the determinant is the first input and the dot product is the second.

This is usually preferable to finding arccos⁡((u⋅v)/(∥u∥∥v∥))\arccos((u\cdot v)/(\|u\|\|v\|)) and guessing the sign afterward. The arccosine formula gives only an unsigned angle and can lose precision when its input is close to 1 or −1.

Worked example

Find the signed angle from u=(1,2)u=(1,2) to v=(−2,1)v=(-2,1).

First compute the determinant:

u_xv_y-u_yv_x=(1)(1)-(2)(-2)=5.$$ Then compute the dot product:

u\cdot v=(1)(-2)+(2)(1)=0.$$

Therefore,

θ=atan2⁡(5,0)=π2=90∘.\theta=\operatorname{atan2}(5,0)=\frac{\pi}{2}=90^\circ.

The positive sign means the rotation from uu to vv is counterclockwise. Reversing the vectors changes the determinant’s sign, so the angle from vv to uu is −90∘-90^\circ.

As a quick clockwise example, let u=(1,0)u=(1,0) and v=(0,−1)v=(0,-1). The determinant is −1-1 and the dot product is 00, so atan2⁡(−1,0)=−90∘\operatorname{atan2}(-1,0)=-90^\circ.

Converting units and using software

Most programming languages provide an atan2 function with inputs ordered as atan2(y, x). Pass the determinant first and the dot product second. For example, in Python:

import math

u = (1, 2)
v = (-2, 1)
det = u[0] * v[1] - u[1] * v[0]
dot = u[0] * v[0] + u[1] * v[1]
angle_radians = math.atan2(det, dot)
angle_degrees = math.degrees(angle_radians)

This gives angle_degrees equal to 90. If you need an angle in the range 0° to 360° instead of a signed angle, add 360° to a negative result. That changes the representation, not the rotation.

What changes in 3D?

Two vectors in 3D still have a well-defined unsigned angle, but there is no unique positive rotation direction without additional information. The vectors lie in a plane through the origin, but that plane can be assigned either orientation.

If you choose a unit normal nn to the plane, the signed angle from uu to vv relative to that normal is

θ=atan2⁡(n⋅(u×v),  u⋅v).\theta=\operatorname{atan2}\left(n\cdot(u\times v),\;u\cdot v\right).

Changing nn to −n-n reverses the sign. The displayed cross-product formula is specific to 3D. For vectors in a higher-dimensional space, choose an oriented 2D plane containing them and use their coordinates in an oriented orthonormal basis of that plane in the 2D determinant-and-dot-product formula.

Common mistakes

  • Swapping the inputs to atan2: Use the determinant as the first input and the dot product as the second. Reversing them generally gives a different angle.
  • Using the determinant in the wrong order: uxvy−uyvxu_xv_y-u_yv_x measures from uu to vv. Reversing the vectors flips the sign.
  • Treating the sign as absolute in 3D: A signed 3D angle requires a chosen normal or axis; the vectors alone do not determine one.
  • Using a zero vector: The direction of the zero vector is undefined, so no angle involving it is defined.
  • Expecting a unique sign for opposite vectors: If vectors point in exactly opposite directions, the determinant is zero and the unsigned angle is 180°. Either direction around the plane reaches the same endpoint, so the signed choice depends on convention or numerical details. Near this case, small rounding errors can affect the reported sign.

References

  1. Gilbert Strang, Introduction to Linear Algebra

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.