# 2D transformation matrix to make a trapezoid out of a rectangle

In most vector graphic software libraries I can use (3x3) matrices to transform 2D geometry (e.g. scale, rotate, skew). How does a matrix need to look like to transform a 2D rectangle to a symmetrical trapezoid (or equilateral triangle if parameters are taken to the extreme)? What parts of the matrix define which parameters?

The transformations that you mentioned are all instances of affine transformations of the plane. Working in homogeneous coordinates, they are represented by $$3\times3$$ matrices of the form $$M=\begin{bmatrix}A&\mathbf t\\\mathbf 0^T&1\end{bmatrix},$$ where $$A$$ is some invertible $$2\times2$$ matrix. Since, as Jyrki Lahtonen pointed out, affine transformations can only map parallelograms to other parallelograms, you’ll need some other type of transformation. If you allow the last row of $$M$$ to be something other than a multiple of $$(0,0,1)$$, the resulting matrix represents a projective transformation of the plane. Just as there is an affine transformation that maps any given pair of triangles, it turns out that for any pair of complete quadrilaterals (no three vertices colinear), there is an invertible projective transformation that maps one onto the other. This answer explains how to construct an appropriate matrix, but any 2-D graphics library worth its salt provides a built-in function to do this. It might take the form of computing a mapping between the unit square and an arbitrary quad, in which case you’ll need to compose two of these more primitive projective transformations to get the one you want.

Note that when homogeneous coordinates of a point on the plane are viewed as points in 3-D space, all of these transformations are linear. This is one of the appealing properties of working with homogeneous coordinates: a large class of interesting transformations can be represented as matrices, composition becomes matrix multiplication, and so on. If you’re willing to step outside this framework, the transformation described by Pedro Gimeno in his answer is worth knowing about. It’s much cheaper to construct than a projective transformation, but can have some inconvenient behaviors when applied to points outside of the two regions being mapped.

If you are looking for a transformation determined by multiplication with a 2x2 matrix, then I'm afraid this cannot be done. Such a transformation is linear, so maps a vector to the same vector irrespective of its starting points. In other words, if $A,B,C,D$ are points on the plane such that $\vec{AB}=\vec{CD}$, and $A',B',C',D'$ are their respective images under this transformation, then we also have $\vec{A'B'}=\vec{C'D'}$.

So a linear transformation will map any parallelogram to another parallelogram, because the opposite sides of a parallelogram form the same vector. As that does not hold for a trapezoid, a linear transformation cannot turn a rectangle (= special case of a parallelogram) into such a trapezoid that is not also a parallelogram.

• A 3D-rectangle OTOH will often look like a trapezoid, when it is projected to the plane of the monitor. That is because its coordinates will be divided by the distance (IIRC often the $z$-coordinate gets that role), because in the projection a point is moved along a line of sight starting from the origin. – Jyrki Lahtonen Jul 10 '12 at 19:54
• You can use a projective transformation. I would go to homogeneous coordinates, and use a suitable $3\times 3$ matrix. With the right matrix, one can transform any quadrilateral into any other quadrilateral. This is a relatively standard computer vision problem, and there must be modern implementations. – André Nicolas Jul 10 '12 at 20:00
• @André: I suspected that much. Your explanation stirs a memory. But won't projective transformations preserve convexity? – Jyrki Lahtonen Jul 10 '12 at 20:04
• I should have said complete quadrilateral (or dually complete quadrangle). So no three points of the configuration on a line, no three lines on a point. Apart from that there is no restriction. – André Nicolas Jul 10 '12 at 20:24
• I think that $3\times 3$ is fine. – André Nicolas Jul 11 '12 at 0:52

There is a kind of transformation that is not linear but it is simple and fits the job. It allows you to transform the unit square into an arbitrary quadrilateral.

$$\pmatrix{x'\\y'} = \pmatrix{u_x&v_x&w_x\\u_y&v_y&w_y}\pmatrix{x\\y\\xy}$$

$$x'=u_xx+v_xy+w_xxy\\ y'=u_yx+v_yy+w_yxy$$

It transforms the unit square in a way controlled by the vectors $u=(u_x,u_y), v=(v_x,v_y), w=(w_x,w_y)$ as follows: Note however that it's not a perspective transformation.