Saturday, February 14, 2009

Transformations: Homework 4

Here is an image from the first part of Homework 4, rotating a triangle:


The Code:
int x1, x2, x3, y1, y2, y3;
Point p1, p2, p3, temp1, temp2, temp3, centroid;
float a,theta;

void setup()
{
size(720, 480);
background(0);
stroke(255);
p1=new Point(0, 0);
p2=new Point(0 ,0);
p3=new Point(0 ,0);
temp1=new Point(0, 0);
temp2=new Point(0 ,0);
temp3=new Point(0 ,0);
centroid=new Point(0 ,0);
a=90;
mousePressed();
}

void mousePressed()
{
background(0);
x1=int(random(720));
x2=int(random(720));
x3=int(random(720));
y1=int(random(480));
y2=int(random(480));
y3=int(random(480));

p1=new Point(x1,y1);
p2=new Point(x2 ,y2);
p3=new Point(x3 ,y3);

centroid = new Point((p1.x+p2.x+p3.x)/3,(p1.y+p2.y+p3.y)/3);

//move all points to the origin TEMPORARLILY
temp1=new Point(p1.x-centroid.x, p1.y-centroid.y);
temp2=new Point(p2.x-centroid.x, p2.y-centroid.y);
temp3=new Point(p3.x-centroid.x, p3.y-centroid.y);

}

void draw()
{
background(0);
frameRate(60);
stroke(255);
a = (mouseX / (float) width) * 360f;
// Convert it to radians
theta = radians(a);

//rotate the temporary points around the origin
p1=rotatePoint(temp1,theta);
p2=rotatePoint(temp2,theta);
p3=rotatePoint(temp3,theta);

//now move them back to OG location
p1=new Point(p1.x+centroid.x, p1.y+centroid.y);
p2=new Point(p2.x+centroid.x, p2.y+centroid.y);
p3=new Point(p3.x+centroid.x, p3.y+centroid.y);

//draw it
point(centroid.x, centroid.y);
myTri(p1,p2,p3);
}


void myTri(Point a, Point b, Point c)
{
line(a.x,a.y,b.x,b.y);
line(b.x,b.y,c.x,c.y);
line(c.x,c.y,a.x,a.y);
}


Point rotatePoint(Point p, float angle)
{
float[] coord = new float[3];
int[] newCoord= new int[3];
float[][] matrix= new float[3][3];
float c=cos(angle);
float s=sin(angle);
Point myPoint=new Point(0,0);

//setup the 3x1 matrix for the point
coord[0]=p.x;
coord[1]=p.y;
coord[2]=1;

//setup the 3x3 matrix for the rotation
matrix[0][0]=c;
matrix[0][1]=s;
matrix[0][2]=0;
matrix[1][0]=-s;
matrix[1][1]=c;
matrix[1][2]=0;
matrix[2][0]=0;
matrix[2][1]=0;
matrix[2][2]=1;

newCoord=matrixMult(matrix, coord);
myPoint.x=newCoord[0];
myPoint.y=newCoord[1];
return myPoint;

}

int[] matrixMult(float matA[][], float matB[])
{
float[] temp= new float[3];
int[] ntemp= new int[3];
temp[0]=0;
temp[1]=0;
temp[2]=0;
for(int i=0; i<3; j="0;" ntemp="int(temp);">

Here are some interesting images I made by rotating several randomly colored triangles, while changing the brightness each time the draw function ran:






The Code:
int x1, x2, x3, y1, y2, y3;
final int num=5;
Point[] p1=new Point[num];
Point[] p2=new Point[num];
Point[] p3=new Point[num];
Point[] temp1=new Point[num];
Point[] temp2=new Point[num];
Point[] temp3=new Point[num];
Point[] centroid=new Point[num];
int h=0;
int s=0;
int v=0;
color c=color(h, s, v);

float a,theta;

void setup()
{
size(720, 480);
background(0);
stroke(255);
colorMode(HSB, 255);
for(int i=0; i255)
v=0;
color c= color(h, s, v);
stroke(c);
point(centroid[i].x, centroid[i].y);
myTri(p1[i],p2[i],p3[i]);
}
}


void myTri(Point a, Point b, Point c)
{
line(a.x,a.y,b.x,b.y);
line(b.x,b.y,c.x,c.y);
line(c.x,c.y,a.x,a.y);
}


Point rotatePoint(Point p, float angle)
{
float[] coord = new float[3];
int[] newCoord= new int[3];
float[][] matrix= new float[3][3];
float c=cos(angle);
float s=sin(angle);
Point myPoint=new Point(0,0);

//setup the 3x1 matrix for the point
coord[0]=p.x;
coord[1]=p.y;
coord[2]=1;

//setup the 3x3 matrix for the rotation
matrix[0][0]=c;
matrix[0][1]=s;
matrix[0][2]=0;
matrix[1][0]=-s;
matrix[1][1]=c;
matrix[1][2]=0;
matrix[2][0]=0;
matrix[2][1]=0;
matrix[2][2]=1;

newCoord=matrixMult(matrix, coord);
myPoint.x=newCoord[0];
myPoint.y=newCoord[1];
return myPoint;

}

int[] matrixMult(float matA[][], float matB[])
{
float[] temp= new float[3];
int[] ntemp= new int[3];
temp[0]=0;
temp[1]=0;
temp[2]=0;
for(int i=0; i<3; j="0;" ntemp="int(temp);">


Finally here is another image I made using pushMatrix() and popMatrix();
They especially made it easy to return to the middle of the screen after each iteration of a loop:




The Code:

int h=0;
int s=0;
int v=0;
color c=color(h, s, v);
int scal=0;
int rot=0;

void setup(){
size(720, 480);
background(0,0,0);
colorMode(HSB, 255);
noLoop();
}

void draw(){
//just draw colored rectangles in each quadrant
randomColor();
rect(0,0,width/2, height/2);
randomColor();
rect(width/2, height/2,width/2, height/2);
randomColor();
rect(0,height/2,width/2, height/2);
randomColor();
rect(width/2,0,width/2, height/2);

//push Matrix, then move to middle of screen
pushMatrix();

translate(width/2, height/2);

for(int j=0; j<4; i="10;" j="0;" i="10;" h="int(random(200));" s="int(random(250));" v="int(random(255));" c="color(h,s,v);">

No comments:

Post a Comment