Search This Blog

TITLE:2. Write C++/Java program to draw circle using Bresenham‘s algorithm. Inherit pixel class.

TITLE:Write C++/Java program to draw circle using Bresenham‘s algorithm. Inherit pixel class.


#include<iostream>
#include<graphics.h>
using namespace std;

class Pixel
{
   public:
     void pixel(int X,int Y,int x,int y)
      {
          putpixel(X+x, Y+y, RED);
          putpixel(X-x, Y+y, RED);
          putpixel(X+x, Y-y, RED);
          putpixel(X-x, Y-y, RED);
          putpixel(X+y, Y+x, RED);
          putpixel(X-y, Y+x, RED);
          putpixel(X+y, Y-x, RED);
          putpixel(X-y, Y-x, RED);

      }
     
};

class Bresenham : public Pixel
{
   public:
     void Bres_algo(int X,int Y,int r)
      {
         int x=0;
         int y=r;
        
         int d = 3-(2*r);
        
         pixel(X,Y,x,y);
        
         do
         {
            if(d<0)
            {
               d = d+(4*x)+6;
            }
            else
            {
               d = d+4*(x-y)+10;
               y--;
            }
            x++;
            pixel(X,Y,x,y);
         }while(x<=y);
      }
};

int main()
{
   int X,Y,r;
  
   Bresenham b;
  
   cout<<"\nBRESENHAM CIRCLE DRAWING.....";
   cout<<"\n Enter the co-ordinates (X,Y) :";
   cin>>X>>Y;
   cout<<"\n Enter the Radius :";
   cin>>r;
  
   int gd = DETECT, gm;
   initgraph(&gd,&gm,NULL);
   b.Bres_algo(X,Y,r);
  
    getch();
    delay(100000);
    closegraph();   
    return 0;
 
}

OUTPUT:

TITLE: Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and Use function overloading.

TITLE: Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and Use function overloading.

#include<iostream>
#include<math.h>
#include<graphics.h>
using namespace std;

class pixel_entry
{
    public:
    int x1,y1,x2,y2;
    void accept()
    {
        cout<<"\n Enter the x1 coordinates: ";
        cin>>x1;
        cout<<"\n Enter the y

1 coordinates: ";
        cin>>y1;
        cout<<"\n Enter the x2 coordinates: ";
        cin>>x2;
        cout<<"\n Enter the y2 coordinates: ";
        cin>>y2;
    }
   
      int plotpixel(int x, int y)
        {
               putpixel(int(x),int(y),WHITE);
        }
};    
class DDA:public pixel_entry
{
    public:
    int i,length;
    float x,y,dx,ix,iy,dy;
   
    int sign(int z)
    {
        if(z>1)
        return 1;
        else
        return -1;
    }
    int line(int x1,int y1,int x2,int y2)
    {   
        dx=abs(x2-x1);
        dy=abs(y2-y1);
        if(dx>dy)
        {
            length=dx;
        }
        else
        {
            length=dy;
        }
        ix=(dx/length);
        iy=(dy/length);
       
        x=x1+0.5*sign(x2-x1);
        y=y1+0.5*sign(y2-y1);
       
        for(i=0;i<=length;i++)
        {
            plotpixel(x,y);
            x=x+ix;
            y=y+iy;
        }
    }
};
class Bresenhms:public pixel_entry
{
    int dx,dy,i,length,x,y,s1,s2,e,temp,exc;
    public:
    int sign(int z)
    {
        if(z>1)
        return 1;
        else
        return -1;
    }   
    int line(int x1,int y1,int x2,int y2)
    {   
        dx=abs(x2-x1);
        dy=abs(y2-y1);
       
        x=x1;
        y=y1;
       
        plotpixel(x,y);
       
        s1=sign(x2-x1);
        s2=sign(y2-y1);
       
        if(dy>dx)
        {
            temp=dy;
            dy=dx;
            dx=temp;
            exc=1;
        }
        else
        {
        exc=0;   
        }
        e=((2*dy)-dx);
        i=1;
        while(i<dx)
        {
            while(e>=0)
            {
                if(exc=1)
                {
                    x=x+s1;
                }
                else
                {
                    y=y+s2;
                }
                e=e-2*dx;
            }
            if(exc=1)
            {
                y=y+s2;
            }
            else
            {
                x=x+s1;
            }
            e=e+2*dy;
            plotpixel(x,y);
            i++;
        }
    }   
};   
   
int main()
{   
   
    int ch,gm,gd=DETECT;
    char ans;
   
    DDA D;
    Bresenhms B;
    cout<<"\n********************MENU**************************";
    cout<<"\n1.DDA Line Drawing Algorithm...";
    cout<<"\n2.Bresenhams Line Drawing Algorithm...";
    cout<<"\n3.Exit...";
   
    do
    {
       
        cout<<"\nEnter the choice: ";
        cin>>ch;
       
        switch(ch)
        {
           
            case 1: D.accept();
                initgraph(&gd,&gm,NULL);
                D.line(D.x1,D.y1,D.x2,D.y2);
                delay(100000);
                closegraph();
                break;
               
            case 2: B.accept();
                initgraph(&gd,&gm,NULL);
                B.line(B.x1,B.y1,B.x2,B.y2);
                delay(100000);
                closegraph();
                break;
               
        }
    }while(ch!=3);
   
    delay(10000);
    closegraph();

    return 0;
}

 

OUTPUT: