/* Note:Your choice is C IDE */
#include "stdio.h"
#include "graphics.h"
#define BACKCOLOR BLACK
#define WRITECOLOR WHITE
void Line_Bresenham(int x0, int y0, int x1, int y1, int Color)
{
int dx = x1 - x0, dy = y1 - y0,x = x0, y = y0;
int twodx = 2*dx, twody = 2*dy, d = 2*dy-dx;
int i;
putpixel(x,y,Color);
if(x0 == x1)
{
if(y0 < y1)
{
for(i = y0; i <= y1; i++)
putpixel(x0, i, Color);
}
else
{
for(i = y1; i <= y0; i++)
putpixel(x0, i, Color);
}
}
else if(((y1 - y0)/(x1 - x0) >= 0 & (y1 - y0)/(x1 - x0) <= 1) || ((y1 - y0)/(x1 - x0) >= -1 & (y1 - y0)/(x1 - x0) <= 0))
{
while(x < x1)
{
if (d > 0)
{
y++;
d -= twodx;
}
x++;
d += twody;
putpixel(x, y, Color);
}
}/*end of the first 'else if'*/
else
{
if(y0 < y1)
{
y = y0;
while(y < y1)
{
if(d < 0)
{
x++;
d =d+twody;
}
y++;
d -= twodx;
putpixel(x, y, Color);
}
}
}
}/*end of the method Line_Bresenham*/
main()
{
int gdrive = DETECT,gmode;
int tmp;
float x0, y0, x1, y1;
/*initgraph the mode of the computer*/
initgraph(&gdrive,&gmode,"");
cleardevice();
setbkcolor(BACKCOLOR);
printf("lease iput the coordinate of the whole tree\n x0=");
scanf("%f",&x0);
printf("\n y0=");
scanf("%f",&y0);
printf("\n x1=");
scanf("%f",&x1);
printf("\n y1=");
scanf("%f",&y1);
if(x0 > x1)
{
tmp = x0;
x0 = x1;
x1 = tmp;
tmp = y0;
y0 = y1;
y1 = tmp;
}
Line_Bresenham(x0,y0,x1,y1,WRITECOLOR);
getch();
closegraph();
}