Bresenham's Line drawing Algorithm

As with the "Computer Graphics" programming holds, the Bresenham's Line Drawing Algorithm is one cool algo to play with. There's no pixel overlapping and it's kinda easy too. Here's the C codes for the program, just copy it out and see for yourself.

#include
#include
#include
#include

void main()
{
clrscr();
/* (x0,y0) and (x1,y1) the end points of the line,
dx = difference of the x co-ordinates,
dy = differenc of the y co-ordinates
p = descision parameter */

int x0,y0,x1,y1,dx,dy,x,y,xend,p,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter x0,y0,x1,y1 :");
scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
dx=abs(x0-x1); //Calculating the difference of the x co-ordinates
dy=abs(y0-y1); //Calculating the difference of the y co-ordinates
p=2*dy-dx; //descision parameter

/* The if-else statement puts the left coordinate out of the two ends in x0,y0 */
if(x0>x1)
{
x = x1;
y = y1;
xend=x0;
}
else
{
x=x0;
y=y0;
xend=x1;
}
putpixel(x,y,1); // puts the pixels on the screen
while(x< xend)
{
x++;
if(p<0) // for p<0
{
p=p+2*dy;
}
else // for p>0
{
y++;
p=p+2*(dy-dx);
}
putpixel(x,y,1);
}
getch();
}


blog comments powered by Disqus