Tuesday, January 27, 2009

Bezier Curves

The following is from an interactive program I wrote to generate Bezier curves. The first click of the mouse draws a point. The second click draws a parametric line. The third click draws a Quadratic Bezier curve, the fourth click draws a Cubic Bezier curve, and the fifth click clears the screen and starts all over again.

Click the picture below open up the application in a new page:




Here is the code:

int pointNum=0;
int x0=0;
int y0=0;
int x1=0;
int y1=0;
int x2=0;
int y2=0;

void setup() {
size(400, 400);
stroke(255);
background(192, 64, 0);
}

void mouseReleased(){
if (pointNum==0){
x0=mouseX;
y0=mouseY;
point(x0,y0);
pointNum=1;
println("point0");

}
else if (pointNum==1){
paraLine(x0,y0,mouseX,mouseY);
x1=mouseX;
y1=mouseY;
pointNum=2;
println("point1");
}
else if (pointNum==2){
background(192, 64, 0);
quadBez(x0,y0,x1,y1,mouseX,mouseY);
x2=mouseX;
y2=mouseY;
pointNum=3;
}
else if(pointNum==3){
background(192, 64, 0);
cubBez(x0,y0,x1,y1,x2,y2,mouseX,mouseY);
pointNum=4;
}
else{
background(192, 64, 0);
pointNum=0;
}

}

void paraLine(int x0,int y0,int x1,int y1){
float x=0;
float y=0;
for(float t=0; t<=1; t=t+.001) { x=x0+(t*(x1-x0)); y=y0+(t*(y1-y0)); point(int(x),int(y)); } } void quadBez(int x0,int y0,int x1,int y1, int x2, int y2){ float x=0; float y=0; for(float t=0; t<=1; t=t+.001) { x=(pow((1-t),2)*x0)+(2*(1-t)*(t*x1))+(pow(t,2)*x2); y=(pow((1-t),2)*y0)+(2*(1-t)*(t*y1))+(pow(t,2)*y2); point(int(x),int(y)); } } void cubBez(int x0,int y0,int x1,int y1, int x2, int y2, int x3, int y3){ float x=0; float y=0; for(float t=0; t<=1; t=t+.001) { x=(pow((1-t),3)*x0)+(3*(pow((1-t), 2))*(t*x1))+(3*(1-t)*(pow(t,2)*x2))+(x3*pow(t,3)); y=(pow((1-t),3)*y0)+(3*(pow((1-t), 2))*(t*y1))+(3*(1-t)*(pow(t,2)*y2))+(y3*pow(t,3)); point(int(x),int(y)); } }

Thursday, January 15, 2009

Bresenham’s line algorithm


Here is a line created using Bresenham’s line algorithm.




Here is the code that drewdit:
void myLine(int x0, int y0, int x1, int y1){

//setup variables
int deltax=x1-x0;
int deltay=y1-y0;
float error;
int xstep, ystep;

//if y slopes down
if(deltay<0){ deltay="-deltay;" ystep="-1;" ystep="1;" deltax="-deltax;" xstep="-1;" xstep="1;">deltay){
error=deltay - (deltax>>1);
while (x0 != x1) {
if (error >= 0) {
y0 += ystep;
error -= deltax;
}
x0 += xstep;
error += deltay;
point( x0, y0);
}
} else {
error = deltax - (deltay >>1);
while (y0 != y1) {
if (error >= 0) {
x0 += xstep;
error -= deltay;
}
y0 += ystep;
error += deltax;
point( x0, y0);
}

}
}



Here is a web made using that same line algorithm followed by the code that generated the web:


void web(int num){
int size=500;
int skip=size/num;
int x1=0;
int y1=0;
int x2=0+skip;
int y2=size;

for(int i=0;i<=size; i=i+skip){ myLine(x1,y1,x2,y2); y1+=skip; x2+=skip; } }



Finally here is a circle based on the same algorithm:

and here is some code for your face to stare at:

void myCircle(int x, int y, int rad)
{

int invRad=1-rad;
int diax=1;
int diay=-2*rad;
int x1=0;
int y1=rad;

//draw n/s/e/w points
point(x,y+rad);
point(x,y-rad);
point(x+rad,y);
point(x-rad,y);

while(x1=0)
{
y1--;
diay += 2;
invRad += diay;
}
x1++;
diax+=2;
invRad+=diax;
point(x+x1, y+y1);
point(x-x1, y+y1);
point(x+x1, y-y1);
point(x-x1, y-y1);
point(x+y1, y+x1);
point(x-y1, y+x1);
point(x+y1, y-x1);
point(x-y1, y-x1);
}


}


Thursday, January 8, 2009

Homework 1....Doh!

Here is an image of Homer Simpson created using the Processing Graphics Environment.
And Here is the code:

size(200,200);
background(65,198,226);
noStroke();
fill(249, 200, 50);
rect(60,50,65,50);
ellipseMode(CORNER);
ellipse(60,25,65,50);
rect(75,100,50,50);
fill(178,138,16);
arc(50,90,60,50,PI,0);
arc(60,90,50,40,0,PI);
fill(255,255,255);
stroke(0);
ellipse(50,60,26,26);
noStroke();
fill(249, 200, 50);
ellipse(50,81,15,15);
rect(55,81,45,15);
ellipse(116,84,15,15);
fill(255,255,255);
stroke(0);
ellipse(70,60,26,26);
fill(0,0,0);
ellipse(57,72,3,3);
ellipse(83,72,3,3);
line(60,115,80,115);
noFill();
arc(80,18,25,20,PI,0);
arc(90,20,25,20,PI,0);
line(116,84,120,70);
line(120,70,124,84);
line(124,84,129,70);
line(129,70,132,84);