Wednesday, November 17, 2010

Program to draw a line using Bresenham’s line drawing algorithm
















//Program to draw a line using Bresenham’s line drawing algorithm

#include
#include
#include
#include
void bresen(int,int,int,int);
void main()
{
int gdriver=DETECT,gmode;
int x1,x2,y1,y2;
clrscr();
printf("\nEnter coordinates of end points : ");
printf("\nx1 : ");scanf("%d",&x1);
printf("y1 : ");scanf("%d",&y1);
printf("x2 : ");scanf("%d",&x2);
printf("y2 : ");scanf("%d",&y2);
initgraph(&gdriver,&gmode,"e:\\tc\\bgi");
bresen(x1,y1,x2,y2);
getch();
}
void bresen(int x1,int y1,int x2,int y2)
{
int dx,dy,p,twoDy,twoDyDx,x,y,xEnd;
dx=abs(x2-x1); dy=abs(y2-y1);
p=2*dy-dx;
twoDy=2*dy;
twoDyDx=2*(dy-dx);
if(x1>x2)
{
x=x2;
y=y2;
xEnd=x1;
}

else
{
x=x1;
y=y1;
xEnd=x2;
}

putpixel(x,y,WHITE);
while(x
{
x++;
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,WHITE);
}

}

No comments:

Post a Comment