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);
}


}


No comments:

Post a Comment