活跃农民
- 积分
- 822
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-9-13
- 最后登录
- 1970-1-1
|
刚刚开始上,之前卡在如何使用Git 这,后来发现用不用这个对我这个**没多大影响。果断先不管。
我也把作业交了吧。
class Planet{
public static double G = 6.67e-11;
public double x;
public double y;
public double xVelocity;
public double yVelocity;
public double mass;
public String img;
public double xNetForce;
public double yNetForce;
public double xAccel;
public double yAccel;
public Planet(double x_position, double y_position, double x_velocity,
double y_velocity, double mass, String img){
this.x = x_position;
this.y = y_position;
this.xVelocity = x_velocity;
this.yVelocity = y_velocity;
this.mass = mass;
this.img = img;
}
public double calcDistance(Planet p){
return Math.sqrt((this.x-p.x)*(this.x-p.x)+
(this.y-p.y)*(this.y-p.y));
}
public double calcPairwiseForce(Planet p){
double distance = this.calcDistance(p);
return G*this.mass*p.mass/(distance*distance);
}
public double calcPairwiseForceX(Planet p){
double pairwiseForce = this.calcPairwiseForce(p);
double distance = this.calcDistance(p);
return (p.x-this.x)/distance*pairwiseForce;
}
public double calcPairwiseForceY(Planet p){
double pairwiseForce = this.calcPairwiseForce(p);
double distance = this.calcDistance(p);
return (p.y-this.y)/distance*pairwiseForce;
}
public void setNetForce(Planet[] planets){
this.xNetForce=0;
this.yNetForce=0;
for(int i =0;i<planets.length;i++){
if(planets[i]!=this){
this.xNetForce+=calcPairwiseForceX(planets[i]);
this.yNetForce+=calcPairwiseForceY(planets[i]);
}
}
}
public void draw(){
StdDraw.picture(this.x,this.y,"images/"+this.img);
}
public void update(double dt){
this.xAccel = this.xNetForce/this.mass;
this.yAccel = this.yNetForce/this.mass;
this.xVelocity += this.xAccel*dt;
this.yVelocity += this.yAccel*dt;
this.x +=this.xVelocity*dt;
this.y +=this.yVelocity*dt;
}
}
class NBody{
public static void main(String[] args) {
double T;
double dt;
String filename;
int num;
double radius;
Planet[] planets;
double time =0 ;
T = Double.parseDouble(args[0]);
dt =Double.parseDouble(args[1]);
filename = args[2];
In in = new In(filename);
num = in.readInt();
radius = in.readDouble();
planets = new Planet[num];
StdDraw.setScale(-radius,radius);
StdDraw.picture(0,0,"images/starfield.jpg");
StdAudio.play("audio/2001.mid");
for(int i =0;i<num;i++){
planets[i] = getPlanet(in);
}
while(time<T){
StdDraw.picture(0,0,"images/starfield.jpg");
for(int j =0;j<num;j++){
planets[j].setNetForce(planets);
planets[j].update(dt);
planets[j].draw();
}
StdDraw.show(10);
time+=dt;
}
StdOut.printf("%d\n",num);
StdOut.printf("%.2e\n",radius);
for(int k=0;k<num;k++){
StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
planets[k].x,planets[k].y,planets[k].xVelocity,
planets[k].yVelocity,planets[k].mass,planets[k].img);
}
}
public static Planet getPlanet(In input){
double x = input.readDouble();
double y = input.readDouble();
double xVelocity = input.readDouble();
double yVelocity = input.readDouble();
double mass = input.readDouble();
String img = input.readString();
Planet p = new Planet(x,y,xVelocity,yVelocity,mass,img);
return p;
}
} |
|