Bresenham's Line drawing Algorithm

4 February, 2009 | admin | Comments
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();
}

  • Anonymous
    - <![CDATA[ If you're Using Turbo C and u got an error saying "Declaration Not allowed here" at line at the initial variable declaration line, just cut the "clrscr()" from the first line in the main function and past it below the variable declaration line. That will do!!
    ]]>
  • Anonymous
    - <![CDATA[ If you're Using Turbo C and u got an error saying "Declaration Not allowed here" at line at the initial variable declaration line, just cut the "clrscr()" from the first line in the main function and past it below the variable declaration line. That will do!!
    ]]>
blog comments powered by Disqus