[Math] Determining a 4×4 homogeneous matrix for a 2D transformation

geometrylinear algebramatricestransformation

Given the vertices of 2 triangles, as written below, how do I find a 4×4 homogeneous transformation matrix to describe the transformation from the first triangle to the second?

$$Triangle 1 = T_1 = \{(0,0)(0,1)(1,0)\}$$

$$Triangle 2 = T_2 = \{(1,1)(2,2)(3,1)\}$$

I can do this using 3×3 matrices, but am specifically asked for a 4×4 matrix. For 3×3 (below), I found the inverse of the matrix describing the first triangle in homogeneous coordinate. I multiplied that matrix by the homogeneous, 3×3 matrix of the second triangle, and ended up with a 3×3 transformation matrix. This matrix works, as when multiplied by $T_1$, you get $T_2$. I am assuming a 4×4 can be found by treating the z values as zeroes, but I'm not sure how to proceed.
$$ T_2 = R_{trans}*T_1 => R_{trans} = T_2*T_1^{-1}$$
$$T_1 = \begin{bmatrix}
0 & 0 & 1\\
0 & 1 & 0 \\
1 & 1 & 1 \\
\end{bmatrix} $$

$$T_1^{-1} = \begin{bmatrix}
-1 & -1 & 1\\
0 & 1 & 0 \\
1 & 0 & 0 \\
\end{bmatrix} $$
$$T_2*T_1^{-1} = \begin{bmatrix}
2 & 1 & 1\\
0 & 1 & 1\\
0 & 0 & 1 \\
\end{bmatrix} = R_{trans}$$

Best Answer

The most likely reason to want a $4\times4$ matrix for this is because you want to leverage some technology which is geared towards 3d operations. So you can think of your 2d coordinates as embedded into a 3d space which in turn is represented using homogeneous coordinates.

In general an affine 2d operation would have the following matrix representation in projective 3d:

$$\begin{pmatrix}x'\\y'\\0\\1\end{pmatrix}=\begin{pmatrix} *&*&0&*\\ *&*&0&*\\ 0&0&1&0\\ 0&0&0&1 \end{pmatrix}\cdot\begin{pmatrix}x\\y\\0\\1\end{pmatrix}$$

So you essentially insert a row and column into the matrix, namely the third row and column in the above matrix, to indicate that the third coordinate should be left as it is. That way a zero $z$ coordinate on input will become a zero $z$ coordinate on output.

If your transformation is not affine but projective, you can use a more general form where the last row can have arbitrary values except in the thrid column. That's what I used in this post when representing a 2d projective transformation for use in a matrix3d CSS transformation.