Rotation

Rotation matrix

In a cartesian coordinates system, the rotation matrix is a matrix whose coefficents are computed with a given angle, the angle rotation.

In this system, the rotation center is the origin, the point O(0,0).

Such a rotation around a point C(x0,y0) uses the same matrix and a translation vector:

 

Rounding errors

It is possible to use the mathematical expression described above but , as the use of fixed point operations introduces rounding errors, some of the pixels of the destination image could not be set. The idea is then to compute, for each pixel of the destination image, the pixel coordinates of the original image that matches with. So, it seems better to go from the destination to the source and in this case to test if the computed coordinates are inside the source image.

Going from destination to source may also introduce unwanted behaviors such as pixel overlappings. Always due to the rouding errors, it is possible that one destination pixel matches with two source pixels. In this case the corresponding colors will be overlayed one over the other and that may impact on the global rendering quality.

From destination to source

The rotation matrix M introduced above is invertible (nonsingular) as its determinant is not equal to zero.

that means it exists a matrix M-1 such as M-1M=MM-1=I2

Such a matrix M-1 is used to compute source pixel coordinates knowing the destination pixel coordinates. The inverse matrix and the relation between the coordinates of the pixels of destination and those of the source is described below:

Intuitively, we have a rotation of an angle from the source to the destination, therefore the inverse matrix corresponds to a rotation of an angle - from the destination to the source.

Finally, the expression of the source pixel coordinates, for given destination pixel coordinates, is given by:

This is the formula used in the pno code, by taking care that the computed coordinates are inside the source image, otherwise the pixel color is set to the background color.