# 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:

1. Horizontal skew by $\theta$

2. Vertical Stretch by $\displaystyle \frac{1}{\cos^2\theta}$

3. Vertical Skew by $-\theta$

4. 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

The second step corresponds to

The second shearing operation corresponds to

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

The last step amounts to a scalar multiplication by $\cos\theta$ and gives us the familiar rotation matrix

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. 