Wednesday, February 18, 2009

Homework 4

Part one: Triangle Rotation



Part two: Rotating a triangle more than once to create a cool picture



Part three: Interesting rotating square







float[][] matrixRotation = new float[2][2];
int x1,y1,x2,y2,x3,y3;
int originX, originY;
float angle, cosine, sine, jitter;

void setup(){
size(720,480);
background(0);
//noLoop();
//noFill();
smooth();

}



void draw(){

fill(random(255),random(255),random(255));
stroke(247,26,141);


crazyBox();
pushMatrix();
popMatrix();

//
//x1 = int(random(520));
//y1 = int(random(360));
//x2 = int(random(520));
//y2 = int(random(360));
//x3 = int(random(520));
//y3 = int(random(360));

//code to rotate the triangle many times to create a cool picture
//for(float a=0; a < 2; a += .067){
//triangleRotation(x1,y1,x2,y2,x3,y3,(a*PI));
//}

//code to rotate the triangle once
//for(float f=0; f < 2; f+=1){
//triangleRotation(x1,y1,x2,y2,x3,y3,(f*PI));
//stroke(255,255,0);
//}



}

void crazyBox() {

fill(random(255),random(255),random(255));
stroke(247,26,141);
stroke(0);
if(second()%2 == 0) {
jitter = (random(-0.20, 1));
}
angle = angle + jitter;
cosine = cos(angle);
sine = sin(angle);

translate(120, 200);
rotate(cosine);
rectMode(CENTER);
rect(10,10,150,150);

}
void anotherCrazyBox() {



}

void rotateTheMatrix(float p){
matrixRotation[0][0] = cos(p);
matrixRotation[0][1] = -sin(p);
matrixRotation[1][0] = sin(p);
matrixRotation[1][1] = cos(p);
}


void triangleRotation(int x0, int y0, int x1, int y1, int x2, int y2, float p){

rotateTheMatrix(p);
originX = (x0+x1+x2)/3;
originY = (y0+y1+y2)/3;

float[][] theOrigin = {{x0-originX,y0-originY},{x1-originX,y1-originY},{x2-originX,y2-originY}};
theOrigin = multiplyMatrices(theOrigin,matrixRotation);

for(int a=0; a < theOrigin.length; a++){
for(int b=0; b < theOrigin[0].length; b++){
if(b==0){
theOrigin[a][b] += originX;
}else
theOrigin[a][b] += originY;
}
}
triangle(theOrigin[0][0],theOrigin[0][1],theOrigin[1][0],theOrigin[1][1],theOrigin[2][0],theOrigin[2][1]);
}



float[][] multiplyMatrices(float[][] matrix1, float[][] matrix2){
float[][] matrix3 = new float[matrix1.length][matrix2[0].length];
for(int i=0; i < matrix1.length; i++) {
for(int j=0; j < matrix2[0].length; j++) {
for(int k=0; k < matrix1[0].length; k++) {
matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return matrix3;
}

void drawTriangle(int x0, int y0, int x1, int y1, int x2, int y2){
line(x0,y0,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x0,y0);
}

No comments:

Post a Comment