Draw a line using bresenham’s line drawing algorithm with all cases
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void bres(int, int, int, int);
void main()
{
int gdriver = DETECT, gmode, x1,x2,y1,y2;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();
printf("Enter the values for Starting point (X1,Y1) : ");
scanf("%d %d",&x1,&y1);
printf("Enter the values for Ending point (X2,Y2) : ");
scanf("%d %d",&x2,&y2);
bres(x1,y1,x2,y2);
getch();
closegraph();
}
void bres(int xa, int ya, int xb, int yb)
{
int dx = abs(xb-xa), dy = abs(yb-ya);
int Ty = 2*dy, Tyx = 2*(dy-dx), p = 2*dy-dx;
int x, y, xEnd;
if(xa > xb)
{
x = xb;
y = yb;
xEnd = xa;
}
else
{
x = xa;
y = ya;
xEnd = xb;
}
putpixel(x,y,14);
while(x < xEnd)
{
x++;
delay(100);
if(p < 0)
p += Ty;
else
{
y++;
p += Tyx;
}
putpixel(x,y,14);
}
}