Search This Blog

Practical 5 Computer Graphics

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

float dda(float x1, float y1, float x2, float y2)
{
     float dx,dy,x,y,length,h,my,mx,ix,iy;
     float signdx, signdy;
     int i;
     
     dx = x2-x1;
     dy = y2-y1;
     
     if(dx>0)
       signdx = 1;
     else if(dx == 0)  
       signdx = 0;
     else
       signdx = -1;  
     
     if(dy>0)
       signdy = 1;
     else if(dy == 0)  
       signdy = 0;
     else
       signdy = -1; 
       
       if(dx<dy)
           length=dy;
       else 
           length=dx;
           
        ix = dx/length;
        iy = dy/length;        
        
        x = x1+(0.5*signdx);
        y = y1+(0.5*signdy);
        
        putpixel(x,y,RED);
        for(i=0;i<=length;i++)
        {
            x = x+ix;
            y = y+iy;
            putpixel(x,y,RED);   
            delay(35);
        }
}

int Circle(int xc, int yc, int x, int y)
{
     putpixel(xc+x,yc+y,RED);
     putpixel(xc-x,yc+y,RED);
     putpixel(xc+x,yc-y,RED);
     putpixel(xc-x,yc-y,RED);
     putpixel(xc+y,yc-x,RED);
     putpixel(xc-y,yc+x,RED);
     putpixel(xc+y,yc+x,RED);
     putpixel(xc-y,yc-x,RED);
}

int circle_bres(int xc, int yc, int r)
{
    int x = 0;
    int y = r;
    
    int d = 3-2*r;
    
    Circle(xc,yc,x,y);
    
    do
    {
       if(d<0)
          d+= 4*x + 6;
       else 
       {
           d += 4*(x-y)+10;
           y--;
       }   
       x++;
       
       Circle(xc,yc,x,y);
    }while(x<=y);
}

int main()
{
  int gd=DETECT,gm; 
 int x,y,r;
      
   
   cout<<"\n Enter the centre point of circle: ";
   cin>>x>>y;
   cout<<"\n Enter the radius of the circle: ";
   cin>>r;   
   
   initgraph(&gd,&gm,NULL);     
   
   circle_bres(x,y,r*2); 
   circle_bres(x,y,r);
   
   dda(x,y-r*2,x-1.73*r,y+r); 
   dda(x-1.73*r,y+r,x+1.73*r,y+r);
   dda(x,y-r*2,x+1.73*r,y+r);
 
   
   delay(1000);
   closegraph();

  return 0; 

} 

PRACTICAL NO. 3 TITLE: Write C++/Java program to draw 2-D object and perform following basic transformations, a) Scaling b) Translation c) Rotation Use operator overloading.

 

PRACTICAL NO. 3

TITLE: Write C++/Java program to draw 2-D object and perform following basic transformations,
a) Scaling
b) Translation
c) Rotation
Use operator overloading.

#include<iostream> #include<graphics.h> #include<math.h> using namespace std; void Display(int n,float c[10][3]) { float maxx,maxy; int i; maxx=getmaxx(); maxy=getmaxy(); maxx=maxx/2; maxy=maxy/2; i=0; while(i<n-1) { line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]); i++; } i=n-1; line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]); line(0,maxy,maxx*2,maxy); line(maxx,0,maxx,maxy*2); } void Multi(int n,float b[10][3],float c[10][3],float a[10][3]) { int i,j,k; for(i=0;i<n;i++) { for(j=0;j<3;j++) { a[i][j]=0; } } for(i=0;i<n;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) { a[i][j]=a[i][j]+(c[i][k]*b[k][j]); } } } } void Translation(int n,float c[][3],float tx,float ty) { float b[10][3],a[10][3]; int i=0,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; } } b[0][0]=1; b[1][1]=1; b[2][0]=tx; b[2][1]=ty; b[2][2]=1; Multi(n,b,c,a); Display(n,a); delay(5000); } void Scaling(int n,float c[][3],float sx,float sy) { float b[10][3],a[10][3]; int i=0,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; } } b[0][0]=sx; b[1][1]=1; b[0][1]=sy; b[0][2]=1; Multi(n,b,c,a); Display(n,a); delay(5000); } void Rotation(int n,float c[][3],float ra) { int i=0,j; float b[10][3],xp,yp,a[10][3]; xp=c[0][0]; yp=c[0][1]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; } } b[0][0]=b[1][1]=cos(ra*3.14/180); b[0][0]=sin(ra*3.14/180); b[1][0]=-sin(ra*3.14/180); b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp; b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp; b[2][2]=1; Multi(n,b,c,a); Display(n,a); delay(5000); } int main() { int i,j,k,iCh,gd=DETECT,n,gm,ch,a; float c[10][3],tx,ty,sx,sy,R,ra; cout<<"\n Enter number of co-ordinates :"; cin>>n; for(i=0;i<n;i++) { cout<<"\n Enter the co-ordinates of vertex:"; cin>>c[i][0]>>c[i][1]; c[i][2]=1; } initgraph(&gd,&gm,NULL); Display(n,c); delay(5000); closegraph(); do { cout<<"*********************MENU**********************"; cout<<"\n1. Translation..."; cout<<"\n2. Scaling..."; cout<<"\n3. Rotation..."; cout<<"\n4. Exit..."; cout<<"\nEnter your choice: "; cin>>ch; switch(ch) { case 1: cout<<"\n Enter the translation factor for x and y axis:"; cin>>tx>>ty; initgraph(&gd,&gm,NULL); Translation(n,c,tx,ty); delay(5000); closegraph(); break; case 2: cout<<"\n Enter the scaling factor for x and y axis:"; cin>>sx>>sy; initgraph(&gd,&gm,NULL); Scaling(n,c,sx,sy); delay(5000); closegraph(); break; case 3: cout<<"\n Enter the angle of rotation:"; cin>>ra; initgraph(&gd,&gm,NULL); Rotation(n,c,ra); delay(5000); closegraph(); break; case 4: exit(0); break; } }while(ch!=4); closegraph(); delay(5000); return 0; }

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

/*

PRACTICAL NO. 2

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;
 
} 



//bresenham s algorithm draw circle using bresenham s algorithm java program to draw circle

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: