Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Computer GraphicsRSS Feeds

Program to draw a Sphere using Parametric Equations

Posted By: Easy Tutor     Category: C++ Programming     Views: 4013

Write a program to draw a Sphere using Parametric Equations.

Code for Program to draw a Sphere using Parametric Equations in C++ Programming

 # 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(constint,constint,constint,constint);

 void get_projected_point(double&,double&,double&);
 void multiply_matrices(constdouble[4],constdouble[4][4],double[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(constint _x,constint _y,constint _z,constint r)
    {
       double x;
       double y;
       double z;

       for(float u=0;u<=1;u+=0.0005)
      {
         for(float v=0;v<=1;v+=0.05)
        {
           x=((r*cosl((u*M_PI))*cosl((v*2*M_PI)))+_x);
           y=((r*cosl((u*M_PI))*sinl((v*2*M_PI)))+_y);
           z=((r*sinl((u*M_PI)))+_z);

           get_projected_point(x,y,z);

           putpixel(int(x+0.5),int(y+0.5),15);
        }
      }

       for(float v=0;v<=1;v+=0.0005)
      {
         for(float u=0;u<=1;u+=0.05)
        {
           x=((r*cosl((u*M_PI))*cosl((v*2*M_PI)))+_x);
           y=((r*cosl((u*M_PI))*sinl((v*2*M_PI)))+_y);
           z=((r*sinl((u*M_PI)))+_z);

           get_projected_point(x,y,z);

           putpixel(int(x+0.5),int(y+0.5),15);
        }
      }
    }

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

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

       double xy[4]={x,y,z,1};
       double 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(constdouble matrix_1[4],
                 constdouble matrix_2[4][4],double 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);
    }
  
Share: 


Didn't find what you were looking for? Find more on Program to draw a Sphere using Parametric Equations Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to draw a Sphere using Parametric Equations is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!