Friday, 29 June 2012

c++ program to draw line using BRESHNHEM'S LINE Algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

struct co_ordinate
{
 int x,y;
};

class bresline
{
 public:
  co_ordinate point[2];
  void input_parameter()
   {


    cout<<"enter starting point co_ordinate:- ";
    cin>>point[0].x>>point[0].y;
    cout<<"enter ending point co_ordinate:- ";
    cin>>point[1].x>>point[1].y;
   }
  void draw_line();
 };//end of class ddaline

void bresline :: draw_line()
 {
  int x,y,dx,dy;
  float p;

  dx=point[1].x-point[0].x;
  dy=point[1].y-point[0].y;

  x=point[0].x;
  y=point[0].y;

  p=(2*dy)-dx;
  putpixel(x,y,5);

  while(x!=point[1].x)
   {
    x++;
    if(p>0)
       y++;
    putpixel(x,y,5);

    if(p<0)
    p=p+(2*dy);
    else
       p=p+((2*dy)-dx);
  }
}//end of function draw_line

void main()
{
 clrscr();
 bresline b;
 int gdriver=DETECT,gmode;
 initgraph(&gdriver,&gmode,"");
 b.input_parameter();
 b.draw_line();
 getch();
}