float angle = (PI*.05) ;
float original [][] = {{500, 300}, {300, 5},{200,300}};
float rotScaler [][] = {{cos(angle),sin(angle)}, {-sin(angle),cos(angle)}};
float translator [][] ={{20,20},{20,20},{20,20}};
float translated [][];
float rotated [][];
void setup() {
size(700, 700);
background(0);
println("Original Coords");
drawMatrix(original); // just writes out the coods of the matrix
triangle(original[0][0], original[0][1], original[1][0], original[1][1],original[2][0],original[2][1]); // draw original triangle
println("Translated Coords");
float moved [][] = translateShape(original, translator ); // draws first translated triangle
for (int i=0; i < 10; i++) { //draws 9 more triangles
fill(random(0,255),random(0,255), random(0,255)); // makes a new random color for each triangle
moved = translateShape(moved, translator); // draws a new translated triangle
}
println("Rotated Coords");
rotateShape(moved,rotScaler); //draws rotated triangle
} // end setup
void draw() {
}
float [][] translateShape (float [][] A , float [][] B){ // translate function
float translated [][] = matrixAdd(A, B);
triangle(translated[0][0], translated[0][1], translated[1][0], translated[1][1],translated[2][0],translated[2][1]); // draws translated triangle
drawMatrix(translated); // writes out coordinats
return translated;
}
float [][] rotateShape (float [][] A , float [][] B){ //rotate function
fill(255,0,0);
float rotated [][] = matrixMul(A, B);
triangle(rotated[0][0], rotated[0][1], rotated[1][0], rotated[1][1],rotated[2][0],rotated[2][1]); // draws rotated triangle
drawMatrix(rotated); //writes out coordinates
return rotated;
}
float[][] matrixMul(float[][] A, float [][] B) { // matrix multiplication function
float[][] product = new float[A.length][B[0].length];
for(int row = 0; row < A.length; row++) {
for(int col = 0; col < B[0].length; col++) {
for(int i = 0; i < B.length; i++){
product[row][col] += A[row][i]*B[i][col];
}
}
}
return product;
} //end matrixMul
float[][] matrixAdd(float[][] A, float [][] B) { // matrix addition function
int row = 0;
int col = 0;
float [][]addSum = new float[A.length][A[row].length];
for(row = 0; row < A.length; row++) {
for(col = 0; col < A[row].length; col++) {
addSum[row][col]+=(A[row][col]+B[row][col]);
}
}
return addSum;
}//end matrix add function
void drawMatrix(float[][] A) { // just prints out the coords of matrices
for(int row = 0; row < A.length; row++) {
for(int col = 0; col < A[row].length; col++) {
print(" "+ A[row][col]);
}
println();
}
}//end drawMulMatrix1