### Lei Mao

Machine Learning, Artificial Intelligence, Computer Science.

# Geometry for Computing Dihedral Angles

### Introduction

Computing dihedral angles are often necessary in physical systems. However, its mathematics are sometimes confusing if we have not seen them for a while. The last time I computed a dihedral angle could be dated back to the time when I was in college, and I have almost forgot how to compute it now.

In this blog post, I would like to discuss how to compute the dihedral angle defined in different ranges.

### Prerequisites

The prerequisites to computing dihedral angles are the definitions of dot product and cross product.

#### Dot Product

Dot product is related to one vector projection on another. Formally, the definition of dot product is

\begin{align} \mathbf{u} \cdot \mathbf{v} &= \| \mathbf{u} \| \| \mathbf{v} \| \cos \theta \end{align}

where $\theta$ is the angle between $\mathbf{u}$ and $\mathbf{v}$, and $\theta \in [0, \pi]$.

With this definition, we could have a series of properties for dot product,

• $\mathbf{u} \cdot \mathbf{v} = \mathbf{v} \cdot \mathbf{u}$
• $\mathbf{u} \cdot \mathbf{u} = \| \mathbf{u} \|^2$
• $\mathbf{u} \cdot (\mathbf{v} + \mathbf{w}) = \mathbf{u} \cdot \mathbf{v} + \mathbf{u} \cdot \mathbf{w}$ (proof)
• For any pair of the orthogonal unit vectors $\mathbf{i}$ and $\mathbf{j}$, we have $\mathbf{i} \cdot \mathbf{j} = 0$.

and further the mathematical expression for dot product using vector coordinates.

Suppose $\mathbf{u} = [u_1, u_2, \cdots, u_n]$ and $\mathbf{v} = [v_1, v_2, \cdots, v_n]$, we have the dot product expression

\begin{align} \mathbf{u} \cdot \mathbf{v} &= (u_1 \mathbf{i_1} + u_2 \mathbf{i_2} + \cdots + u_n \mathbf{i_n}) \cdot (v_1 \mathbf{i_1} + v_2 \mathbf{i_2} + \cdots + v_n \mathbf{i_n}) \\ &= u_1 v_1 + u_2 v_2 + \cdots + u_n v_n \\ \end{align}

It happens to be the same to the definition of inner product in real space.

For 3D space, we just have $\mathbf{u} = [u_1, u_2, u_3]$, $\mathbf{v} = [v_1, v_2, v_3]$, and

\begin{align} \mathbf{u} \cdot \mathbf{v} &= u_1 v_1 + u_2 v_2 + u_3 v_3 \end{align}

By the way, it is a common mistake that people are using the mathematical expression for dot product to prove the dot product product properties, since they overlooked the fact that we first have the definition and properties, and then we have the mathematical expression.

The length of the projected vector $\mathbf{u}$ on $\mathbf{v}$ is

\begin{align} \| \mathbf{u} \| \cos \theta &= \frac{\mathbf{u} \cdot \mathbf{v}}{\| \mathbf{v} \|} \end{align}

The projected vector is

\begin{align} \| \mathbf{u} \| \cos \theta \frac{\mathbf{v}}{\| \mathbf{v} \|} &= \frac{\mathbf{u} \cdot \mathbf{v}}{\| \mathbf{v} \|} \frac{\mathbf{v}}{\| \mathbf{v} \|} \\ &= \frac{\mathbf{u} \cdot \mathbf{v}}{\| \mathbf{v} \| \| \mathbf{v} \|} \mathbf{v} \\ &= \frac{\mathbf{u} \cdot \mathbf{v}}{\mathbf{v} \cdot \mathbf{v}} \mathbf{v} \\ \end{align}

#### Cross Product

Cross product is related to the area of a parallelogram formed by two vectors. Formally, the definition of cross product is

\begin{align} \mathbf{u} \times \mathbf{v} &= (\| \mathbf{u} \| \| \mathbf{v} \| \sin \theta) \mathbf{n} \end{align}

where $\theta$ is the angle between $\mathbf{u}$ and $\mathbf{v}$, and $\theta \in [0, \pi]$. $\mathbf{n}$ is the unique unit vector perpendicular to $\mathbf{u}$ and $\mathbf{v}$ such that $\{ \mathbf{u}, \mathbf{v}, \mathbf{n} \}$ forms a right-handed system. It should be noted that the cross product is only defined in the 3D space, which is different from the dot product. Also note that unlike dot product whose result is a scalar value, the result of cross product is a vector whose magnitude is $\| \mathbf{u} \| \| \mathbf{v} \| \sin \theta$.

With this definition, we could have a series of properties for cross product,

• $\mathbf{u} \times \mathbf{v} = - \mathbf{v} \times \mathbf{u}$
• $\mathbf{u} \times \mathbf{u} = 0$
• $\| \mathbf{u} \times \mathbf{v} \| = \| \mathbf{u} \| \| \mathbf{v} \| \sin \theta$
• $\mathbf{u} \times (\mathbf{v} + \mathbf{w}) = \mathbf{u} \times \mathbf{v} + \mathbf{u} \times \mathbf{w}$ (proof)
• $\mathbf{i} \times \mathbf{j} = \mathbf{k}$, $\mathbf{j} \times \mathbf{k} = \mathbf{i}$, $\mathbf{k} \times \mathbf{i} = \mathbf{j}$

and further the mathematical expression for cross product using vector coordinates.

Suppose $\mathbf{u} = [u_1, u_2, u_3]$ and $\mathbf{v} = [v_1, v_2, v_3]$, we have the dot product expression

\begin{align} \mathbf{u} \times \mathbf{v} &= (u_1 \mathbf{i} + u_2 \mathbf{j} + u_3 \mathbf{k}) \times (v_1 \mathbf{i} + v_2 \mathbf{j} + v_3 \mathbf{k}) \\ &= (u_2 v_3 - u_3 v_2) \mathbf{i} - (u_1 v_3 - u_3 v_1) \mathbf{j} + (u_1 v_2 - u_2 v_1) \mathbf{k} \\ &= \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ u_1 & u_2 & u_3 \\ v_1 & v_2 & v_3 \\ \end{vmatrix} \\ &= [u_2 v_3 - u_3 v_2, u_3 v_1 - u_1 v_3, u_1 v_2 - u_2 v_1] \\ \end{align}

The area of the parallelogram formed by two vectors is

$\| \mathbf{u} \| \| \mathbf{v} \| \sin \theta = \| \mathbf{u} \times \mathbf{v} \|$

### Dihedral Angle

Dihedral angle is the angle between two planes. We know that three points (two vectors) defines a planes. If we could somehow get the the vector that is perpendicular to the two planes, computing the dihedral angle between the two planes will be equivalent to computing the angle between the two perpendicular vectors.

Given two (non-parallel) vectors on the plane, we could compute the perpendicular vector based on the cross product definition.

$\mathbf{n} = \frac{\mathbf{u} \times \mathbf{v}}{\| \mathbf{u} \| \| \mathbf{v} \| \sin \theta}$

Actually, computing the unit perpendicular vector $\mathbf{n}$ is not necessary. $\mathbf{u} \times \mathbf{v}$ would be sufficiently a perpendicular vector.

Therefore, given $\mathbf{u}_1 \times \mathbf{v}_1$ from plane 1 and $\mathbf{u}_2 \times \mathbf{v}_2$ from plane 2, we could further compute the angle between $\mathbf{u}_1 \times \mathbf{v}_1$ and $\mathbf{u}_2 \times \mathbf{v}_2$ using the definition of dot product.

$\cos \theta = \frac{(\mathbf{u}_1 \times \mathbf{v}_1) \cdot (\mathbf{u}_2 \times \mathbf{v}_2)}{\| \mathbf{u}_1 \times \mathbf{v}_1 \| \| \mathbf{u}_2 \times \mathbf{v}_2 \|}$

If the dihedral angle is defined in a range of $[0, \frac{\pi}{2}]$, then

\begin{align} \theta &= \arccos \frac{| (\mathbf{u}_1 \times \mathbf{v}_1) \cdot (\mathbf{u}_2 \times \mathbf{v}_2) |}{\| \mathbf{u}_1 \times \mathbf{v}_1 \| \| \mathbf{u}_2 \times \mathbf{v}_2 \|} \end{align}

In some cases, the two planes are half-planes and the dihedral angles are defined in a range of $[0, \pi]$. The approach for computing the dihedral angle remains almost the same. However, if we are not careful, the actual dihedral angle could just be $\pi - \theta$, where $\theta$ is the dihedral angle we computed. The source of the problem is that the two right-handed system on the two planes are not correctly “aligned”. To make sure that the two right-handed system on the two planes are correctly “aligned”, we could find a point $P$ on the half-plane intersection and three vectors $\mathbf{b}_0$, $\mathbf{b}_1$, $\mathbf{b}_2$ such that the points $P + \mathbf{b}_0$, $P + \mathbf{b}_1$, $P + \mathbf{b}_2$ belong respectively to the intersection line, the first half plane, and the second half plane, then the dihedral angle is

\begin{align} \theta &= \arccos \frac{ (\mathbf{b}_0 \times \mathbf{b}_1) \cdot (\mathbf{b}_0 \times \mathbf{b}_2) }{\| \mathbf{b}_0 \times \mathbf{b}_1 \| \| \mathbf{b}_0 \times \mathbf{b}_2 \|} \end{align}

In some other cases, such as polymer physics, the dihedral angle is defined in a range of $[-\pi, \pi]$. For example, the protein backbone has three torsion angles $\phi$, $\psi$, and $\omega$ for each of its residues. Each of the torsion angles are defined in a range of $[-\pi, \pi]$. This is because, for example, the backbone conformation when the torsion angle is $\phi$ is totally different from the backbone conformation when the torsion angle is $-\phi$.

So to compute the dihedral angle $\theta$ defined in a range of $[-\pi, \pi]$, we not only have to know the value of $\cos \theta$, but also the value of $\sin \theta$.

Suppose we have four consecutive points $P_1$, $P_2$, $P_3$, $P_4$, and we would like to compute the dihedral angle between the plane $P_1 P_2 P_3$ and $P_2 P_3 P_4$ defined in a range of $[-\pi, \pi]$. We have three vectors

$\begin{gather} \mathbf{b}_0 = P_2 - P_1 \\ \mathbf{b}_1 = P_3 - P_2 \\ \mathbf{b}_2 = P_4 - P_3 \\ \end{gather}$

Computing $\cos \theta$ for the dihedral angle $\theta$ is similar.

\begin{align} \cos \theta &= \frac{ (\mathbf{b}_0 \times \mathbf{b}_1) \cdot (\mathbf{b}_1 \times \mathbf{b}_2) }{\| \mathbf{b}_0 \times \mathbf{b}_1 \| \| \mathbf{b}_1 \times \mathbf{b}_2 \|} \end{align}

In addition, we noticed that $\{\mathbf{b}_0 \times \mathbf{b}_1, \mathbf{b}_1, \mathbf{b}_2 \times \mathbf{b}_3\}$ forms a right-handed system, and the angle between $\mathbf{b}_0 \times \mathbf{b}_1$ and $\mathbf{b}_2 \times \mathbf{b}_3$ is exactly the dihedral angle $\theta$. So we could apply the definition of cross product to compute the $\sin \theta$.

Because

\begin{align} (\sin \theta) \mathbf{n} &= \frac{\mathbf{u} \times \mathbf{v}}{\| \mathbf{u} \| \| \mathbf{v} \|} \end{align} \begin{align} (\sin \theta) \mathbf{n} \cdot \mathbf{n} &= \frac{\mathbf{u} \times \mathbf{v}}{\| \mathbf{u} \| \| \mathbf{v} \|} \cdot \mathbf{n} \\ &= \sin \theta \\ \end{align}

Therefore,

\begin{align} \sin \theta &= \frac{ (\mathbf{b}_0 \times \mathbf{b}_1) \times (\mathbf{b}_1 \times \mathbf{b}_2) }{\| \mathbf{b}_0 \times \mathbf{b}_1 \| \| \mathbf{b}_1 \times \mathbf{b}_2 \|} \cdot \frac{\mathbf{b}_1}{ \| \mathbf{b}_1 \| } \\ &= \frac{((\mathbf{b}_0 \times \mathbf{b}_1) \times (\mathbf{b}_1 \times \mathbf{b}_2)) \cdot \mathbf{b}_1 }{\| \mathbf{b}_0 \times \mathbf{b}_1 \| \| \mathbf{b}_1 \times \mathbf{b}_2 \| \| \mathbf{b}_1 \|} \end{align}

Note that $\frac{\mathbf{b}_1}{ \| \mathbf{b}_1 \| }$ is the unit vector.

Because we have computed $\cos \theta$ and $\sin \theta$, the value of dihedral angle $\theta$ could be determined in the range of $[-\pi, \pi]$ explicitly using $\text{atan2}$. Specifically,

$\theta = \text{atan2}(\sin\theta, \cos\theta)$