Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  h1
Language: C/C++
Code:
#include <iostream>
#include<graphics.h>
#define ROUND(a)(int(a+0.5))
using namespace std;

class pixel
{
        public:
                float x1,x2,y1,y2;
                int xa,xb,ya,yb;
                void drawline(float,float,float,float);
                void drawline(int,int,int,int);
                
}
class point:public pixel
{
        void getdata1()
        {
                cout<<n enter the points for dda line drawing"<<endl;
                cin>>x1>>y1>>x2>>y2;
                drawline(x1,y1,x2,y2);
        }
        void getdata2()
        {
                cout<<n enter the points for bresenhams line drawing"<<endl;
                cin>>xa>>ya>>xb>>yb;
                drawline(xa,ya,xb,yb);
        }
}
void pixel:: drawline(float x1,float y1,float x2,float y2)
{
        int dx,dy,xinc,yinc,steps,x,y;
        
        dx=abs(x2-x1);
        dy=abs(y2-y1);
        
        if(dx>dy)
            steps=dx;
        else
            steps=dy;
            
        xinc=(x2-x1)/steps;
        yinc=(y2-y1)/steps;
        
        x=x1;
        y=y1;
        
        putpixel(ROUND(x),ROUND(y),10);
        
        for(int k=0;k<steps;k++)
        {
                x=x+xinc;
                y=y+yinc;
                putpixel(ROUND(x)ROUND(y),10);
        }
        
}
void pixel:: drawline(int xa,int ya,int xb,int yb)
{
    float dx,dy,p,xend,x,y;
    
    dx=xb-xa;
    dy=yb-ya;
    
    p=2*dy-dx;
    if(xa>xb)
    {
            x=xb;
            y=yb;
            xend=xa;
    }
    else
    {
            x=xa;
            y=ya;
            xend=xb;
    }
    putpixel(x,y,10);
    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,10);
    
    }
        
        
}
int main() {
     int gd=DETECT,dm;
     initgraph(&gd,&gm,NULL);
     
     point p;
     char ans;
     int ch;
     do
     {
             cout<<"n1.ddan2.bresenhams"<<endl;
             cin>>ch;
             switch(ch)
             {
                     case 1:
                             p.getdata1();
                             
                    case 2:
                             p.getdata2();
             }
             cout<<"n do you want to continue?"<<endl;
             cin>>ans;
     }while(ans=='y'||ans=='Y');
     return 0;
}

          
Comments: