c program for bresenham line drawing algorithm




This is a C program for implementing bresenham line drawing algorithm.The explanation of the coding is as follows:
Get the two end points for a line and store it into the frame buffer.Then calculate the decision parameter,p.then if p>0,then the p=p+2dy,else p=p+2(dy-dx).
Open Turbo C windows and type the coding.then compile and run the program by specifying the end points.the program is as follows.

BRESENHAM’S ALGORITHM FOR LINE DRAWING

#include
#include
#include
void linebres(int,int,int,int);
int a,b,c,d;
int main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode," ");
printf("enter x co ordinates");
scanf("%d %d",&a,&b);
printf("enter y co ordinates");
scanf("%d %d",&c,&d);
linebres(a,b,c,d);
getch();
closegraph();
return 0;
}
void linebres(int xa,int ya,int xb,int yb)
{
int dx=abs(xb-xa),dy=abs(yb-ya);
int p=2*dy-dx;
int twody=2*dy,twodydx=2*(dy-dx);
int x,y,xend;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
while(x{
x++;
if(p<0)
p+=twody;
else
{
y++;
p+=twodydx;
}
putpixel(x,y,1);
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: