//Program to plot antialiased line by Bresanham's Approach  

#include <iostream.h>  
#include <graphics.h>  
#include <math.h>  
#include <conio.h>  
intpixel(int x, int y, float f)  
{  
    int i=0;  
/*    float s=1.5/16.0;  
    i=fabs(f)/s;  
    if (i<10)  
      putpixel(x,y,15-i);  
      cout<<i<<"\t";*/  
    f=fabs(f);  
      if (f<.5)  
    putpixel(x,y,15);  
    else if (f<1)  
    putpixel(x,y,12);  
}  
void drawline(int x1, int y1, int x2, int y2)  
{  
  long temp,p, dx, dy;  
  dx=abs(x2-x1);  
  dy=abs(y2-y1);  
  float m,dd;  
  dd=.5/sqrt(dx*dx+dy*dy);  
  m=float(y2-y1)/float(x2-x1)>0?2:1;  
  if (abs(x2-x1)>=abs(y2-y1))  
  {  
    if (x2<x1)  
    {  
      temp=x2;  
      x2=x1;  
      x1=temp;  
      temp=y2;  
      y2=y1;  
      y1=temp;  
    }  
    int x=x1,y=y1;  
    p=2*dy-dx;  
    while (x<x2)  
    {  
      x++;  
      if (p<=0)  
        p+=2*dy;  
      else  
      {  
        p+=2*(dy-dx);  
        y=y+pow(-1,m);  
      }  
      intpixel(x+getmaxx()/2,getmaxy()/2-y,(p+dx)*dd);  
      intpixel(x+getmaxx()/2,getmaxy()/2-y-1,abs(2*dx-p-dx)*dd);  
      intpixel(x+getmaxx()/2,getmaxy()/2-y+1,abs(2*dx+p+dx)*dd);  
    }  
  }  
  else  
  {  
    if (y2<y1)  
    {  
      temp=x2;  
      x2=x1;  
      x1=temp;  
      temp=y2;  
      y2=y1;  
      y1=temp;  
    }  
    int x=x1,y=y1;  
    p=2*dx-dy;  
    while (y<y2)  
    {  
      y++;  
      if (p<=0)  
        p+=2*dx;  
      else  
      {  
        p+=2*(dx-dy);  
        x=x+pow(-1,m);  
      }  
      intpixel(x+getmaxx()/2,getmaxy()/2-y,(p+dy)*dd);  
      intpixel(x+1+getmaxx()/2,getmaxy()/2-y,abs(2*dy-p-dy)*dd);  
      intpixel(x-1+getmaxx()/2,getmaxy()/2-y,abs(2*dy+p+dy)*dd);  
    }  
  }  
}  
void main()  
{  
  int x1, x2, y1, y2;  
  int gdriver = DETECT, gmode, errorcode;  
  initgraph(&gdriver, &gmode, "");  
  struct palettetype pal;  
  getpalette(&pal);  
  for (int i=0;i<16;i++)  
    setrgbpalette(pal.colors[i],i*4,i*4,i*4);  
  setbkcolor(BLACK);  
  cout<<"Enter x1 ";  
  cin>>x1;  
  cout<<"Enter y1 ";  
  cin>>y1;  
  cout<<"Enter x2 ";  
  cin>>x2;  
  cout<<"Enter y2 ";  
  cin>>y2;  
  drawline(x1,y1,x2,y2);  
  getch();  
}