Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  ss
Language: C/C++
Code:
#include<graphics.h>
#include<math.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
//Base class
class pixel
{
     public :
          void plot(int x,int y)
          {
              putpixel(x,y,2);
          }
};

// Derived Class
class line_algo :public pixel //Inheritance
{


public :
          void line_draw(int x1,int y1, int x2,int y2); //Function overloading
          void line_draw(float x1,float y1,float x2,float y2);

};


void line_algo::line_draw(float x1,float y1,float x2,float y2) //DDA line
{
float x,y,dx,dy,Dx,Dy,length;
int i;

     dx=abs(x2-x1);
     dy=abs(y2-y1);
     if(dx>=dy)
          length=dx;
     else
          length=dy;
     Dx=(x2-x1)/length;
     Dy=(y2-y1)/length;
     x=x1+0.5;
     y=y1+0.5;
     i=1;
     while(i<=length)
     {
          plot(x,y);
          x=x+Dx;
          y=y+Dy;
          i=i+1;
          delay(50);
     }
}
void line_algo::line_draw(int x1,int y1,int x2,int y2)  //Bresenham's line
{
int x,y,dx,dy,e,i;

     dx=abs(x2-x1);
     dy=abs(y2-y1);
     x=x1;
     y=y1;
     e=(2*dy)-dx;
     i=0;
     do
     {
          plot(x,y);
          if(e>=0)
          {
               y=y+1;
               e=e-(2*dx);
          }
          x=x+1;
          e=e+(2*dy);
          i=i+1;
          delay(50);
     }while(i<=dx);
}


int main()
{
     int ch,c;
     float x1,y1,x2,y2;
     int xp1,yp1,xp2,yp2;
     line_algo l1;
     int i,gm,gd=DETECT;

     do
     {
          cout<<"n===================================================n";
          cout<<"n(1) DDAn(2) Bresenhamsn(3)Exit";
          cout<<"nEnter Your Choice ->  ";
          cin>>ch;

          switch(ch)
          {
               case 1:
                    cout<<"nEnter the coordinate of 1st pt. = ";
                    cin>>x1>>y1;
                    cout<<"nEnter the coordinate of 2nd pt. = ";
                    cin>>x2>>y2;
                    initgraph(&gd,&gm,NULL);
                    l1.line_draw(x1,y1,x2,y2);

                    break;
               case 2:
                    cout<<"nEnter the coordinate of 1st pt. = ";
                    cin>>xp1>>yp1;
                    cout<<"nEnter the coordinate of 2nd pt. = ";
                    cin>>xp2>>yp2;
               initgraph(&gd,&gm,NULL);
                    l1.line_draw(xp1,yp1,xp2,yp2);

                    break;
               case 3:
                    exit(0);
                    break;

          }
          cout<<"If U want to continue press 1 -> ";
          cin>>c;
     }while(c==1);
delay(5000);
closegraph();
return 0;

}
          
          
Comments: