各位达人帮忙找错误:)
这是一个关于行星绕恒星的c程序,我用c++builder改了编译后无法绕出运动轨迹
望各位大哥帮我找找错误
注:px为行星在x轴上的速度,s[]为行星在y轴上的速度
x,y为行星运行的轨迹,m为行星速度,(b=GMm但行星能否绕出轨迹好象与这无关)
#include<d:\tc\ccbp\ccbp.h>
main()
{
float px,py,b,x,y,t,m;
int s[5];
int j;
long int graphdriver=DETECT,graphmode,i;
initgraph(&graphdriver,&graphmode,"");
setfillstyle(1,12);
fillellipse(200,220,15,15);
s[0]=800;
s[1]=1000;
s[2]=1200;
s[3]=1500;
s[4]=-1500;
for (j=0;j<5;j++)
{
t=0.1;m=1000;px=0;py=s[j];x=100;y=220;b=100000;
for(i=1;i<15000;i++)
{
px=px-(b*(x-200)*t)/sqrt(pow((x-200)*(x-200)+(y-220)*(y-220),3));
py=py-(b*(y-220)*t)/sqrt(pow((x-200)*(x-200)+(y-220)*(y-220),3));
x=x+(px*t)/m;
y=y+(py*t)/m;
fillellipse(x,y,5,5);
setfillstyle(1,2);
delay(20);
}
}
getch();
closegraph();
}
我用c++builder改写的程序
image1为64X64的图片(恒星),image2为32X32的图片(行星)
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
signed int a=0,x=-200,y=0;
float Px,Py,m;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Form1->Left=0;
Form1->Top=0;
Form1->Width=Screen->Width;
Form1->Height=Screen->Height;
Form1->Button1->Top=Screen->Height-100;
Form1->Button2->Top=Screen->Height-50;
Form1->Button1->Left=Screen->Width-70;
Form1->Button2->Left=Screen->Width-70;
Form1->Edit1->Top=Screen->Height-100;
Form1->Edit2->Top=Screen->Height-50;
Form1->Edit1->Left=Screen->Width-150;
Form1->Edit2->Left=Screen->Width-150;
Form1->Label1->Top=Screen->Height-100;
Form1->Label2->Top=Screen->Height-50;
Form1->Label1->Left=Screen->Width-230;
Form1->Label2->Left=Screen->Width-230;
Form1->Image1->Left=Screen->Width/2-32;
Form1->Image1->Top=Screen->Height/2-32;
Form1->Image2->Left=Screen->Width/2-200-16;
Form1->Image2->Top=Screen->Height/2-16;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
float B;
if(a==1)
{
B=100*m;
Px-=B*x/pow(x*x+y*y,1.5);
Py-=B*y/pow(x*x+y*y,1.5);
x=x+Px/m;
y=y+Py/m;
Form1->Image2->Left=Screen->Width/2+x-16;
Form1->Image2->Top=Screen->Height/2+y-16;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
a=1;
Form1->Image1->Left=Screen->Width/2-32;
Form1->Image1->Top=Screen->Height/2-32;
Form1->Image2->Left=Screen->Width/2-200-16;
Form1->Image2->Top=Screen->Height/2-16;
Py=StrToFloat(Form1->Edit1->Text);
m=StrToFloat(Form1->Edit2->Text);
Px=0;
x=-200;
y=0;
}