Bresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int dx,dy,x,y,p,x1,x2,y1,y2,xend;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n Enter the coordinates of first point : ");
scanf("%d%d",&x1,&y1);
printf("%d%d \n",x1,y1);
printf("\n\n Enter the co=ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*(dy) - (dx);
if (x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,WHITE);
setcolor(6);
setbkcolor(3);
while(x <= xend)
{
if(p<=0)
{
x=x+1;
y=y;
p=p+2*(dy);
}
else
{
x=x+1;
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}