Shows 2x2 matrices use for simple 2D transformations (rotation, translation, scaling…)
See the comments in source for detailed explanations
On line demo (seems to have some issues with simultaneous key press detection)
2x2 matrices have a lot of limitations :
- all transformations are origin centered
- they can’t provide basic transformations like translation
- some transformations like scaling, etc. imply unwanted translations
That’s why 3x3 matrices are a better choice ! See this repository
If points coordinates are represented by a vector or 2x1 column matrix, the multiplication of this vector by a 2x2 matrices is (I will represent coordinates by a row vector, when using a column vector just transpose the matrices used) :
Why would we want to express transformation by matrices multiplication ?
-
we could expresse all transformation with a single operation (multiplication)
-
as matrix multiplication is associative, by multiplying all transformation matrices in the correct order, we could create a unique matrix for a given combined transformations, in some cases (for exemple, apply the same transformation to a huge amount of points) the gain in performance would be noticeable.
-
don’t forget that matrix multiplication is non-commutative : matrices order is important and can’t be changed !
Let‘s see how different 2x2 matrices could be used to express different transformations.
Multiplication by the identity matrix (a matrix filled of 1s on the diagonal only) seems useless. But it shows us the logic of using matrix multiplication to apply a transformation. Well chosen parameters in the 2x2 transformation matrix will affect the coordinates of a point, and therefore, the form of a figure (that’s what a transformation is, isn’t it ?)
If we replace the 1s by 10s in the identity matrix, we see that the coordinates will be expanded by a 10 factor. If we apply this to all the points of a figure, all the distances will be expanded, like in a scaling transformation. The scaling factor can be different for x and y.
Thus, here is the scaling matrix, with scaling factors Sx and Sy :
But this method has a drawback : by directly changing x and y to change the scale of the figure, we will also change the position of the figure…
If we put a scaling factor outside of the diagonal of the matrix, what happens ?
We see that as far as y gets bigger (or smaller) x is proportionnally affected, but y remains the same. It produces a deformation along the x axis like the figure got teared called shear.
As above, directly modifying x also change the x position of the figure…
The matrix for y shearing is :
Get back to the identity matrix, and change a 1 by a -1 :
It results by an horizontal flip, as if the figure were reflected with respect of the y axis.
The matrix for a vertical flip :
The drawback of this method is that the figure is transformed regarding x and y axes.
The rotation formulas are harder to explain.
First of all, a reminder of basic trigonometry formulas :
Let’s apply these formulas in the context of a rotation :
Let’s do some mathematical manipulations :
(Angle sums identities on Wikipedia in case you forgot)
We can replace r cos φ by x and r sin φ by y and get :
We can get the same result with this matrix product :
Thereby the rotation matrix is :
The drawback of this formula is that rotation is (x and y) axes origin centered.
Anti-clockwise rotation can easily be realised with the rotation matrix, you just need to use -θ rather than θ.
But you can also stick to θ and use a very similar matrix :
Of course the rotation is also axes origin centered.
2x2 matrices have a major drawbacks : they can’t express translation.
For exemple : consider a point translation of 10 units along the x axis and 8 units along the y axis, we can express this translation by :
Of course if we want to express this transformation with matrices, 2x1 matrices addition is trivial :
but 2x2 matrices can’t express such transformation by multiplication :
Each term is either a multiple of x or a multiple of y. We can’t add a constant.
We have seen that, by constructions, 2x2 matrices transformation lead to unwanted translation or are axes/origin centered.
Moreover, we have seen that translation can be expressed by addition but not really by multiplication.
Actually, we can build 2x2 matrices that performs rotation centered on any arbitrary point, as if the figures were translated to rotate around this point. I will not do the math, but we can understand that if translation is obtained “by addition” and rotation “by multiplication” the matrix formula will contain an additive term. The building process gets more complex, and as not straightforward as the genuine multiplication of transformation matrices. Especially if we want to chain transformations.
That’s why 3x3 matrices – with a little trick – are a better choice ! See this repository
To run the demo :
make run
Build .js executable for the web :
make js
- Maybe add figures to explanations !
- The .js version seems to have issues detecting simultaneous key press.