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;iint 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 (xcode=code+8;
if (ycode=code+1;
if (y>YMAX)
code=code+2;
return code;
}
No comments:
Post a Comment