Tuesday, February 24, 2009

Rotating Triangles

The final part of this assignment was to use the pushMatrix() and popMatrix() commands along with shape translations to create a design.











void setup(){
size(720, 480);
background(0);
stroke(255);
//top
fill(230,245,255);
triangle(0,0,720/4,0,720/2,480/2);
fill(175,225,255);
triangle(720/2,0,720/4,0,720/2,480/2);
fill(80,195,255);
triangle(720/2,0,(720/4)*3,0,720/2,480/2);
fill(20,175,255);
triangle(720,0,(720/4)*3,0,720/2,480/2);
//bottom
fill(5,5,5);
triangle(0,480,720/4,480,720/2,480/2);
fill(30,40,45);
triangle(720/2,480,720/4,480,720/2,480/2);
fill(30,60,70);
triangle(720/2,480,(720/4)*3,480,720/2,480/2);
fill(25,65,85);
triangle(720,480,(720/4)*3,480,720/2,480/2);
//right--need to change stroke
fill(25,70,100);
triangle(720,0,720,480/4,720/2,480/2);
triangle(720,480/2,720,480/4,720/2,480/2);
triangle(720,480/2,720,(480/4)*3,720/2,480/2);
triangle(720,480,720,(480/4)*3,720/2,480/2);
//left--need to change stroke
triangle(0,0,0,480/4,720/2,480/2);
triangle(0,480/2,0,480/4,720/2,480/2);
triangle(0,480/2,0,(480/4)*3,720/2,480/2);
triangle(0,480,0,(480/4)*3,720/2,480/2);
pushMatrix();
for (int i=0;i<30;i++){
pushMatrix();
fill(random(100),random(200),255);
translate(random(150),random(100));
scale(.9);
ellipse(2,2,100,100);
}
for (int i=0;i<30;i++)
popMatrix();
pushMatrix();
translate(750,0);
for (int i=0;i<30;i++){
pushMatrix();
fill(random(100),random(200),255);
translate(random(150)-150,random(100));
scale(.9);
ellipse(2,2,80,80);
}
for (int i=0;i<30;i++)
popMatrix();
popMatrix();
translate(720/2,480/2);
for (int i=0;i<10;i++){


translate(2,2);
scale(1.4);
fill((i+1)*25);
rect(0,0,10,10);
rotate(PI/30);
pushMatrix();



}
for (int i=0;i<10;i++){
popMatrix();
}

translate(-10,-10);
for (int j=0;j<10;j++){


translate(-2,-6);
scale(1.3);
fill(20,random(255),255);
rect(0,0,10,10);

stroke(random(255));
scale(1.3);
fill(random(255),255,255);
translate(-2,-6);
ellipse(5,5,10,10);
rotate(PI/30);
pushMatrix();



}
for (int i=0;i<10;i++)
popMatrix();
translate(0,-20);


}
void draw(){}




The second part of assignment 4 is to use our rotating triangles function to create an interesting image using only rotating triangles.











void setup()
{
size(720, 480);
background(0);
stroke(255);
int x1=0;
int x2=0;
int x3=20;
int y1=0;
int y2=20;
int y3=0;
int x4=720;
int y4=480;
int x5=720;
int y5=460;
int x6=700;
int y6=480;
int x11=0;
int x12=0;
int x13=20;
int y11=480;
int y12=460;
int y13=480;
int x14=720;
int y14=0;
int x15=720;
int y15=20;
int x16=700;
int y16=0;
int r1=255;
int g1=255;
int b1=255;
int r2=0;
int g2=0;
int b2=0;
stroke(r1,g1,b1);
int ang1=0;
int ang2=0;
myTriangle(x1,y2,x2,y2,x3,y3);
for (int i=0;i<300;i++){
ang1=ang1+20;
ang2=ang2-20;
x1=x1+3;
y1=y1+2;
x2=x2+3;
y2=y2+2;
x3=x3+3;
y3=y3+2;
x4=x4+3;
x5=x5+3;
x6=x6+3;
y4=y4+2;
y5=y5+2;
y6=y6+2;
x11=x11-3;
y11=y11-2;
x12=x12-3;
y12=y12-2;
x13=x13-3;
y13=y13-2;
x14=x14-3;
x15=x15-3;
x16=x16-3;
y14=y14-2;
y15=y15-2;
y16=y16-2;
r1=r1-2;
g1=g1-3;
b1=b1-4;
r2=r2+4;
g2=g2+3;
b2=b2+2;
stroke(r1,g1,b1);
Point p1=new Point(x1, y1);
Point p2=new Point(x2 ,y2);

Point p3=new Point(x3 ,y3);
rotateTri(p1,p2,p3,ang1);
stroke(b1,g1,r1);
Point p4=new Point(x4, y4);
Point p5=new Point(x5 ,y5);

Point p6=new Point(x6 ,y6);
rotateTri(p3,p4,p5,ang2);
stroke(r2,g2,b2);
Point p11=new Point(x11, y11);
Point p12=new Point(x12 ,y12);

Point p13=new Point(x13 ,y13);
//rotateTri(p11,p12,p13,ang1);
stroke(b2,g2,r2);
Point p14=new Point(x14, y14);
Point p15=new Point(x15 ,y15);

Point p16=new Point(x16 ,y16);
rotateTri(p16,p14,p15,ang2);
}
}
void draw(){}
void myTriangle(int x1,int y1,int x2, int y2, int x3, int y3){
line(x1,y1, x2,y2);
line(x2,y2, x3,y3);
line(x3,y3,x1,y1);
}
void rotateTri(Point p1, Point p2, Point p3, float angle){
//draw original triangle

float rad=radians(angle);
Point center=new Point(((p1.x+p2.x+p3.x)/3),((p1.y+p2.y+p3.y)/3));
Point temp1=new Point(p1.x-center.x, p1.y-center.y);
Point temp2=new Point(p2.x-center.x, p2.y-center.y);
Point temp3=new Point(p3.x-center.x, p3.y-center.y);
p1=rotatePnt(temp1, rad);
p2=rotatePnt(temp2, rad);
p3=rotatePnt(temp3, rad);
p1=new Point(p1.x+center.x, p1.y+center.y);
p2=new Point(p2.x+center.x, p2.y+center.y);
p3=new Point(p3.x+center.x, p3.y+center.y);

myTriangle(p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
}
Point rotatePnt(Point pnt, float angle)
{
float[][] homoCoord = new float[1][3];
float[][] homoCoord2= new float[1][3];
float[][] transformMat= new float[3][3];
float cosine=cos(angle);
float sine=sin(angle);
Point temp=new Point();


homoCoord[0][0]=pnt.x;
homoCoord[0][1]=pnt.y;
homoCoord[0][2]=1;

transformMat[0][0]=cosine;
transformMat[0][1]=sine;
transformMat[0][2]=0;
transformMat[1][0]=-sine;
transformMat[1][1]=cosine;
transformMat[1][2]=0;
transformMat[2][0]=0;
transformMat[2][1]=0;
transformMat[2][2]=1;

homoCoord2=multMat(homoCoord, transformMat);
temp.x=(int)homoCoord2[0][0];
temp.y=(int)homoCoord2[0][1];
return temp;

}
float[][] multMat(float[][] m1,float[][] m2){
float[][]newMatrix=new float[1][3];
for(int i = 0; i < 1; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++){
newMatrix[i][k]+=m1[i][j]*m2[j][k];
}
}
}
return newMatrix;
}




This assignment involved writing a function in processing that takes in the 3 points of a triangle and its angle of rotation and draws both the original triangle and the triangle rotated by the angle parameter. In this example, the original triangle (light gray) is rotated by 90 degrees (white).








void setup()
{
size(720, 480);
background(0);
stroke(255);
Point p1=new Point(100,240);
Point p2=new Point(500,240);
Point p3=new Point(300,100);
rotateTri(p1,p2,p3,90);
}
}
void draw(){}
void myTriangle(int x1,int y1,int x2, int y2, int x3, int y3){
line(x1,y1, x2,y2);
line(x2,y2, x3,y3);
line(x3,y3,x1,y1);
}
void rotateTri(Point p1, Point p2, Point p3, float angle){
//draw original triangle
myTriangle(p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
float rad=radians(angle);
Point center=new Point(((p1.x+p2.x+p3.x)/3),((p1.y+p2.y+p3.y)/3));
Point temp1=new Point(p1.x-center.x, p1.y-center.y);
Point temp2=new Point(p2.x-center.x, p2.y-center.y);
Point temp3=new Point(p3.x-center.x, p3.y-center.y);
p1=rotatePnt(temp1, rad);
p2=rotatePnt(temp2, rad);
p3=rotatePnt(temp3, rad);
p1=new Point(p1.x+center.x, p1.y+center.y);
p2=new Point(p2.x+center.x, p2.y+center.y);
p3=new Point(p3.x+center.x, p3.y+center.y);

myTriangle(p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
}
Point rotatePnt(Point pnt, float angle)
{
float[][] homoCoord = new float[1][3];
float[][] homoCoord2= new float[1][3];
float[][] transformMat= new float[3][3];
float cosine=cos(angle);
float sine=sin(angle);
Point temp=new Point();


homoCoord[0][0]=pnt.x;
homoCoord[0][1]=pnt.y;
homoCoord[0][2]=1;

transformMat[0][0]=cosine;
transformMat[0][1]=sine;
transformMat[0][2]=0;
transformMat[1][0]=-sine;
transformMat[1][1]=cosine;
transformMat[1][2]=0;
transformMat[2][0]=0;
transformMat[2][1]=0;
transformMat[2][2]=1;

homoCoord2=multMat(homoCoord, transformMat);
temp.x=(int)homoCoord2[0][0];
temp.y=(int)homoCoord2[0][1];
return temp;

}
float[][] multMat(float[][] m1,float[][] m2){
float[][]newMatrix=new float[1][3];
for(int i = 0; i < 1; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++){
newMatrix[i][k]+=m1[i][j]*m2[j][k];
}
}
}
return newMatrix;
}



Sunday, February 1, 2009

Assignment 4: Triangles and Line Clipping

Assignment 4 involves:
1. Creating an applet that creates a cubic Bezier curve based on points created when the user clicks on the applet
2. Creating a function that takes 3 sets of coordinates and draws a triangle
3. Adding to the triangle function to create 5 triangles with random coordinates on a 720 x 480 image
4. To be continued....

1. Bezier curve using user input:

**Still working on embedding the applet here.











int count=0;
int x1=0;
int x2=0;
int x3=0;
int x4=0;
int y1=0;
int y2=0;
int y3=0;
int y4=0;

void setup(){
size(200,200);
background(0);
stroke(255);
}
void draw(){}
void mousePressed(){

if (count==0){
x1=mouseX;
y1=mouseY;
point(x1,y1);
count++;
}
else if (count==1){
x2=mouseX;
y2=mouseY;
point(x2,y2);
count++;
}
else if (count==2){
x3=mouseX;
y3=mouseY;
point(x3,y3);
count++;

}
else if (count==3){
background(0);
x4=mouseX;
y4=mouseY;
cubBesCurve(x1,y1,x2,y2,x3,y3,x4,y4);
count++;
}
else if (count==4){
background(0);
count=0;
}
}

void parLine(int x1, int y1, int x2, int y2){
for (float t=0;t<1;t=t+.001){
point(x1+t*(x2-x1),y1+t*(y2-21));
}
}
void quadBesCurve(int x1, int y1, int x2, int y2, int x3, int y3){
for (float t=0;t<1;t=t+.001){
point((sq(1-t)*x1+2*(1-t)*t*x2+sq(t)*x3),(sq(1-t)*y1+2*(1-t)*t*y2+sq(t)*y3));
}
}
void cubBesCurve(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
for (float t=0;t<1;t=t+.001){
point ((pow((1-t),3)*x1+3*sq(1-t)*t*x2+3*(1-t)*sq(t)*x3+pow(t,3)*x4),
(pow((1-t),3)*y1+3*sq(1-t)*t*y2+3*(1-t)*sq(t)*y3+pow(t,3)*y4));
}
}

2. Drawing Triangles:

The first step to this part was to create a function to draw 1 triangle, then I created another function to draw 5 triangles in random positions



Here is a shot after implementing a line clipping algorithm:





















Source Code:
int XMAX=540;
int XMIN=180;
int YMAX=360;
int YMIN=120;

void setup(){
size(720,480);
stroke(255);
background(0);
fill(50);

rect(XMIN,YMIN,360,240);

myRandomTriangles(5);

}
void draw(){

}
void myRandomTriangles(int numTriangles){

for (int i=0;i int r=(int)random(255);
int g=(int)random(255);
int b=(int)random(255);
int x1=(int)random(720);
int x2=(int)random(720);
int x3=(int)random(720);
int y1=(int)random(480);
int y2=(int)random(480);
int y3=(int)random(480);
stroke(r,g,b);
myTriangle(x1,y1,x2,y2,x3,y3);
}
}
void drawLine(int x1,int y1,int x2,int y2){
int code1=getLocationCode(x1,y1);
int code2=getLocationCode(x2,y2);
if ((code2==0)&&(code1==0)){
line(x1,y1,x2,y2);
}
else if ((code1&code2)!=0){
}
else {
clipLine(x1,y1,x2,y2);
}
}
void myTriangle(int x11,int y11,int x12, int y12, int x13, int y13){
int x1=x11;
int x2=x12;
int x3=x13;
int y1=y11;
int y2=y12;
int y3=y13;

drawLine(x1,y1, x2,y2);
drawLine(x2,y2, x3,y3);
drawLine(x1,y1,x3,y3);

}
void clipLine(int x1, int y1,int x2, int y2){
int code1=getLocationCode(x1, y1);
int code2=getLocationCode(x2,y2);
double m=((double)(y2-y1)/(x2-x1));
double b=y2-m*x2;
if( (code1&1)!=0){

x1 = (int)((YMIN-b)/m);
y1 = YMIN;
code1=getLocationCode(x1,y1);
}
else if ((code1 & 2)!= 0){

x1= (int)((YMAX-b)/m);
y1 = YMAX;
code1=getLocationCode(x1,y1);
}
if((code1 & 4)!= 0){

y1 =(int)(m*XMAX+b);
x1 = XMAX;
code1=getLocationCode(x1,y1);
}
else if((code1 & 8)!=0){

y1 = (int)(m*XMIN+b);
x1 = XMIN;
code1=getLocationCode(x1,y1);
}
if( (code2&1)!=0){

x2 = (int)((YMIN-b)/m);
y2 = YMIN;
code2=getLocationCode(x2,y2);
}
else if ((code2 & 2)!= 0){

x2= (int)((YMAX-b)/m);
y2 = YMAX;
code2=getLocationCode(x2,y2);
}
if((code2 & 4)!= 0){

y2 =(int)(m*XMAX+b);
x2 = XMAX;
code2=getLocationCode(x2,y2);
}
else if((code2 & 8)!=0){

y2 = (int)(m*XMIN+b);
x2 = XMIN;
code2=getLocationCode(x2,y2);
}

line(x1,y1,x2,y2);
}
int getLocationCode(int x, int y){
int code=0;
if (x>XMAX)
code=code+4;
if (x code=code+8;
if (y code=code+1;
if (y>YMAX)
code=code+2;
return code;
}