summaryrefslogtreecommitdiff
path: root/site/content/math/angle_between_two_vectors.typ
blob: c3b9c9344af879be16a6e761b065ad384b563cec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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]).