2D Transforms
I’ve been doing a lot of matrix and graphics programming for the first time in my career and it’s been tough. So I’m killing two birds with one stone here: creating a reference that even I can follow, and learning the material by teaching it.
👩🏫 Basic math
Here’s a quick referesher on the basics of trigonometry and linear algebra that you will need to understand. Skip to Rotation if you still remember all this.
Trig
The important aspects of trig for our purposes can be summarized in the following 3 diagrams.
The most important takeaway is that and are equal to and respectively when (the unit circle). This means that for any polar coordinates , the cartesian coordinates are and .
Linear algebra
Matrix multiplication is most of what we need. Let’s do a simple 2x2 x 1x2
matrix.
First we do the dot product of the first row with the first column of the second ().
Then the dot product of the second row with the first column.
This gives us a final result.
Check out this animation for another explanation.
Identity matrix
One more important concept is the identity matrix. The identity matrix will result in the same matrix when multiplied, so it’s a good starting point to not change things you don’t want changed with multiplying matrices together.
Identity matrices always have 1s down the diagonal, so the identity matrix for 2D transforms is:
🔄 Rotation
Everybody starts with Translation, but I’m starting with rotation because that’s more interesting. Once you have rotation down, translation is a 🍰.
The goal is to express as a function of:
- The original position
- An angle
- A point that we’re rotating around. For this exercise we will start rotating around the origin
We can define x
and y
in terms of their polar coordinates, ie. their rotation () around the origin from
And we can define in terms of their differences from x & y.
Now… look at x
and y
and the expanded x'
, y'
. You can see a simple substition to get:
This can be represented in matrix form as:
Rotating around a point other than the origin
This only get slightly more complicated if we rotate around a point other than the origin. Let’s call the point to rotate around
That can be expressed in matrix form as well, of course (with some new concepts we haven’t seen yet…)
That seems complicated! The easier way to express this is to first move our frame our reference (translate) and then rotate around the origin of that frame of reference using the standard rotation matrix we derived above.
That brings us to Translation.
↗️ Translation
Compared to what we just went through with Rotation, Translation is easy. You just add the change () in and and you get a new and .
In matrix form:
That’s all there is to it… until you want to both rotate and translate.
🔢 2D Transform via Linear Algebra
We can express both rotation and translation (as well as scale and shear) using a 2D Transformation matrix.
To do this, we tack our translation matrix as a column onto our rotation matrix.
Before we get into it, let’s recognize that this intuitively makes sense. If you have a matrix with 3 columns, you need 3 rows to do multiplication. We’re trying to end up with , so a 3x3 identity matrix with in the last column will do just that.
That just expresses the dot product of the first row, but the other two rows are just the identity and will result in .
Ok, so let’s take a look at a full transformation matrix with rotation and translation.
That’s all there is to it. Given a rotation of and translation of , you can use that matrix to apply both at the same time and get the correct 1.
Wrapping up
This isn’t comprehensive. There’s a lot more but Gemini can help you out with additional details. Some additional one-off notes for reference are below.
Full transformation matrix
A full matrix with all 4 factors — translation , rotation , scale , and shear — is
This is typically applied Scale → Shear → Rotate → Translate, which appears reversed when doing matrix multiplication.
Footnotes
-
We glossed over non-origin rotation before learning about translation and then didn’t mention it. Briefly, a rotation around a non-origin point without translating would be done by first applying a translation () to the point, applying the simple rotation matrix () and then applying the inverse of to move it back, ie ↩