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
If you want to access a element of a matrix, the notation A[m,n] where A is the matrix m is the row and n is the column. So if we call the last matrix B, then B[0,1] is equal to 13.
To add or subtract matrices, they must be the same dimension.
example
43-1+209=63-8
0137-34-1-3176

You can also multiply by a matrix by a scalar.
example
3*43-1=129-3
013703921

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

Heres the algorithm:

  1. For each row in the first matrix:
    Multiply that row by each column in the second matrix, element by element. Sum up the result.
  2. 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.
example: I'm highlighting the the stuff being multiplied, in grey.
124*4143 =    
2 6 0 0-13 1  26 
  275 2 
(2*4) + (6*3) + (0*5) = 26
1 2 4 *4143 =   13
2600-131   26 
  2752  
(1*3) + (2*1) + (4*2) = 13
The identity matrix is shown:
100
010
001
Its dimension is always mxm. The identity matrix times any matrix A, will always equal matrix A. Go ahead and try to work it out on paper.

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
Okay heres the matrix
10Tx
01Ty
001

Rotation

Note the degree is in radians!
cos-sin0
sincos0
001

3D rotation parallel to the x-axis
1000
0cossin0
0-sincos0
0001

3D rotation parallel to the y-axis
cos0-sin0
0100
sin0cos0
0001

3D rotation parallel to the z-axis
cossin00
-sincos00
0010
0001

Scaling

Remember take away the z row if you're in two dimenisions
s000
0s00
00s0
0001

Scaling each to a different scale
sx000
0sy00
00sz0
0001