AffineTransformations - Serabe/rinzelight GitHub Wiki

Theory

Defining an affine transformation.

A pixel can be seen as a vector with four components: red, green, blue and alpha. Then, we can apply matrix multiplication to it, returning a new pixel.

We will call affine transformation to a 4×4 matrix.

Commutative… they’re not.

As most people know, matrix multiplication is not commutative, meaning that applying A and then B is generally not the same as applying B and then A, being A and B affine transformations.

Implementation

Getting an affine transformation.

There are several predefined transformations. For each of them there are a macro and a method.

Let scale be the transformation we want to work with, then the scale-transform macro returns an AffineTransformation object and the scale function returns a new image with the transformation applied.

rotate-transform macro accepts either one or three arguments. The first one is the angle of rotation and the optional two represent the center of rotation.

scale-transform accepts either one or two parameters. If two parameters are supplied, they are considered to be the scale for x and y respectively. If only one arguments is passed, it is used for both x and y.

shear-transform accepts the same parameters as scale-transform but it uses as shear values.

translate-transform accepts the same parameters as scale-transform and shear-transform but using them as translation values.

For each x-transform there is a x function that accepts the same parameters as the macro but with the image at the beginning and optional rendering hints at the end.

Applying an affine transformation.

Applying an affine transform is as easy as using @apply-transform" with the image and the affine transform plus rendering-hints, if needed.

Composing affine transformations.

As said at the beginning of the page, matrix multiplication is not commutative, so there are two ways of composing transformations:

  • Concatenate: (concat-transforms a b) means applying a and then b.
  • Preconcatenate: (preconcat-transforms a b) means applying b and then a.

Both methods return another affine transform, and accept as many affine transforms as you want.

Special cases.

Both scale and shear resizes the image in order to get a full image, and not a cropped one. This is done by setting *is-a-resize-safe-transform* to true. Don’t do this unless you are sure of what you’re doing.

Examples

Rotate with only the angle as parameter.

Rotate with three parameters.

Scale with only one parameter.

Scale with two parameters.

Shear with one parameter.

Shear with two parameters.

Translate with one parameter

Translate with two parameters.

For showing the difference between concat-transforms and preconcat-transforms, followin examples applied the same transformations in different order. The first transformation is a rotation of 60º with center the center of the image. The second is a translation of vector (40, 20).

Concat

Preconcat

⚠️ **GitHub.com Fallback** ⚠️