# Linear and Affine Transformations

## Cheatsheet about affine transformation matrices

When we talk about math in computer graphics, of course matrix transformations constitute a very important part of it. In the end, you should convert these triangles to some 2D projection to be able to rasterise them in OpenGL.

The low-level transformations we do in OpenGL using matrices nicely map to transformations we have studied during the Linear Algebra course. We have rotation, shearing, and scaling which work the same. However, we also need translation, which is very simple to define but actually forbidden in linear transformations. Therefore, we can look at affine transformations, which allow that.

## Affine transformations using matrices

An affine transformation of a vector within a simple dimension (i. e. without projection) is a linear transformation output plus some constant vector, which is the amount of translation *after* all other transformations performed.

and the corresponding inverse transformation would be

$A_{−1}(v’-t) =v$Implementing this like this everywhere would produce ugly code and would’t be so performant compared to pure linear transformations.

Computer graphics gods had a very simple solution to this. They would just have one more dimension in the vector than the dimension of the space in context, to store an arbitrary constant to be employed in some clever math.

In the case of 3D affine transformations, the fourth component of the vec4 is kept at 1.

`vec4 vertex = vec4(x, y, z, 1.0);`

Now let’s look at how we can have a translation (transformation with only nonzero translation) matrix.

$(for v having the fourth component as 1) 1000 0100 0010 2341 v = v+ 2340 $In general, translation of a 3D vector v translated by [x y z]^T is given by.

$ 1000 0100 0010 ΔxΔyΔz1 v = v+ ΔxΔyΔz0 $Combining this with other types of transformations is also dead simple. You just need to replace the components m11 - m33 with the linear transformation matrix values which transform the vector *before* a final translation. If you want to find the transformation matrix with the translation applied before some transformations, you need to find the transformation matrices of each step separately and multiply them, first step to the RHS.

These don’t go beyond the LA course but the other 4x4 3D transformation matrices would be:

## Linear scaling

$ s_{x}000 0s_{y}00 00s_{z}0 0001 $## Linear right-handed rotations

Add the identity matrix column and row to the following 2D matrix at the index of the rotation axis:

$(cosθsinθ −sinθcosθ )$Which gives

Rotation around +x

$ 1000 0cosθsinθ0 0−sinθcosθ0 0001 $Rotation around +y

$ cosθ0−sinθ0 0100 sinθ0cosθ0 0001 $Rotation around +z

$ cosθsinθ00 −sinθcosθ00 0010 0001 $Notice that all these matrices are orthogonal.