#include< stdio.h>
#include< graphics.h>
#include< conio.h>
void BRESENHAM_LINE(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2,gdriver=DETECT,gmode;
clrscr();
initgraph(&gdriver,&gmode,"");
printf("\nEnter point1(x1,y1):");
scanf("%d %d",&x1,&y1);
printf("\nEnter point2(x2,y2):");
scanf("%d %d",&x2,&y2);
BRESENHAM_LINE(x1,y1,x2,y2);
getch();
}
void BRESENHAM_LINE(int x1,int y1,int x2,int y2)
{
int dx=abs(x1-x2),dy=abs(y1-y2);
int p=2*dy-dx;
int twoDy=2*dy,twoDyDx=2*(dy-dx);
int x,y,xEnd;
if(x1>x2)
{
x=x2;
y=y2;
xEnd=x1;
}
else
{
x=x1;
y=y1;
xEnd=x2;
}
putpixel(x,y,WHITE);
while(x < xEnd)
{
x++;
if(p < 0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,WHITE);
}
}
OUT PUT
#include< graphics.h>
#include< conio.h>
void BRESENHAM_LINE(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2,gdriver=DETECT,gmode;
clrscr();
initgraph(&gdriver,&gmode,"");
printf("\nEnter point1(x1,y1):");
scanf("%d %d",&x1,&y1);
printf("\nEnter point2(x2,y2):");
scanf("%d %d",&x2,&y2);
BRESENHAM_LINE(x1,y1,x2,y2);
getch();
}
void BRESENHAM_LINE(int x1,int y1,int x2,int y2)
{
int dx=abs(x1-x2),dy=abs(y1-y2);
int p=2*dy-dx;
int twoDy=2*dy,twoDyDx=2*(dy-dx);
int x,y,xEnd;
if(x1>x2)
{
x=x2;
y=y2;
xEnd=x1;
}
else
{
x=x1;
y=y1;
xEnd=x2;
}
putpixel(x,y,WHITE);
while(x < xEnd)
{
x++;
if(p < 0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,WHITE);
}
}
OUT PUT