C Program to implement Bresenham’s Line Drawing Algorithm

#include<graphics.h>
#include<conio.h>
#include<math.h>
void bresenham_algo(int,int,int,int);
void main()
{
 int gd=DETECT,gmode,xa,ya,xb,yb;
 initgraph(&gd,&gmode,"c:\\tc\\bgi");
 printf("Enter first co-ordinates:");
 scanf("%d %d",&xa,&ya);
 printf("\nEnter second co-ordinates:");
 scanf("%d %d",&xb,&yb);
 bresenham_algo(xa,xb,ya,yb);
 getch();
}

void bresenham_algo(int xa,int ya,int xb,int yb)
{
 int dx,dy,x,y,xend,p;
 dx=abs(xa-xb);
 dy=abs(ya-yb);
 p=(2*dy)-dx;
 if(xa<xb)
 {
  x=xa;
  y=ya;
  xend=xb;
 }
 else
 {
  x=xb;y=yb;xend=xa;
 }
 putpixel(x,y,7);
 while(x<=xend)
 {
  x=x+1;
  if(p<0)
  {
   p=p+(2*dy);
  }
  else
  {
   y=y+1;
   p=p+2*(dy-dx);
  }
  putpixel(x,y,7);
 }
}



OUTPUT:

No comments:

Post a Comment