Wednesday, February 18, 2009


























The tunnel Link Just hit 'r' and it will take off


float i=0;
float k=0;
int x0 = 300;
int y0 = 300;
int x1 = 400;
int y1 = 100 ;
int x2 = 500 ;
int y2 = 300 ;

void setup() {
size(700, 700);
background(0);

} // end setup

void draw() {
frameRate(1999999999);
fill(random(0,255),random(0,255), random(0,255));
keyPressed();
}

void keyPressed(){
if (key == 'r'){
i -=.17;
k +=1;
translate(width/2,height/2);
rotate(PI*i);
triangle(x0-k,y0-k,x0-k,y1-k,0,0);
}
}

Tuesday, February 17, 2009


float angle = (PI*.05) ;
float original [][] = {{500, 300}, {300, 5},{200,300}};
float rotScaler [][] = {{cos(angle),sin(angle)}, {-sin(angle),cos(angle)}};
float translator [][] ={{20,20},{20,20},{20,20}};
float translated [][];
float rotated [][];
int x0 = 500 ;
int x1 = 300 ;
int x2 = 300 ;
int y0 = 5 ;
int y1 = 200 ;
int y2 = 300 ;

void setup() {
size(700, 700);
background(0);

println("Original Coords");
drawMatrix(original); // just writes out the coods of the matrix
triangle(original[0][0], original[0][1], original[1][0], original[1][1],original[2][0],original[2][1]); // draw original triangle

println("Translated Coords");
float moved [][] = translateShape(original, translator ); // draws first translated triangle
for (int i=0; i < 10; i++) { //draws 9 more triangles
fill(random(0,255),random(0,255), random(0,255)); // makes a new random color for each triangle
moved = translateShape(moved, translator); // draws a new translated triangle
}

println("Rotated Coords");
rotateShape(moved,rotScaler); //draws rotated triangle
} // end setup

void draw() {
fill(random(0,255),random(0,255), random(0,255));
triangle(x0,y0,x1,y1,x2,y2); // does translate with wasd keys

}


float [][] translateShape (float [][] A , float [][] B){ // translate function
float translated [][] = matrixAdd(A, B);
triangle(translated[0][0], translated[0][1], translated[1][0], translated[1][1],translated[2][0],translated[2][1]); // draws translated triangle
drawMatrix(translated); // writes out coordinats
return translated;
}

float [][] rotateShape (float [][] A , float [][] B){ //rotate function
fill(255,0,0);
float rotated [][] = matrixMul(A, B);
triangle(rotated[0][0], rotated[0][1], rotated[1][0], rotated[1][1],rotated[2][0],rotated[2][1]); // draws rotated triangle
drawMatrix(rotated); //writes out coordinates
return rotated;
}


float[][] matrixMul(float[][] A, float [][] B) { // matrix multiplication function
float[][] product = new float[A.length][B[0].length];
for(int row = 0; row < A.length; row++) {
for(int col = 0; col < B[0].length; col++) {
for(int i = 0; i < B.length; i++){

product[row][col] += A[row][i]*B[i][col];
}
}
}
return product;
} //end matrixMul


float[][] matrixAdd(float[][] A, float [][] B) { // matrix addition function
int row = 0;
int col = 0;
float [][]addSum = new float[A.length][A[row].length];
for(row = 0; row < A.length; row++) {
for(col = 0; col < A[row].length; col++) {
addSum[row][col]+=(A[row][col]+B[row][col]);
}
}
return addSum;
}//end matrix add function


void drawMatrix(float[][] A) { // just prints out the coords of matrices
for(int row = 0; row < A.length; row++) {
for(int col = 0; col < A[row].length; col++) {
print(" "+ A[row][col]);
}
println();
}
}//end drawMulMatrix1



void keyPressed(){
if (key=='d'){
x0+=10 ;
x1+=10 ;
x2+=10 ;
}

if (key=='a'){
x0-=10 ;
x1-=10 ;
x2-=10 ;
}


if (key=='s'){
y0+=10 ;
y1+=10 ;
y2+=10 ;
}

if (key=='w'){
y0-=10 ;
y1-=10 ;
y2-=10 ;
}
}

Rotating and Translating functions


float angle = (PI*.05) ;
float original [][] = {{500, 300}, {300, 5},{200,300}};
float rotScaler [][] = {{cos(angle),sin(angle)}, {-sin(angle),cos(angle)}};
float translator [][] ={{20,20},{20,20},{20,20}};
float translated [][];
float rotated [][];

void setup() {
size(700, 700);
background(0);

println("Original Coords");
drawMatrix(original); // just writes out the coods of the matrix
triangle(original[0][0], original[0][1], original[1][0], original[1][1],original[2][0],original[2][1]); // draw original triangle

println("Translated Coords");
float moved [][] = translateShape(original, translator ); // draws first translated triangle
for (int i=0; i < 10; i++) { //draws 9 more triangles
fill(random(0,255),random(0,255), random(0,255)); // makes a new random color for each triangle
moved = translateShape(moved, translator); // draws a new translated triangle
}

println("Rotated Coords");
rotateShape(moved,rotScaler); //draws rotated triangle
} // end setup

void draw() {
}


float [][] translateShape (float [][] A , float [][] B){ // translate function
float translated [][] = matrixAdd(A, B);
triangle(translated[0][0], translated[0][1], translated[1][0], translated[1][1],translated[2][0],translated[2][1]); // draws translated triangle
drawMatrix(translated); // writes out coordinats
return translated;
}

float [][] rotateShape (float [][] A , float [][] B){ //rotate function
fill(255,0,0);
float rotated [][] = matrixMul(A, B);
triangle(rotated[0][0], rotated[0][1], rotated[1][0], rotated[1][1],rotated[2][0],rotated[2][1]); // draws rotated triangle
drawMatrix(rotated); //writes out coordinates
return rotated;
}


float[][] matrixMul(float[][] A, float [][] B) { // matrix multiplication function
float[][] product = new float[A.length][B[0].length];
for(int row = 0; row < A.length; row++) {
for(int col = 0; col < B[0].length; col++) {
for(int i = 0; i < B.length; i++){

product[row][col] += A[row][i]*B[i][col];
}
}
}
return product;
} //end matrixMul


float[][] matrixAdd(float[][] A, float [][] B) { // matrix addition function
int row = 0;
int col = 0;
float [][]addSum = new float[A.length][A[row].length];
for(row = 0; row < A.length; row++) {
for(col = 0; col < A[row].length; col++) {
addSum[row][col]+=(A[row][col]+B[row][col]);
}
}
return addSum;
}//end matrix add function


void drawMatrix(float[][] A) { // just prints out the coords of matrices
for(int row = 0; row < A.length; row++) {
for(int col = 0; col < A[row].length; col++) {
print(" "+ A[row][col]);
}
println();
}
}//end drawMulMatrix1




Warning Seizure maker using transform


Seizure Maker






void setup() {
size(800, 800);

smooth();
fill(95);


}

void draw() {
background(random(0,255));
fill(random(0,255),random(0,255), random(0,255));
checkerboard();


}

void checkerboard(){
rect(0,0,100,100);
for (int row = 0; row<8; row++){
for (int col=0; col<4; col++){
translate(200, 0);
rect(0,0,100,100);
}
if(row==0 || row==4 || row==6){
translate(-700, 100);
rect(0,0,100,100);
}
else{
translate(-900, 100);
rect(0,0,100,100);
}

}

}

Monday, February 9, 2009

Link to HW4 program and instructions

Hw4 With opening this link you can access my applet and run it. Hitting the L key will draw a triangle with line clipping. Hitting the T key will draw triangles without clipping. And four mouse clicks will draw a bezier curve. All shape colors are random so you can see the difference between them

Hw4
























int xMax=720;
int yMax=480;
int xclipMax=540;
int yclipMax=360;
int xclipMin=180;
int yclipMin=120;
int top=0;
int bottom = 0;
int right = 0;
int left = 0;
//1001 1000 1010
//0001 0000 0010
//0101 0100 0110



int coord;
int x0, x1,x2, y0,y1,y2,x3,y3;
void setup() {
size(720, 480);
background(0);

smooth();

}

void draw() {

noFill();
stroke(random(0,255),random(0,255), random(0,255));
line(180,0,180, height);
line(540,0,540, height);
line(0,120,width, 120);
line(0,360,width, 360);
stroke(255,0, 0);
rect(180,120,360,240);
stroke(random(0,255),random(0,255), random(0,255)); // draws shapes in random colors to see the diffrent shapes.


}
void keyPressed(){
if (key=='t'){ // draws a triangle using the given triangle function
triangle(random(xMax),random(yMax),random(xMax),random(yMax),random(xMax),random(yMax));
}
if (key=='l'){ // draws a triangle with created function and clips the lines
drawTriangle();
}

}
void mousePressed(){
println("x " +mouseX+" Y "+ mouseY); // test for coordinates

if (coord==0){
x0 = mouseX;
y0 = mouseY;
point(x0,y0);
println("x " +mouseX+" Y "+ mouseY);
coord =1;
}

else if (coord==1){
x1 = mouseX;
y1 = mouseY;
println("x " +mouseX+" Y "+ mouseY);
coord =2;
}

else if (coord==2){
x2 = mouseX;
y2 = mouseY;
println("x " +mouseX+" Y "+ mouseY);
coord =3;
}

else if (coord==3){
x3 = mouseX;
y3 = mouseY;
point(x3,y3);
bezier(x0,y0,x1,y1,x2,y2,x3,y3);
println("x " +mouseX+" Y "+ mouseY);
coord =0;
println("Bezier Curve is complete ");
}

}

/***********************************************************
* Draw random Triangles with 3 lines
*/
void drawTriangle(){
float a = random(xMax);
float c = random(xMax);
float e = random(xMax);
float b = random(yMax);
float d = random(yMax);
float f = random(yMax);
lineClip(a,b,c,d);
lineClip(c,d,e,f);
lineClip(e,f,a,b);
}

/***********************************************************
* Cohen-Sutherland Line Clipping Algorithm
*/
void lineClip(float x0, float y0, float x1,float y1){
int[] coord0 = new int[4];
int[] coord1 = new int[4];

//1001 1000 1010
//0001 0000 0010
//0101 0100 0110

for(int i = 0; i <>= xclipMax) { //if greater then 540
coord0[1]=1;
print(" right ");
print(" X0 " +x0);
}

if (y0 <= yclipMin) { //if less then 120 coord0[2]=1; print(" bottom "); print(" Y0 "+ y0); } else if ( y0 >= yclipMax) { //if greater tehn 360
coord0[3]=1;
print(" top ");
print(" Y0 " +y0);
}
/***********************************************************
* Second set of points
*/
if (x1 <= xclipMin) { //if less then 180 coord1[0]=1; print(" left "); print(" X1 " +x1); } else if ( x1 >= xclipMax) { //if greater then 540
coord1[1]=1;
print(" right ");
print(" X1 " +x1);
}

if (y1 <= yclipMin) { //if less then 120 coord1[2]=1; print(" bottom "); print(" Y1 "+ y1); } else if ( y1 >= yclipMax) { //if greater tehn 360
coord1[3]=1;
print(" top ");
print(" Y1 " +y1);
}


if ((coord0[0]coord1[0]) == 0 && (coord0[1]coord1[1]) == 0 && (coord0[2]coord1[2]) == 0 && (coord0[3]coord1[3]) == 0){ // test one check if the line is inside the box
line(x0,y0,x1,y1); //draw the line freely
println("Line completley in the square ");
}
else if (((coord0[0]&coord1[0]) != 0) ((coord0[1]&coord1[1]) != 0) ((coord0[2]&coord1[2]) != 0) ((coord0[3]&coord1[3]) != 0)){
println(" Line not Drawn outside the box ");
}
else {

// huge else start
float modX0 = x0;
float modY0 = y0;
float modX1 = x1;
float modY1 = y1;
//1001 1000 1010
//0001 0000 0010
//0101 0100 0110
//clip first set of points
if (coord0[0] == 1){ //if left
if (modX0 <= xclipMin modX0 >= xclipMax){
modY0 = y0 + (y1 - y0) * (xclipMin - x0) / (x1 - x0);
modX0 = xclipMin;
println(" 0 left ");
}
}
else if (coord0[1] == 1) { //if right
if (modX0 <= xclipMin modX0 >= xclipMax){
modY0 = y0 + (y1 - y0) * (xclipMax - x0) / (x1 - x0);
modX0 = xclipMax;
println(" 0 right ");
}
}
if (coord0[2] == 1 ) { //if bottom
if (modY0 <= yclipMin modY0 >= yclipMax) {
modX0 = x0 + (x1 - x0) * (yclipMin - y0) / (y1 - y0);
modY0 = yclipMin;
println(" 0 bottom ");
}
}
else if (coord0[3] == 1){ //if top
if (modY0 <= yclipMin modY0 >= yclipMax){
modX0 = x0 + (x1 -x0) * (yclipMax - y0) / (y1 - y0);
modY0 = yclipMax;
println( "0 top ");
}
}
//1001 1000 1010
//0001 0000 0010
//0101 0100 0110
//clip first second set of points
if (coord1[0] == 1){ //if left
if (modX1 <= xclipMin modX1 >= xclipMax){
modY1 = y0 + (y1 - y0) * (xclipMin - x0) / (x1 - x0);
modX1 = xclipMin;
println(" 1 left ");
}
}
else if (coord1[1] == 1) { //if right
if (modX1 <= xclipMin modX1 >= xclipMax){
modY1 = y0 + (y1 - y0) * (xclipMax - x0) / (x1 - x0);
modX1 = xclipMax;
println(" 1 right ");
}
}
if (coord1[2] == 1 ) { //if bottom
if (modY1 <= yclipMin modY1 >= yclipMax) {
modX1 = x0 + (x1 - x0) * (yclipMin - y0) / (y1 - y0);
modY1 = yclipMin;
println(" 1 bottom ");
}
}
else if (coord1[3] == 1){ //if top
if (modY1 <= yclipMin modY1 >= yclipMax){
modX1 = x0 + (x1 -x0) * (yclipMax - y0) / (y1 - y0);
modY1 = yclipMax;
println( "1 top ");
}
}
if (modX0 >= xclipMin && modX0 <= xclipMax && modX1 >= xclipMin && modX1 <= xclipMax) { if (modY0 >= yclipMin && modY0 <= yclipMax && modY1 >= yclipMin && modY1 <= yclipMax) { line(modX0,modY0,modX1,modY1); // modded line } } } // end the huge else } // end line clip function