Math behind rotation in MS Paint For those who don't know, MS Paint only has the options to rotate an image by right angles. To carry out an arbitrary rotation ($\theta^\circ$), the following hack is suggested:


  
  Horizontal skew by $\theta$
  Vertical Stretch by $\displaystyle \frac{1}{\cos^2\theta}$
  Vertical Skew by  $-\theta$
  Horizontal and Vertical Stretch by $\cos \theta$
  


I would like to know how this works.

(I guess one way would be to show that multiplying the above operations represented as matrices should return the $2$D rotation matrix, but I do not know what are the matrix representations for the intermediate steps.)
I think the following matrix product gives an explanation.

The first step is given by the matrix 
$$
S_1=\left(\begin{array}{cc}1&amp;\tan\theta\\0&amp;1\end{array}\right).
$$

The second step corresponds to
$$
D_1=\left(\begin{array}{cc}1&amp;0\\0&amp;\frac{1}{\cos^2\theta}\end{array}\right).
$$

The second shearing operation corresponds to
$$
S_2=\left(\begin{array}{cc}1&amp;0\\-\tan\theta&amp;1\end{array}\right).
$$

At this point we have done $S_2D_1S_1$ that after a little bit of matrix manipulation and using the identity $\sin^2\theta +\cos^2\theta=1$ becomes
$$
S_2D_1S_1=\left(\begin{array}{cc}1&amp;\tan\theta\\-\tan\theta&amp;1\end{array}\right).
$$

The last step amounts to a scalar multiplication by $\cos\theta$ and gives us the familiar rotation matrix
$$
\cos\theta S_2D_1S_1=\left(\begin{array}{rc}\cos\theta&amp;\sin\theta\\-\sin\theta&amp;\cos\theta\end{array}\right).
$$



Edit: I got to use this as an exercise in a freshman course that just ended. I was actually a bit unhappy with that youtube video, because it doesn't show the transitions from one step to the other. To fix this problem I made an animation, where the four steps are done gradually: in steps one and three I continuously move the shear (skew) parameter from zero to the desired value. Similarly in steps two and four the scaling parameter changes continuously from one to the proper value. All the steps are done sequentially.
It is worth noting that you can accomplish rotation with only a single trip to the Resize and Skew menu.
Paint applies the operations in the order: Resize, Skew Horizontal, Skew Vertical.

As a sequence of matrix transforms acting on a column vector, that is:

$$(\begin{bmatrix}1&amp;0\\K_v&amp;1\end{bmatrix}(\begin{bmatrix}1&amp;K_h\\0&amp;1\end{bmatrix}(\begin{bmatrix}S_h&amp;0\\0&amp;S_v\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix})))$$

Where $K_v$ and $K_h$ are the vertical and horizontal skew factors, and $S_v$ and $S_h$ are likewise the scale factors.

Because matrix multiplication is associative, let's remove the parentheses and multiply these transforms from left to right to give an equivilent single-matrix transform:
$$\begin{bmatrix}S_h&amp;K_hS_v\\S_hK_v&amp;K_hK_vS_v+S_v\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}$$

Setting this matrix equal to the rotation matrix, $\begin{bmatrix} \cos(θ) &amp; \sin(θ) \\ -\sin(θ) &amp; \cos(θ) \end{bmatrix}$, we get a system of equations which we can solve for our scale and skew factors:
$$S_h = \cos(θ)$$
$$K_h = \sin(θ)cos(θ)$$
$$S_v =1/\cos(θ)$$
$$K_v = -\tan(θ)$$

These variables describe factors in an affine transformation, but paint wants "% Resizing" and "degrees skew", so input:
$$\%R_h = 100*S_h=100*\cos(θ)$$
$$\%R_v = 100*S_v=100/\cos(θ)$$
$$DegSk_h = \arctan(K_h)=\arctan(\sin(θ)\cos(θ))$$
$$DegSk_v = \arctan(K_v) = \arctan(-\tan(θ))=-θ$$

As an operational note: Paint will automatically resize your selection after a skew if the skew transformation would put the selection outside of the canvas, so make sure you have plenty of room around your selection.