Created
December 16, 2011 18:02
-
-
Save josher19/1487167 to your computer and use it in GitHub Desktop.
Linear Attractor using matrix multiplication. Click inside the square to change x or y attraction values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// matrix.pde | |
// Using matrix multiplication in processingjs for an attractor | |
bool running = true; | |
int sz=250; | |
int dx = sz/2; | |
int dy = sz/2; | |
int sq = 20; | |
int counter = 1; | |
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 setup() { | |
size(sz,sz); | |
frameRate(15); | |
strokeWeight(2); | |
translate(dx,dy); | |
text("click mouse", 0, 0); | |
text(matrixA, -100, 20); | |
} | |
void setAttractorPull() { | |
++counter; | |
if (abs(mouseX-dx) < sq && abs(mouseY-dy) < sq) { | |
// pushMatrix(); | |
// translate(dx,dy); | |
double ystr = (dy - mouseY) * 2 / sq; | |
double xstr = (mouseX - dy) * 2 / sq; | |
if (counter % 2) { | |
matrixB[0][1] = xstr | |
matrixB[1][0] = ystr | |
// background(55); | |
} else ( | |
matrixB[0][0] = xstr; | |
matrixB[1][1] = ystr; | |
} | |
text(matrixB, dx + 2 * sq, dy + 2 * sq) | |
// popMatrix(); | |
} | |
} | |
int lastX = round(random(sz)); | |
int lastY = round(random(sz)); | |
void mousePressed() { | |
setAttractorPull(); | |
matrixA[0][0] = mouseX - dx; | |
matrixA[0][1] = mouseY - dy; | |
matrixA[1][0] = lastX - dx; | |
matrixA[1][1] = lastY - dy; | |
lastX = mouseX; | |
lastY = mouseY; | |
running = false; | |
} | |
void mouseReleased() { | |
// setAttractorPull(); | |
running = true; | |
} | |
void draw() { | |
if (!running) return; | |
background(50); | |
//line(random(width),random(height),random(width),random(height)); | |
pushMatrix(); | |
translate(dx,dy); | |
// center quardant box | |
stroke(255,156,0); | |
strokeWeight(1); | |
fill(50,50,50); | |
rect(-sq,-sq,2*sq,2*sq); | |
rect(-sq/2,-sq/2,sq,sq); | |
fill(155,155,155) | |
stroke(155,156,0); | |
line(-sq, 0, sq, 0); | |
line(0, -sq, 0, sq); | |
// blue | |
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(matrixA, 0,90); | |
text(matrixB, 30, 0); | |
popMatrix(); | |
} | |
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