// SPDX-License-Identifier: CC-BY-SA-4.0 // Copyright © Paweł Redman #import "/common.typ": post, link-self, static, img #show: post.with( title: "Angle between two vectors", date: datetime(year: 2020, month: 4, day: 17), updated: datetime(year: 2026, month: 6, day: 14), ) #figure( img(static("math/angle_between_vectors.png")), caption: [Which one to choose?], ) In $RR^n$ there are two angles between any two vectors. The smallest one I'll call $theta$. The other one, its 360° complement, I'll call $phi$. The smallest angle between two vectors $bold(u)$ and $bold(v)$ is given by: $ theta = cos^(-1)((bold(u) dot bold(v)) / (u v)) $ which follows easily from the geometric definition of the dot product: $ bold(u) dot bold(v) = u v cos theta $ The other angle is simply the 360° complement: $ phi = 2 pi - theta $ = Oriented angle #figure( img(static("math/oriented_angles_between_vectors.png")), caption: [Oriented angles between example vectors *u* and *v*.], ) Some applications call for the *oriented* angle $psi$. It's the angle going from one vector to the other in a specific direction. The convention that I use is that the direction be *counter-clockwise*. Because the angle is oriented, changing the order of vectors (or the used convention) changes the angle's sign: $ psi(bold(u), bold(v)) = -psi(bold(v), bold(u)) $ or, equivalently: $ psi(bold(u), bold(v)) = 2 pi - psi(bold(v), bold(u)) $ Furthermore: $ psi(bold(u), bold(u)) = 0 $ It's also worth noting that: $ cos theta = cos phi = cos psi $ which is why the dot product doesn't carry enough information to compute $psi$. == In two dimensions $ psi(bold(u), bold(v)) &= op("atan2")(bold(u) times_2 bold(v), bold(u) dot bold(v)) $ where the "two-dimensional cross product" is defined as: $ bold(u) times_2 bold(v) = u_1 v_2 - u_2 v_1 = u v sin theta $ == Derivation: in two dimensions Using the geometric definitions of the two-dimensional cross product (above) and the dot product: $ cases( bold(u) dot bold(v) = u v cos theta, bold(u) times_2 bold(v) = u v sin theta, ) $ Solving for the sine and cosine: $ cases( cos theta = 1 / (u v) bold(u) dot bold(v), sin theta = 1 / (u v) bold(u) times_2 bold(v), ) $ Solving for $theta$ (see #link-self("math/atan2#systems")[Systems of equations involving the cosine and the sine of an unknown]): $ theta &= op("atan2")(1 / (u v) bold(u) times_2 bold(v), 1 / (u v) bold(u) dot bold(v)) \ &= op("atan2")(bold(u) times_2 bold(v), bold(u) dot bold(v)) $ The simplification can be done because $1 / (u v) > 0$ and eliminating it will not change the result (see #link-self("math/atan2")[properties of atan2]).