Why do 2D transformations need 3x3 matrices?
Asked Answered
Q

2

21

I want to do some 2D drawing and thus want to implement some matrix transformations. With my light mathematics background I am trying to understand how to do so in C# (any other oop language would do it obviously).

All I read is explaining that we need to work with 3x3 matrices to be able to cope with the translations. Because you cannot make translation with multiplications. But this is with multiplications of the matrices that we create our transformations. So we work with something like:

{ x1, x2, tx }
{ y1, y2, ty }
{ 0,  0,  1  }

I understand the mean of the third column, but why do we need the third row? In a identity matrix as well as in a rotation, scale or rotation the last row is the same. Are there operations I did not reach yet which will need it? Is it because some languages (Java) performs better with "squared dimensions" arrays? If so I can use 3 columns and 2 rows in C# (since jagged arrays works as well or better).

For example, for a rotation + translation I have a matrix like this

{ cos(rot)*x1, (-sin(rot))*x2, tx }
{ sin(rot)*y1, cos(rot)*y2,    ty }
{ 0,           0,              1  }

No need of the last row.

Quag answered 22/5, 2012 at 9:8 Comment(2)
Start your reading at en.wikipedia.org/wiki/Translation_(geometry), then follow one of the links to en.wikipedia.org/wiki/Homogeneous_coordinatesFugger
@HighPerformanceMark I am here trying to explain which point of these concepts I do not understand. Yes these are the 2 first documents with which I started my reading.Quag
B
36

this is with multiplications of the matrices that we create our transformations

This is why we want square matrices.

Suppose we did what you propose, and used 2x3 matrices for our transformations.

Then a rotation would be

( x1, x2, 0 )
( y1, y2, 0 )

and a translation would be

( 1, 0, tx )
( 0, 1, ty )

and we could perform either rotations or translations by multiplying our matrix by a column vector representing the point:

    ( x )
M   ( y )
    ( 0 )

to get correct answers.

However - how would we go about composing transformations? Indeed, for your "for a rotation + translation I have a matrix like this" example, how did you get to that matrix? Sure, in this case you can just write it out, but in general? Well, you know the answer:

this is with multiplications of the matrices that we create our transformations

So it must be possible to multiply two transformation matrices to give another transformation matrix. And the rules of matrix multiplication show that this:

( . . . ) ( . . . )
( . . . ) ( . . . ) = ???

is not a valid matrix multiplcation. We need matrices that can be multipled in order for our transformations to be composable. So we have that extra row.


Now, the way I've expressed it here is in fact completely backward from the standard mathematical presentation, in which the familiar transformations of rotation and translation are just special cases of the full power of homogeneous coordinate transformations on the projective plane - but I think it will do to show you why we need that extra row - to make the matrix square, and thus able to be multipled with like matrices.

Berkeleianism answered 22/5, 2012 at 12:23 Comment(2)
Uuh that is the point I was not seeing yet! Thanks you AakashM. Thank to have taken the time to spot the hole in my thinking.Quag
And the actual calculation for a translation of (tx,ty) looks like [x y 1] x [1 0 0; 0 1 0; tx ty 1] = [x+tx y+ty 1].Always
L
10

The answer is Homogeneous Coordinates. To combine rotation and translation in one operation one extra dimension is needed than the model requires. For planar things this is 3 components and for spatial things this is 4 components. The operators take 3 components and return 3 components requiring 3x3 matrices.

Lurcher answered 22/5, 2012 at 11:59 Comment(3)
But what is the purpose of the {0,0,1} row in 2D? We can calculate translations as well as linear transformation without it. (If I correctly understood)Quag
As I said, the 3rd row is needed to produce a 3 component outcome. It is possible that the third component is not always equal to 1 and the 3rd row is not [ 0 0 1 ] in other affine transformations.Lurcher
I did not think about the fact that we need this for matrix operations; that's a "mathematical constraint" I did not not foreseen. (Sorry for being slow)Quag

© 2022 - 2024 — McMap. All rights reserved.