C++ Programming Blog

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

 # define  f                 0.3
 # define  projection_angle   45

 void show_screen( );

 void Sphere(const int,const int,const int,const int);

 void get_projected_point(int&,int&,int&);
 void multiply_matrices(const float[4],const float[4][4],float[4]);


 int main( )
    {
       int driver=VGA;
       int mode=VGAHI;

       int x=0;
       int y=0;
       int z=0;
       int r=0;

       do
      {
         show_screen( );

         gotoxy(8,10);
         cout<<\"Coordinates of Central Point - (x,y,z) :\";

         gotoxy(8,11);
         cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";

         gotoxy(12,13);
         cout<<\"Enter the value of x = \";
         cin>>x;

         gotoxy(12,15);
         cout<<\"Enter the value of y = \";
         cin>>y;

         gotoxy(12,17);
         cout<<\"Enter the value of z = \";
         cin>>z;

         gotoxy(8,21);
         cout<<\"Radius of the Sphere - r :\";

         gotoxy(8,22);
         cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";

         gotoxy(12,24);
         cout<<\"Enter the value of r = \";
         cin>>r;

         initgraph(&driver,&mode,\"..\\\\Bgi\");

         setcolor(15);
           Sphere(x,y,z,r);

         setcolor(15);
           outtextxy(110,460,\"Press <Enter> to continue or any other key to exit.\");

         int key=int(getch( ));

         if(key!=13)
        break;
      }
       while(1);

       return 0;
    }



 //-----------------------------  Sphere( )  -----------------------------//


 void Sphere(const int _x,const int _y,const int _z,const int r)
    {
       int x=_x;
       int y=_y;
       int z=_z;

       get_projected_point(x,y,z);

       for(int angle=2;angle<=r;angle+=5)
      {
         ellipse(x,y,0,360,r,angle);
         ellipse(x,y,0,360,angle,r);
      }
    }

 /************************************************************************/
 //---------------------  get_projected_point( )  -----------------------//
 /************************************************************************/

 void get_projected_point(int& x,int& y,int& z)
    {
       float fcos0=(f*cos(projection_angle*(M_PI/180)));
       float fsin0=(f*sin(projection_angle*(M_PI/180)));

       float Par_v[4][4]={
                {1,0,0,0},
                {0,1,0,0},
                {fcos0,fsin0,0,0},
                {0,0,0,1}
             };

       float xy[4]={x,y,z,1};
       float new_xy[4]={0};

       multiply_matrices(xy,Par_v,new_xy);

       x=new_xy[0];
       y=new_xy[1];
       z=new_xy[2];
    }

 /************************************************************************/
 //----------------------  multiply_matrices( )  ------------------------//
 /************************************************************************/

 void multiply_matrices(const float matrix_1[4],
                  const float matrix_2[4][4],float matrix_3[4])
    {
       for(int count_1=0;count_1<4;count_1++)
      {
         for(int count_2=0;count_2<4;count_2++)
        matrix_3[count_1]+=
               (matrix_1[count_2]*matrix_2[count_2][count_1]);
      }
    }


 //--------------------------  show_screen( )  ---------------------------//


 void show_screen( )
    {
       restorecrtmode( );
       clrscr( );
       textmode(C4350);

       cprintf(\"\\n********************************************************************************\");
       cprintf(\"*-********************************-          -********************************-*\");
       cprintf(\"*---------------------------------- \");

       textbackground(1);
       cprintf(\" Sphere \");
       textbackground(8);

       cprintf(\" ----------------------------------*\");
       cprintf(\"*-********************************-          -********************************-*\");
       cprintf(\"*-****************************************************************************-*\");

       for(int count=0;count<42;count++)
      cprintf(\"*-*                                                                          *-*\");

       gotoxy(1,46);
       cprintf(\"*-****************************************************************************-*\");
       cprintf(\"*------------------------------------------------------------------------------*\");
       cprintf(\"********************************************************************************\");

       gotoxy(1,2);
    }

 
 
Didn't find what you were looking for? Find more on Program to draw a Sphere using Ellipses