Transformations
A fast way to do transformations or mathematical operations is to use Matrices. I'm not sure of everyone's math level, so I'll will explain and put up the code to do matrix multiplication.
Matrix A is made of rows and columns.
example: Matrix A's dimension is 3x3, 3 rows and 3 columns. Remember, rows go
horizontally and columns go vertical.
| 6 26 -7 |
A= | 0 3 1 |
| 11 -3 5 |
ex 2: I'm going to use tables to make the rest of the matrices and not the usual lines on the sides, as in math books. Same thing, it's just faster for me to type. As you can see this is a 2x3 matrix.
| 4 | 3 | -1 |
| 0 | 13 | 7 |
To add or subtract matrices, they must be the same dimension.
example
| 4 | 3 | -1 | + | 2 | 0 | 9 | = | 6 | 3 | -8 |
| 0 | 13 | 7 | -3 | 4 | -1 | -3 | 17 | 6 |
You can also multiply by a matrix by a scalar.
example
| 3 | * | 4 | 3 | -1 | = | 12 | 9 | -3 |
| 0 | 13 | 7 | 0 | 39 | 21 |
Okay, now Matrix Multiplication. I suggest you get a piece of paper and write down the matrices and work it out and see if you come up with the same answer.
RULES
- Matrix multiplication is not commutative: A*B is not equal to B*A.
- The number of columns in the first matrix n must be equal to the number of rows in the second matrix (also) n. This means that if one matrix has a dimension of mxn, then the other matrix must be mxr. The dimensions m and r can be anything. The result will produce a matrix with a mxr dimension.
Heres the algorithm:
- For each row in the first matrix:
Multiply that row by each column in the second matrix, element by element. Sum up the result. - Place the result into postion i,j of the results matrix, where i is the row of the first matrix being processed and j is the column of the second matrix being processed.
| 1 | 2 | 4 | * | 4 | 1 | 4 | 3 | = | ||||
| 2 | 6 | 0 | 0 | -1 | 3 | 1 | 26 | |||||
| 2 | 7 | 5 | 2 | |||||||||
| 1 | 2 | 4 | * | 4 | 1 | 4 | 3 | = | 13 | |||
| 2 | 6 | 0 | 0 | -1 | 3 | 1 | 26 | |||||
| 2 | 7 | 5 | 2 | |||||||||
The identity matrix is shown:
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
Matrix Multiplication
//heres the code, you may need to change it for 2D tranformations
//matrix multiplication
void MatrixMult(double r[4][4], const double m1[4][4], const double m2[4][4])
{
for (int i=0; i<4; ++i)
for (int j=0; j<4; ++j)
{r[i][j]=0;
for (k=0; k<4; ++k)
r[i][j]+=m1[i][k]*[m2[k][j];
}
}
Translation
Keep in mind to use a matrix with your variables x,y (and z, if in 3D) and 1.example: for a 2D translation
| x-value |
| y-value |
| 1 |
| 1 | 0 | Tx |
| 0 | 1 | Ty |
| 0 | 0 | 1 |
Rotation
Note the degree is in radians!| cos | -sin | 0 |
| sin | cos | 0 |
| 0 | 0 | 1 |
3D rotation parallel to the x-axis
| 1 | 0 | 0 | 0 |
| 0 | cos | sin | 0 |
| 0 | -sin | cos | 0 |
| 0 | 0 | 0 | 1 |
3D rotation parallel to the y-axis
| cos | 0 | -sin | 0 |
| 0 | 1 | 0 | 0 |
| sin | 0 | cos | 0 |
| 0 | 0 | 0 | 1 |
3D rotation parallel to the z-axis
| cos | sin | 0 | 0 |
| -sin | cos | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 |
Scaling
Remember take away the z row if you're in two dimenisions| s | 0 | 0 | 0 |
| 0 | s | 0 | 0 |
| 0 | 0 | s | 0 |
| 0 | 0 | 0 | 1 |
Scaling each to a different scale
| sx | 0 | 0 | 0 |
| 0 | sy | 0 | 0 |
| 0 | 0 | sz | 0 |
| 0 | 0 | 0 | 1 |