Understanding 2D rotation matrices
published:
categories: misc
When I first learned about rotation matrices they appeared quite “magic”; if you squinted your eyes a bit it sort of made sense, and if you did the math you could prove that the matrix does indeed perform the rotation and that all the group properties are met, but none of that explains where that form comes from, why it works. In this blog post I will explore a way to derive the formula for rotation matrices step by step. If you wish to follow along you need only basic knowledge of linear algebra and trigonometry.
This post makes extensive use of MathML, if your browser does not support it you will be seeing gibberish.
Points on the unit circle
We start our journey with the simple case of the unit circle. A unit circle in the Euclidean plane is a circle with its center at the origin and a radius of . Each point on the plane is given by a pair of coordinates. If we limit ourselves to the unit circle we observe that each point is uniquely identified by an angle around the center. For convenience we will choose that the point corresponds to the angle , and that rotations go counter-clockwise. Both of these are long-established conventions.
Using basic trigonometry we can see that for a given angle the coordinates of the point are ; this is true because we can draw a right-angled triangle where the length of the hypotenuse is the radius of the circle and the lengths of the catheti are the coordinates of the point.
Rotations along the unit circle
We can rotate the point around the origin by adding an angle to . Thus we are looking for a matrix which solves the equation
We are going to make use of two trigonometric identities, their proof is left as an exercise to the reader.
With these identities we can find the rotation matrix by taking the resulting vector apart.
This is indeed the familiar rotation matrix formula. We found it just by applying familiar knowledge from trigonometry.
Rotation of arbitrary points
Let us now widen our scope to all points in the plane: a point is now uniquely identified by its angle of rotation and by the distance from the origin. Using the same arguments as above, but taking into account that the length of the hypotenuse is now , we get the coordinates .
It is easy to confirm that our previously found formula for rotation matrices works for points outside of the unit circle as well.
Rotating and scaling points
As far as rotations go we are done, but we can take it a step further and add a scaling factor to the formula as well. If we wish to scale one coordinate of the vector we have to scale the corresponding row of the matrix, thus to uniformly scale the entire vector we have to uniformly scale the entire matrix.
Consequences
A number of operations can be expressed as special cases of our rotate-scale matrix.
- Identity
The identity transformation is represented by the identity matrix, which corresponds to a scale factor of and rotation angle of .
- Scaling
A pure scaling has a variable scaling factor and a fixed rotation angle of . A scaling matrix is thus just a uniformly scaled identity matrix.
- Inversion or reflection
Reflecting a point along the origin can be interpreted either as a rotation by without scaling, or as a scaling by without rotation. Both yield the same matrix.
The group of rotation and scaling matrices
The matrices of rotation and scaling form a group. If we apply a transformation to a point, then apply another transformation to the result it is equivalent to applying one combined transformation to the original point. We combine transformations by multiplying their matrices.
Not only is this a rotation matrix, the result is also independent of the order of operands, something that is generally not true for matrix multiplication. We are thus dealing with a commutative magma. This magma is also an Abelian group:
- The neutral element is the identity transformation.
- The inverse of a transformation with scale and angle is a transformation with scale and angle .
- Since matrix multiplication is associative in general, the composition of transformations must be associative as well.
Conclusion
We have derived the formula for rotation matrices without prior knowledge of what result to work towards. Instead we restricted our research to a very basic case, that of points on a unit circle, and used our knowledge of trigonometry to find a solution. Once we had our simple solution we extended our problem domain to that of arbitrary points and the scaling of vectors, and looked for ways to extend our simple solution to that new domain.
We then investigated some of the properties and concluded that what we have is a group structure, which allows use to use all results from group theory as well. There is actually much more to rotation matrices, but that would be beyond the scope of this post. I mainly wanted to show how one can come up with this formula that usually just appears like “magic” by starting with a simple base case and then further generalising from there.