Monday, February 16, 2009

Homework #4


For this program, I already had a MyMatrix class for Scientific Computing that I made that could handle the matrix multiplication, so I used that.
import processing.core.*;
public class Homework4 extends PApplet{
int x0=100;
int y0=100;
int x1=200;
int y1=175;
int x2=330;
int y2=175;
double rotDeg = 20;
MyMatrix test;

public void setup(){
size(410,410);
background(255);
stroke(0,0,0);
//triangle(x0,y0,x1,y1,x2,y2);
//myRotate(rotDeg);
//triangle(x0,y0,x1,y1,x2,y2);

for(int i = 0; i < 18; i++){
triangle(x0,y0,x1,y1,x2,y2);
myRotate(rotDeg);
}
}
public int[] centrePoint(int cx0, int cy0, int cx1, int cy1, int cx2, int cy2){
int[] result = new int[2];

result[0] = Math.round((1f/3f)*(x0+x1+x2));

result[1] = Math.round((1f/3f)*(y0+y1+y2));
return result;
}

public void draw(){

}

public void myRotate(double theta){

theta = (theta/360.)*(2*Math.PI);

float[][] m = new float[2][2];
int [] cp = centrePoint(x0,y0,x1,y1,x2,y2);
m[0][0] = Float.parseFloat((Double.toString(Math.cos(theta))));
m[0][1] = Float.parseFloat((Double.toString(Math.sin(theta))));
m[1][1] = Float.parseFloat((Double.toString(Math.cos(theta))));
m[1][0] = Float.parseFloat((Double.toString(-1.0*Math.sin(theta))));



MyMatrix rotMat = new MyMatrix(m.clone());
//rotMat.print();
MyMatrix vector;
MyMatrix temp;
float [][] arr = new float[2][1];
//p0
arr[0][0] = x0-cp[0];
arr[1][0] = y0-cp[1];

vector = new MyMatrix(arr.clone());
temp = new MyMatrix(rotMat.multiply(vector).theMatrix.clone());

x0 = Math.round(temp.theMatrix[0][0])+cp[0];
y0 = Math.round(temp.theMatrix[1][0])+cp[1];
//System.out.println(x0+","+y0);
//p1
arr[0][0] = x1-cp[0];
arr[1][0] = y1-cp[1];

vector = new MyMatrix(arr.clone());
temp = new MyMatrix(rotMat.multiply(vector).theMatrix.clone());

x1 = Math.round(temp.theMatrix[0][0])+cp[0];
y1 = Math.round(temp.theMatrix[1][0])+cp[1];
//System.out.println(x1+","+y1);
//p2
arr[0][0] = x2-cp[0];
arr[1][0] = y2-cp[1];

vector = new MyMatrix(arr.clone());
temp = new MyMatrix(rotMat.multiply(vector).theMatrix.clone());

x2 = Math.round(temp.theMatrix[0][0])+cp[0];
y2 = Math.round(temp.theMatrix[1][0])+cp[1];
//System.out.println(x2+","+y2);

}

}

No comments:

Post a Comment