cs61b Project0:NBody Simulation
1.Planet.java代码
public class Planet{
public double xxPos;
public double yyPos;
public double xxVel;
public double yyVel;
public double mass;
public String imgFileName;
private static final double G = 6.67e-11;
public Planet(double xP,double yP,double xV,double yV,double m,String img){
xxPos = xP;
yyPos = yP;
xxVel = xV;
yyVel = yV;
mass = m;
imgFileName = img;
}
public Planet(Planet p){
this(p.xxPos,p.yyPos,p.xxVel,p.yyVel,p.mass,p.imgFileName);
}public double calcDistance(Planet p){
return Math.pow((Math.pow((this.xxPos-p.xxPos),2)+Math.pow((this.yyPos-p.yyPos),2)),1.0/2);
}public double calcForceExertedBy(Planet p){
return (this.mass*p.mass*G)/Math.pow(this.calcDistance(p),2);
}public double calcForceExertedByX(Planet p){
return this.calcForceExertedBy(p)*(p.xxPos-this.xxPos)/this.calcDistance(p);
}public double calcForceExertedByY(Planet p){
return this.calcForceExertedBy(p)*(p.yyPos-this.yyPos)/this.calcDistance(p);
}public double calcNetForceExertedByX(Planet[] p){
double NetExertedByX=0;
for(int i=0;
i【cs61b Project0:NBody Simulation】
2.NBody.java代码
public class NBody{public static double readRadius(String path){
In in =new In(path);
int firstinteger=in.readInt();
double ReadRadius=in.readDouble();
return ReadRadius;
}public static Planet[] readPlanets(String path){
In in=new In(path);
int numbersum=in.readInt();
Planet[] p =new Planet[numbersum];
double ReadRadius=in.readDouble();
for(int i=0;
i
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- cs61b week8 -- Binary Search Tree
- cs61b week8 -- Disjoint Sets
- cs61b week7 -- Asymptotics ||
- cs61b week7 -- Asymptotics I
- cs61b|cs61b week6 -- Packages and Access Control
- cs61b week5 -- Object Methods
- cs61b week5 -- Exceptions, Iterators, Iterables
- cs61b week5 -- Generics, Autoboxing
- cs61b week3 -- Subtype Polymorphism vs. HoFs
- cs61b week3--Extends, Casting, Higher Order Functions