# Rotations in 2D space

Rotation matrix and DCM

This article is an introduction to the basic concept in 2D space. The following are the `R`

packages used in this post.

```
library(magrittr)
library(ggplot2)
theme_set(theme_bw())
```

## Rotation of a vector in 2D plane

Let us consider a vector \(\underline{a} = (3, 1)\) on the 2D plane, then we can visualize the vector as follows;

Now, if we rotate the vector \(\underline{a}\) by a counterclockwise angle \(30^\circ\) about the origin, then the result vector \(\underline{a}'\) will look like this.

### Rotation matrix

The matrix \(R\), called a rotation matrix, represents the counter-clockwise rotation by angle \(\theta\) (radian), which can be written as follows: \[ R_{\theta}=\left(\begin{array}{cc} cos\left(\theta\right) & -sin\left(\theta\right)\\ sin\left(\theta\right) & cos\left(\theta\right) \end{array}\right) \] To rotate a vector using the rotation matrix, we need to multiply the rotation matrix by the vector as follows: \[ \underline{a}' = R_{\theta} \underline{a} \] where \(\underline{a}'\) indicates the rotated vector of \(\underline{a}\). Thus, in the above example, since we have a counter-clockwise \(30 ^\circ\) rotation, we have the following rotation matrix:

```
rotation_mat <- function(theta, degree = FALSE){
if (degree){
theta <- theta * pi / 180
}
rmat <- matrix(c(cos(theta), -sin(theta),
sin(theta), cos(theta)), nrow = 2, byrow = T)
return(rmat)
}
rmat <- rotation_mat(30, degree = TRUE)
rmat
## [,1] [,2]
## [1,] 0.8660254 -0.5000000
## [2,] 0.5000000 0.8660254
```

Using this rotation matrix, we can calculate the endpoint of the rotated vector, \(\underline{a}'\), as follows:

```
as.vector(rmat %*% c(3, 1))
## [1] 2.098076 2.366025
```

## Rotation of frame in 2D space

In the previous section, the rotated object was a vector. However, sometimes we have a situation where the coordinate system is rotated rather than the vector. Let us consider the same example that we had in the previous section. We have the same vector \(\underline{a}\) on the initial 2D space, and we call this coordinate system as a reference frame. In this reference frame, the coordinates of the vector are \((3, 1)\), because the vector \(\underline{a}\) can be represented as a linear combination of the basis vector \(e_1, e_2\) of the frame as follows:

\[ \underline{a}=3e_1 + 1e_2, \] where \(e_1\) and \(e_2\) are \[ e_1 = (1, 0)^T, \quad e_2 = (0, 1)^T \]

### Rotating the reference frame

What if the reference frame is rotated by a clockwise angle \(\theta\) to another frame? Furthermore, what will be the coordinates of the vector \(\underline{a}\) in the new rotating frame? The following figure shows the situation where the initial frame is rotated by a clockwise \(30 ^\circ\). The solid red lines represent the x and y-axis in the new frame, called \(x'\) and \(y'\).

In the new frame, the coordinates of \(\underline{a}\) will be \((2.098076, 2.366025)\), which is the same coordinates we had in the previous vector rotation case. Thus, rotating the initial frame by a clockwise angle \(\theta\) has the same effect as the counter-clockwise rotation of the vector by an angle \(\theta\) when we consider the measurements of the vector.

Let us define some notations to describe the situation more precisely. Let us denote \(I\) and \(B\) by the initial frame and the rotating frame (usually called body frame), respectively. Then, the rotating the **frame** \(B\) by a counter-clockwise angle \(\theta\) with respect to the frame \(I\) can be denoted by a matrix \(C^{B|I}_{\theta}\), which can be written as follows:
\[
\begin{align*}
C_{\theta}^{B|I} & =R_{-\theta}\\
& =\left(\begin{array}{cc}
cos\left(-\theta\right) & -sin\left(-\theta\right)\\
sin\left(-\theta\right) & cos\left(-\theta\right)
\end{array}\right)\\
& =\left(\begin{array}{cc}
cos\left(\theta\right) & sin\left(\theta\right)\\
-sin\left(\theta\right) & cos\left(\theta\right)
\end{array}\right)
\end{align*}
\]
Note that the first line in the formula shows that the rotating the frame has the same effect of rotating the objects opposite direction. The defined matrix \(C_{\theta}^{B|I}\) called direction cosine matrix (DCM). Why do we call this matrix DCM even though we have cos and sin function in the matrix? The answer can be shown with the conversion of the sine function to the cosine function using the identities as follows:
\[
\begin{align*}
C_{\theta}^{B|I} & =\left(\begin{array}{cc}
cos\left(\theta\right) & sin\left(\theta\right)\\
-sin\left(\theta\right) & cos\left(\theta\right)
\end{array}\right)\\
& =\left(\begin{array}{cc}
cos\left(\theta\right) & cos\left(\frac{\pi}{2}-\theta\right)\\
cos\left(\frac{\pi}{2}+\theta\right) & cos\left(\theta\right)
\end{array}\right)\\
& =\left(\begin{array}{cc}
cos\left(\theta_{11}\right) & cos\left(\theta_{12}\right)\\
cos\left(\theta_{21}\right) & cos\left(\theta_{22}\right)
\end{array}\right)\\
& =\left(\begin{array}{cc}
C_{11} & C_{12}\\
C_{21} & C_{22}
\end{array}\right)
\end{align*}
\]
DCM has also have geometric interpretation of the angles \(\theta_{11}, \theta_{12}, \theta_{21}, \theta_{21}\) as follows:

- \(\theta_{11}\): counter clockwise angle from \(x\)-axis to \(x'\)-axis.
- \(\theta_{12}\): counter clockwise angle from \(x\)-axis to \(y'\)-axis.
- \(\theta_{21}\): clockwise angle from \(y\)-axis to \(x'\)-axis.
- \(\theta_{22}\): counter clockwise angle from \(y\)-axis to \(y'\)-axis.

Thus, in our example, we have the following values for \(\theta\)s in the DCM: \[ \left(\begin{array}{cc} cos\left(\theta_{11}\right) & cos\left(\theta_{12}\right)\\ cos\left(\theta_{21}\right) & cos\left(\theta_{22}\right) \end{array}\right) = \left(\begin{array}{cc} cos\left(330^{\circ}\right) & cos\left(60^{\circ}\right)\\ cos\left(120^{\circ}\right) & cos\left(330^{\circ}\right) \end{array}\right) \] whose values are the same as before.

```
matrix(c(cos(11*pi / 6), cos(pi / 3),
cos(2*pi / 3), cos(11*pi / 6)), ncol = 2, byrow = T)
## [,1] [,2]
## [1,] 0.8660254 0.5000000
## [2,] -0.5000000 0.8660254
```

## Transfer coordinates

To summarize the discussion so far, coordinates of a given vector \(\underline{a}\) in the initial frame can be transferred to the rotating frame by using the following matrix multiplication. \[ \underline{a}_{B} = C^{B|I}_{\theta} \underline{a}_{I} \] where \(\underline{a}_I\) and \(\underline{a}_B\) represent the coordinates (or measurement) of the vector \(\underline{a}\) in the initial frame and the body frame, respectively.

### Reference

[1] Kim, P. (2013). Rigid body dynamics for beginners: euler angles & quaternions.

[2] Green, P. E. (2014). Mathematical tools for applied multivariate analysis. Academic Press.