# Converting Rotation Matrices to Angles

- Published
- Revisions

Rotation matrices are great for carrying out calculations, but in many situations, working with angles is more convenient. Constructing rotation matrices from angles is straightforward because that's how they're defined but getting back the angles can be tricky. In this article, we're taking a look how to convert an arbitrary rotation matrix to angles.

## In two dimensions

Let's start in two dimensions with the following rotation matrix:

\[ R = \begin{bmatrix} r_{11} & r_{12} \\ r_{21} & r_{22} \end{bmatrix} \]

Looking at the definition of counterclockwise rotation matrix, we know that there's some angle \(\theta\) we need to find:

\[ R = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} \]

In order to solve the unknown angle, it can be helpful to understand what the rotation matrix represents. Columns of the rotation matrix are vectors \((r_{11},r_{21})\) and \((r_{12},r_{22})\) which are the basis vectors \((1,0)\) and \((0,1)\) rotated by angle \(\theta\):

Based on the picture, we can find \(\theta\) using some trigonometry:

\[\tan\theta = \frac{r_{21}}{r_{11}}\] \[\theta = \arctan\left(\frac{r_{21}}{r_{11}}\right)\]

However, this will only work when \(-90\degree < \theta < 90\degree\). Instead of \(\arctan\), we can use arctan2 which works in the full range \(-180\degree < \theta \leq 180\degree\):

\[\theta = \text{arctan2}(r_{21}, r_{11})\]

Most programming languages should have **arctan2** or **atan2** function in their standard library.

## In three dimensions

Next, let's move up to the three dimensions with the following rotation matrix:

\[ R = \begin{bmatrix} r_{11} & r_{12} & r_{13}\\ r_{21} & r_{22} & r_{23} \\ r_{31} & r_{32} & r_{33} \end{bmatrix} \]

In three dimensions, rotation is commonly represented as Euler angles \(\psi\), \(\theta\) and \(\phi\) which describe the rotation of x, y and z axis respectively. In other words, the rotation consists of two-dimensional rotations around different axes:

The order in which the rotations are applied matters. We're going to use the natural order \(R_x\), \(R_y\) and \(R_z\) which can be combined into a single rotation matrix:

Solving the unknown angles is more complicated than in the two-dimensional case. Luckily, Slabaugh (n.d.) has published the following solution which takes into account different edge cases:

In the general case, there two solutions \(\psi_1\), \(\theta_1\), \(\phi_1\) and \(\psi_2\), \(\theta_2\), \(\phi_2\) which result in the same rotation. Here's an animation to illustrate the two solutions for rotating an object 180 degrees around the z axis:

However, when \(\theta = \pm90\degree\) there are an infinite number of solutions. This phenomenon is known as gimbal lock. For example, we get the same rotation when \(\theta=-90\degree\) and \(\psi=\phi=\alpha\) where \(\alpha\) can be any angle:

## References

- Slabaugh G. G. (n.d).
*Computing Euler angles from a rotation matrix*. Available at http://www.close-range.com/docs/Computing_Euler_angles_from_a_rotation_matrix.pdf.