What Is Transformation Matrix and How To Use It
What Is Transformation Matrix and How To Use It
Patagames Software Support Forum » Rules » Articles » What Is Transformation Matrix and How to Use It
What Is Transformation Matrix and How to Use It - This article provides explanation of
what is a transformation matrix and why it works like it does.
Paul Rayman
Posted : 5 years ago
This article provides deeper explanation of what is a transformation matrix and why it
works like it does.
Introduction to matrices
If you are very new to linear algebra, matrices are simply set of values grouped together
into a rectangular or square form. Here are examples of matrices:
Many physical values can be (and often are) represented in the matrix form. For instance,
coordinates; they are often represented as a vector – a matrix with one column and the
number of rows for each spatial dimension (2 rows for 2D, 3 rows for 3D and so on). Each
element in such a matrix is a coordinate on the corresponding axis. The example of a
vector is shown above.
PDF documents also use a coordinate system. The two axes are vertical and horizontal
dimensions of a page with each coordinate representing the number of PDF Points on that
axis. So, if a given object on the page is placed at 3500 Points from the Bottom and 100
Points from the Left, its coordinate vector is:
:
which is essentially the same.
Matrices can be summed, subtracted, multiplied and divided (inverted) much like ordinary
numbers. Addition and subtraction are the easiest operations, here are examples:
Addition
To add one matrix to another you should simply add each element of the first matrix to the
corresponding element of the second matrix. Matrices must be of equal dimensions, of
course.
Subtraction
Same thing goes for subtraction except for the arithmetic sign:
Addition and subtraction are commutative. You can change the order of matrices and still
get the same result (except for the arithmetic sign in case of subtraction):
A+B=B+A
A - B = -(B - A)
Multiplication
At first, we multiply the first row of the first matrix to the first column of the second matrix.
This gives us the element at (1,1) of the resulting matrix:
Then, we multiply the second row and the first column to calculate the element at (1,2):
:
Then, we calculate the element at (2,1):
1. The number of columns of A must match the number of rows of B. You can multiple 2-
by-2 and 2-by-2 matrices as shown above. You can also multiple say 2-by-6 to 4-by-2
(results in a 6-by-4 matrix) or 3-by-3 to 1-by-3 (results in a 3-by-3 matrix). But you
cannot multiply 3-by-3 to 1-by-2, for example.
2. Multiplication of matrices is NOT commutative. That is . If you take the above
example and change the order of matrices the result will be different, namely:
Division
If you thought multiplying of matrices was hard to understand, we have bad news for you:
dividing of them is even trickier. However, there is also good news: you don’t need to
divide matrices to perform transformation of PDF objects, so we simply leave this Wikipedia
link JFYI: https://en.wikipedia.org/wiki/Invertible_matrix
For example, to rotate an image on the bitmap of a page, the PDF renderer should take
coordinates of each point of the image, alter them using the above formula and render that
pixel at the new coordinates.
That is what you do when you call the SetMatrix function: you tell the renderer how it
should transform each rendered pixel of the object.
Transformation operations
Translate
The translation operation moves the coordinate system by the given offset. The operation
results in a new coordinate system that is moved by e along the x axis and by f along the y
axis from the original coordinate system.
Example:
Coordinates of a point in the original coordinate system are (240 651 1). We want to
translate the coordinate system by 10 points left and 20 points up. The required
transformation matrix is:
As you see, the coordinates changed just like planned. The same way all other pixels of the
image are transformed.
where a and d are respectively horizontal and vertical scaling factors. If they are greater
than 1, the object is scaled up. If they are less than 1, the object is scaled down from its
original size.
Example:
This matrix will scale the object up by 40% along the x axis and down by 20% along the y
axis.
Code:
1 SetMatrix(1.4, 0, 0, 0.8, 0, 0)
:
Flip/Reflect
This operation is similar to scaling. We simply need to invert one of the coordinates for
horizontal/vertical flip or both of them to reflect about origin.
Horizontal flip:
Code:
1 SetMatrix(-1, 0, 0, 1, 0, 0)
Vertical flip:
Code:
1 SetMatrix(1, 0, 0, -1, 0, 0)
Central flip:
Code:
1 SetMatrix(-1, 0, 0, -1, 0, 0)
:
Rotate
The rotation operation rotates the original coordinate system clockwise or counterclockwise
for the given angle.
The form of the matrix comes from simple trigonometry, but the formal proof goes beyond
the scope of this article.
Let’s say we want to rotate an object by 30 degrees clockwise. Then, each pixel of the
object is transformed as follows:
Code:
1 SetMatrix(1, 0, 20, 1, 0, 0)
1 SetMatrix(1, -45, 0, 1, 0, 0)
Multiple transformations
What if you need to apply several transformations to the same object? Like, scale up and
rotate? Or flip horizontally for a mirrored image and translate it to the desired position on
the page?
In this case you simply need several transformation matrices. To perform a series of
transformations, matrices of individual transformations are multiplied together. However,
as mentioned above, the multiplication operation is not commutative, hence the order of
transformation matrices makes a substantial difference.
For example, if you want to rotate the object, and then translate it, the resulting
transformation matrix is:
For the reversed order of transformation matrices the resulting matrix is different though:
:
It may seem unobvious for the first time. You can move an object by 10 points and then
rotate it 45 degrees. Or you can rotate it 45 degrees and move the rotated object by 10
points then. Why the difference? Because the transformation matrix does not rotate
(translate, scale etc.) the object, it rotates the coordinate system containing the object.
Think of it this way. Put a pencil on the table. When you transform the object, the sequence
of transformations would be “rotate the pencil 45 degrees, then move it left by 10 cm”. But
when you transform the coordinate system, you get “rotate the table 45 degrees, then
move the table left by 10 cm”.
Conclusion
Now you know what these a, b, c, d, e and f coefficients in the Matrix class mean. The
transformation matrix composed of these elements allows to adjust any PDF bitmap or
other object almost arbitrary.