PDA

View Full Version : حرفه ای: برنامه سیارات C++‎‎‎‎



hansolbook
جمعه 24 آذر 1391, 16:47 عصر
با سلام خدمت تمام دوستان
از شما عزیزان یک راهنمایی میخواستم یک برنامه است که من باید با استاد خود اراپه بدم برنامه شبیه سازی منظومه شمسی من برنامه رو دارم فقط از یک کارشناس میخواهم در حد 5 یا 6 خط توضیح بده که این برنامه مثلا از خط اول شروع میکنه چی کار میکنه حالا کامل هم نبود زیاد مهم نیس به نوعی حضوری برنامه رو من تحویل میخواهم بدم به استاد یه توضیحاتی بدم که برنامه چی کار میکنه چند تا متغییر داریم از اینا ... با سپاس فراوان از شما دوستان عزیز.

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

Ananas
جمعه 24 آذر 1391, 17:13 عصر
مرتب شده ی کد :

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

hansolbook
جمعه 24 آذر 1391, 17:16 عصر
من ننوشتم دوست عزیز باید اراپه بدم این برنامه رو

Ananas
جمعه 24 آذر 1391, 20:57 عصر
یک کلاس تعریف کرده برای هر سیاره که یک جابجایی داری x و y و فکر کنم rx و ry هم شعاع های افقی و عمودی هستن و position موقعیت معنی میده و منظورش مکان زاویه ایش هست نسبت به مرکز مسیر حرکتتش (مثلا خوشید) و displacement هم جابجایی هست احتمالا که اون هم مثل position مقدار زاویه ای هست که به position اضافه میشه و میشه گفت این مقدار تعیین کننده ی سرعت هست چون هر چی بیشتر باشه ، مقدار بیشتری به زاویه اضافه میشه در هر فریم انیمیشنی. برای نمایش هم با استفاده ازین مقادیر نمایش رو انجام میده. تابع ShowObrbit هم اگه اشتباه نکنم مسیر حرکت بیضی شکل رو ترسیم میکنه و Show هم خود سیاره رو.
و هر شی از این کلاس با استفاده از متدهای تعریف شده در کلاس و با پارامتر های مخصوص خود شی، توانایی اینکه :
-خودش رو جابجا کنه ، Move
-مسیر حرکتشو ترسیم کنه و ShowOrbit
-خودش رو ترسیم کنه و Show
-مکان بعدیش رو تعیین کنه GetNextPosition
رو داره.
بعد در تابع main یک سری سیاره با اسماشون ساخته (از کلاس مورد نظر اشیا ای ساخته) و در یک حلقه شروع کرده به حرکت سیاره ها و ترسیم اونها بعد ازهر حرکت (انیمیشن).
الان چیز خاص دیگه ای برای توضیح کلی به نظرم نمیاد بگم.