Skip to content

Instantly share code, notes, and snippets.

@josher19
Created December 1, 2011 20:25
Show Gist options
  • Save josher19/1419575 to your computer and use it in GitHub Desktop.
Save josher19/1419575 to your computer and use it in GitHub Desktop.
Using matrix multiplication in processingjs for an attractor
// matrix.pde
// Using matrix multiplication in processingjs for an attractor
void setup() {
size(250,250);
frameRate(20);
strokeWeight(2);
translate(125,125);
text("click mouse", 0, 0);
text(matrixA, -100, 20);
}
void mousePressed() {
background(50);
//line(random(width),random(height),random(width),random(height));
pushMatrix();
translate(125,125);
// center line
stroke(255,156,0);
strokeWeight(1);
line(-4, 0, 4, 0);
line(0, -4, 0, 4);
stroke(0,156,255);
strokeWeight(2);
line(matrixC[0][0],matrixC[0][1],matrixC[1][0],matrixC[1][1]);
text(matrixA, 0,70); // width/2,height/2);
multiply(matrixA,matrixB,matrixC); // C = A*B
//text(matrixA, 0,60);
//arrayCopy(matrixC,matrixA);
//arrayCopy(matrixC,matrixA); // A = C
//matrixC = matrixA.reverse().reverse();
multiply(matrixC,matrixId,matrixA); // A = C*Id = C
text(matrixC, 0,90);
popMatrix();
}
int[][] matrixId = {{1,0},{0,1}};
int[][] matrixA = {{round(random(width)/2),round(random(height)/2)},{round(random(width)/2),random(height)/2}};
int[][] matrixB= {{0,-0.9},{1.0,0}};
int[][] matrixC = {{0,0},{0,0}}; // new int[2][2];
void multiply(matrixA,matrixB,matrixC)
{
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
matrixC[i][j] = 0;
for(int k=0; k<2; k++){
matrixC[i][j]= matrixC[i][j]+ matrixA[i][k] * matrixB[k][j];
}
matrixC[i][j] = round(matrixC[i][j]*100)/100;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment