Skip to main content


The translation problem

So far in this series we've learnt how to use linear transformation matrices to rotate points around the origin. This is all well and good but it's really not enough to just be able to rotate points, we would also like to translate them. We need to be able to move them around the plane (left/right/up/down etc.) - otherwise we would be stuck designing robots that just spin around on the spot!

Translation Matrices

On the surface, this seems like a very straightforward problem to solve - we simply need to add the appropriate amount to the xx and yy coordinates. Say, for example, that we had the point (x,y)(x,y) and we wanted to shift it by sxs_x units in the x direction and sys_y units in the y direction. We simply perform the following addition:

[xy]+[sxsy]=[x+sxy+sy]\begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} s_x \\ s_y \end{bmatrix} = \begin{bmatrix} x + s_x \\ y + s_y \end{bmatrix}

Seems simple enough, right? The problem is that this operation is non-linear. If you remember, all the transformations we've looked at so far have been of the form f(p)=Apf(\mathbf{p}) = \mathbf{A}\mathbf{p} which is linear. But our translation looks like f(p)=p+bf(\mathbf{p}) = \mathbf{p} + \mathbf{b} .

While it's not the end of the world, having to introduce this nonlinearity is a bit unfortunate. We suddenly lose all those key properties we had with linear transformations, most notably how easily we could chain different transformations together. You'll recall that if we had three nested transformations we could simply multiply the matrices together: f3(f2(f1(p)))=A3A2A1pf_3(f_2(f_1(\mathbf{p}))) = \mathbf{A}_3\mathbf{A}_2\mathbf{A}_1\mathbf{p} .

Let's take a look at what happens if we want to perform the following steps:

  1. Shift a point by (sx,sy)(s_x, s_y), then
  2. Rotate it by θ\theta, then
  3. Shift it again by (tx,ty)(t_x, t_y), then
  4. Rotate it by ϕ\phi

This chain produces the following equation:

p2=R(ϕ)(R(θ)p+s)+t=R(ϕ)R(θ)p+R(ϕ)s+t\begin{align*}\mathbf{p}_2 &= \mathbf{R}(\phi)(\mathbf{R}(\theta)\mathbf{p} + \mathbf{s}) + \mathbf{t} \\ &= \mathbf{R}(\phi) \mathbf{R}(\theta)\mathbf{p} + \mathbf{R}(\phi)\mathbf{s} + \mathbf{t}\end{align*}

If we were to write these matrices out in full, the equation quickly becomes very confusing. On top of that, inverting the combined transformation becomes really awful. There must be a better way! Thankfully, there is.

Introducing... Homogeneous Coordinates!

To solve this problem we're going to introduce a slightly modified representation of our coordinates. This new system is called homogeneous coordinates. What we'll discover in this post and the next is that by using a homogeneous coordinate system, we can represent both rotations and translations using a single matrix. In this post, we'll focus solely on the translation.

The first thing we have to do is modify our coordinates, which simply involes tacking a "11" onto the end of our point vector. For the next little while we will use the bar (pˉ\bar{\phantom{p}}) above our various variable names to express that they are working with the homogeneous coordinates, but in later posts it will just be assumed.

pˉ=[xy1]\bar{\mathbf{p}} = \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

Deriving the Translation Matrix

What we want to try to do now is to find a linear transformation in this new coordinate system that would represent our translation. In 2D, this will mean we are looking for a 3×33 \times 3 matrix to multiply by pˉ\bar{\mathbf{p}} that is equivalent to adding s=[sx,sy,0]T\mathbf{s} = [s_x, s_y, 0]^\text{T}.

pˉ2=Apˉ1=pˉ1+s[x2y21]=[?????????][x1y11]=[x1+sxy1+sy1]\bar{\mathbf{p}}_2 = \mathbf{A} \bar{\mathbf{p}}_1 = \bar{\mathbf{p}}_1 + \mathbf{s}\\ \begin{bmatrix} x_2 \\ y_2 \\ 1 \end{bmatrix} = \begin{bmatrix} ? & ? & ? \\ ? & ? & ? \\ ? & ? & ? \end{bmatrix}\begin{bmatrix} x_1 \\ y_1 \\ 1 \end{bmatrix} = \begin{bmatrix} x_1 + s_x\\ y_1 + s_y \\ 1 \end{bmatrix}

Let's figure this out, step by step. Firstly, we need to guarantee a 11 in the bottom element of the result. To achieve this, the elements in the bottom row of our matrix will need to be all 00, except for a 11 at the end.

[??????001][x1y11]=[??1]\begin{bmatrix} ? & ? & ? \\ ? & ? & ? \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix} x_1 \\ y_1 \\ 1 \end{bmatrix} = \begin{bmatrix} ? \\ ? \\ 1 \end{bmatrix}

Secondly, we know that for the first element of our result, there is one x1x_1 and no y1y_1, and vice versa for the second element. To get this, we put a little identity matrix in the top left corner.

[10?01?001][x1y11]=[x1+?y1+?1]\begin{bmatrix} 1 & 0 & ? \\ 0 & 1 & ? \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix} x_1 \\ y_1 \\ 1 \end{bmatrix} = \begin{bmatrix} x_1 + ? \\ y_1 + ? \\ 1 \end{bmatrix}

Lastly, the top of the right column of our matrix will contain the column vector we want to translate by.

[10sx01sy001][x1y11]=[x1+sxy1+sy1]\begin{bmatrix} 1 & 0 & s_x \\ 0 & 1 & s_y \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix} x_1 \\ y_1 \\ 1 \end{bmatrix} = \begin{bmatrix} x_1 + s_x \\ y_1 + s_y \\ 1 \end{bmatrix}

And we're done! By using homogeneous coordinates, we can represent our non-linear translation as a linear transformation. The reason this works is that although a translation is not a linear transformation, it falls under a bigger subset of non-linear transformations called affine transformations. This will work in 2D, 3D, or however many dimensions you want!

Next Steps

Below you can see an example of translations in action. In the next post we'll combine translations and rotations into a single transformation matrix.





Translation Matrix Plot
% Set up an array of points
x_points = [2, 2, 0.5, -1, -1, 2];
y_points = [-1, 2, 3, 2, -1, -1];
points = [x_points; y_points; ones(1, length(x_points))];

% Translation matrix
sx = 2; sy = 3;
trans_mat = [1, 0, sx; ...
0, 1, sy; ...
0, 0, 1];

% Transform the points
for p = 1:size(points,2)
trans_pts(:,p) = trans_mat * points(:,p);

% Plot everything
plot(0,0,'+k', 'DisplayName', 'Origin');
hold on;
plot(sx,sy,'+g', 'DisplayName', 'Translated Origin');
plot(points(1,:), points(2,:), 'x-k', 'DisplayName', 'Original Points');
plot(trans_pts(1,:), trans_pts(2,:), 'x-g', 'DisplayName', 'Translated Points');
legend show; grid on; axis equal;