Bresenhams Line Drawing Algorithm

Ram Pothuraju
This program is for line drawing using Bresenham's algorithm  . 


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int xa,ya,xb,yb,dx,dy,x,y,xEnd,p;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter (xa,ya) & (xb,yb)\n");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*(dy-dx);
if(xa>xb)
{
 x=xb;
 y=yb;
 xEnd=xa;
}
else
{
 x=xa;
 y=ya;
 xEnd=xb;
}
putpixel(x,y,YELLOW);
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,YELLOW);
}
getch();
closegraph();
}

Post a Comment

0Comments

Post a Comment (0)