Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program to show the simulation of the Solar System

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

A C++ Program to show the simulation of the Solar System.

Code for Program to show the simulation of the Solar System in C++ Programming

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


 /*************************************************************************///-------------------------------  Planet  ------------------------------///*************************************************************************/class Planet
 {
    private:
       int x;
       int y;
       int rx;
       int ry;
       int color;
       int radius;
       int position;

       float displacement;

       char* Buffer;

    public:
       Planet( )   { x=y=rx=ry=radius=0; }
       ~Planet( )  { delete Buffer; }
       Planet(constint,constint,constint,constfloat);

       void Show( );
       void Move( );
       void ShowOrbit( );
       void GetNextPosition( );
 };

 /*************************************************************************///-----------------------------  Planet( )  -----------------------------///*************************************************************************/

 Planet::Planet(constint _rx,constint _ry,constint _radius,
                         constfloat _displacement)
 {
    rx=_rx;
    ry=_ry;

    x=(319+rx);
    y=240;

    radius=_radius;
    displacement=_displacement;
    position=0;
 }

 /*************************************************************************///----------------------------  ShowOrbit( )  ---------------------------///*************************************************************************/void Planet::ShowOrbit( )
 {
    setcolor(1);
      ellipse(320,240,0,360,rx,ry);
 }

 /*************************************************************************///--------------------------------  Show( )  ----------------------------///*************************************************************************/void Planet::Show( )
 {
    color=(random(14)+8);

    if(color==8 || color==14 || color==16)
       color=(random(6)+1);

    Buffer=newchar[imagesize((x-(radius+3)),(y-(radius+3)),
                        (x+radius+3),(y+radius+3))];

    getimage((x-(radius+3)),(y-(radius+3)),(x+radius+3),(y+radius+3),Buffer);

    setcolor(color);
    setfillstyle(1,color);
      pieslice(x,y,0,360,radius);

    setcolor(11);
      circle(x,y,radius);
 }

 /*************************************************************************///--------------------------------  Move  -------------------------------///*************************************************************************/void Planet::Move( )
 {
    putimage((x-(radius+3)),(y-(radius+3)),Buffer,0);

    delete Buffer;

    GetNextPosition( );

    Buffer=newchar[imagesize((x-(radius+3)),(y-(radius+3)),
                    (x+(radius+3)),(y+(radius+3)))];

    getimage((x-(radius+3)),(y-(radius+3)),(x+(radius+3)),
                        (y+(radius+3)),Buffer);

    setcolor(color);
    setfillstyle(1,color);
      pieslice(x,y,0,360,radius);

    setcolor((15-color));
      circle(x,y,radius);
 }

 /*************************************************************************///-------------------------  GetNextPosition( )  ------------------------///*************************************************************************/void Planet::GetNextPosition( )
 {
    float angle=(displacement*position);

    x=(int)(rx*cosl(angle)+320+0.5);
    y=(int)(ry*sinl(angle)+240+0.5);

    if((y==240 || y==239) && x>320)
       position=1;

    position++;
 }

 /*************************************************************************//*************************************************************************//*******************************  main( )  *******************************//*************************************************************************//*************************************************************************/void main( )
 {
    int driver=VGA;
    int mode=VGAHI;
    int error_code;

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

    error_code=graphresult( );

    if(error_code!=grOk)
    {
       restorecrtmode( );
       textmode(BW80);
       clrscr( );

       cout<<" \n Fatal Error  : Graphic Driver not initialized"<<endl;
       cout<<" Error Reason : "<<grapherrormsg(error_code)<<endl;
       cout<<" \n Press any key to exit...";

       getch( );
       exit(1);
    }

    setlinestyle(0,0,3);

    setcolor(7);
      rectangle(0,0,getmaxx( ),getmaxy( ));

    setlinestyle(0,0,0);

    for(int count=0;count<5000;count++)
       putpixel(random(640),random(480),random(15));

    settextstyle(2,0,7);
      setcolor(9);
    outtextxy(15,10,"Solar");
    outtextxy(16,10,"Solar");
    outtextxy(16,11,"Solar");

      setcolor(3);
    outtextxy(72,10,"System");
    outtextxy(73,10,"System");
    outtextxy(73,11,"System");

      setcolor(11);
    outtextxy(22,27,"Simulation");
    outtextxy(23,27,"Simulation");
    outtextxy(23,28,"Simulation");

    setcolor(7);
    settextstyle(2,0,4);
      outtextxy(10,465,"Press any key to exit.");

    setcolor(14);
    setfillstyle(1,14);
      pieslice(320,240,0,360,12);

   setcolor(12);
     circle(320,240,12);

   setcolor(4);
     circle(320,240,13);

    Planet Mercury(50,30,8,0.0175);
    Planet Venus(80,55,8,0.0155);
    Planet Earth(110,80,8,0.0135);
    Planet Mars(140,105,8,0.0115);
    Planet Jupiter(170,130,8,0.0095);
    Planet Saturn(200,155,8,0.0075);
    Planet Uranus(230,180,8,0.0055);
    Planet Neptune(260,205,8,0.0035);
    Planet Pluto(290,230,8,0.0015);

    Mercury.ShowOrbit( );
    Mercury.Show( );

    Venus.ShowOrbit( );
    Venus.Show( );

    Earth.ShowOrbit( );
    Earth.Show( );

    Mars.ShowOrbit( );
    Mars.Show( );

    Jupiter.ShowOrbit( );
    Jupiter.Show( );

    Saturn.ShowOrbit( );
    Saturn.Show( );

    Uranus.ShowOrbit( );
    Uranus.Show( );

    Neptune.ShowOrbit( );
    Neptune.Show( );

    Pluto.ShowOrbit( );
    Pluto.Show( );

    do
    {
       delay(25);

       Mercury.Move( );
       Venus.Move( );
       Earth.Move( );
       Mars.Move( );
       Jupiter.Move( );
       Saturn.Move( );
       Uranus.Move( );
       Neptune.Move( );
       Pluto.Move( );
    }
    while(!kbhit( ));

    getch( );
    closegraph( );
 }
  
Share: 


Didn't find what you were looking for? Find more on Program to show the simulation of the Solar System Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to show the simulation of the Solar System 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

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

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

 
Shivang Kumar from Australia Comment on: Mar 15
Sir,
I.m trying to make a basic C++ program in which i want to implement basic animations of shapes. I initially thought of implementing it by drawing the image in white, setting a delay of a few milliseconds and then redrawing the image in black. But, the output shows the rectangle flashing. What other method can i use to implement animations?

View All Comments