Tuesday, July 26, 2011

Bresenham’s Line Drawing Algorithm


#include<graphics.h>
#include<iostream.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
class Line
{
      public:
      int x1,x2,y1,y2;
      int dx,dy,x,y,xend,p;
      int xa,ya,xb,yb;
      void input();
      void draw(int,int, int,int);
};
void Line :: input()
{
      cout<<"Enter the points x1,y1:";
      cin>>x1>>y1;
      cout<<"Enter the points x2,y2:";
      cin>>x2>>yb;
draw(x1,y1,x2,y2);
}
void Line :: draw(xa,ya,xb,yb)
{
      dx=abs(xa-xb);
      dy=abs(ya-yb);
      p=2*(dy-dx);
      if (xa>xb)
      {
                  x=xb;
                  y=yb;
                  xend=xa;
      }
      else
      {
                  x=xa;
                  y=ya;
                  xend=xb;
      }
      putpixel (x,y,1);
     
while (x<xend)
      {
                  x=x+1;
                  if (p<0)
                  {
                              p=p+2*dy;
                  }
                  else
                  {
                              y=y+1;
                              p=p+2*(dy-dx);
                  }
                  putpixel (x,y,1);
                  sleep(1);
                 
      }
}
void main()
{
      int gd=DETECT,gm;
      initgraph(&gd,&gm,"");
      Line b;
      b.input();
      closegraph();
      getch();

}

input:   

Enter the points x1,y1:200
300
Enter the points x2,y2:400
350

No comments:

Post a Comment

Please put your valuable comments