讲义:NBody Simulation | CS 61B Spring 2018
为什么使用Git命令+javac??
答:
这样可以单独的编译和运行某一个程序,而IDE编译一个程序的时候还会编译其它的程序,会很烦人
翻译时绕过数学公式
goole浏览器按F12进入网页控制台,输入$('math, .math, .MathJax').attr('translate','no');
javac编译的时候注意事项
javac -encoding UTF-8 Planet.java TestCalcForceExertedBy.java //有中文
javac TestCalcForceExertedBy.java //无中文
增强的for循环
public class EnhancedForBreakDemo {
public static void main(String[] args) {
String[] a = {"cat", "dog", "laser horse", "ketchup", "horse", "horbse"};
for (String s : a) {
for (int j = 0; j < 3; j += 1) {
System.out.println(s);
if (s.contains("horse")) {
break;
}
}
}
}
}
可视化代码,超好用
Online Java Compiler, Visual Debugger, and AI Tutor - Learn Java programming by visualizing code
Planets类
import java.security.PublicKey;
public class Planet {
public double xxPos;
public double yyPos;
public double xxVel;
public double yyVel;
public double mass;
public String imgFileName;
public Planet(double xP,double yP,double xV,double yV,double m,String img){
this.xxPos=xP;
this.yyPos=yP;
this.xxVel=xV;
this.yyVel=yV;
this.mass=m;
this.imgFileName=img;
}
// public Planet(){
// this(0,0,0,0,0,null);
// }
public Planet(Planet p){
this(p.xxPos,p.yyPos,p.xxVel,p.yyVel,p.mass,p.imgFileName);
}
/*计算两个物体之间的距离的平方*/
public double calcDistance(Planet other){
return Math.sqrt((this.xxPos-other.xxPos)*(this.xxPos- other.xxPos)+(this.yyPos-other.yyPos)*(this.yyPos-other.yyPos));
}
/*计算两个物体之间的受力,牛顿万有引力*/
public double calcForceExertedBy(Planet other){
double G=6.67e-11;
return (G*(this.mass*other.mass))/(this.calcDistance(other)*this.calcDistance(other));
}
//this代表受力的物体,other代表施力的物体
//X分力
public double calcForceExertedByX(Planet other){
double F=this.calcForceExertedBy(other);
double r=this.calcDistance(other);
return F*(other.xxPos-this.xxPos)/r;
}
//Y方向上的分力
public double calcForceExertedByY(Planet other){
double F=this.calcForceExertedBy(other);
double r=this.calcDistance(other);
return F*(other.yyPos-this.yyPos)/r;
}
//计算受力物体受其他物体X方向上的力
public double calcNetForceExertedByX(Planet[] planets){
double xNetForce=0.0;
for(Planet p:planets){
if(!this.equals(p))
xNetForce+=this.calcForceExertedByX(p);
}
return xNetForce;
}
//计算受力物体受其它物体Y方向的力
public double calcNetForceExertedByY(Planet[] planets){
double yNetForce=0;
for(Planet p:planets){
if(!this.equals(p))
yNetForce+=this.calcForceExertedByY(p);
}
return yNetForce;
}
//更新速度,牛顿第二定理
//已知x,y方向的分力以及作用的时间,求出变化后的速度
public void update(double time,double xForce,double yForce){
//由F=ma先求出a,然后v=v0+a*t;
double ax=xForce/this.mass;
double ay=yForce/this.mass;
this.xxVel=this.xxVel+ax*time;
this.yyVel=this.yyVel+ay*time;
//讲义给的公式:p_new=p_old+v_new*dt
//我高中学的:p_new=p_old+0.5*a*t*t;算了按讲义来吧
this.xxPos=this.xxPos+this.xxVel*time;
this.yyPos=this.yyPos+this.yyVel*time;
}
public void draw(){
StdDraw.picture(this.xxPos,this.yyPos,"images/"+this.imgFileName);
}
}
public class NBody {
//返回宇宙的半径
public static double readRadius(String Filename){
In in=new In(Filename);
int N=in.readInt();
return in.readDouble();
}
//返回行星数组
public static Planet[] readPlanets(String Filename){
In in=new In(Filename);
int N=in.readInt();
Planet[] planets=new Planet[N];
double radius=in.readDouble();
for(int i=0;i<N;i++){
double xxPos =in.readDouble() ,
yyPos = in.readDouble(),
xxVel = in.readDouble(),
yyVel = in.readDouble(),
mass =in.readDouble();
String imageFile=in.readString();
Planet p=new Planet(xxPos,yyPos,xxVel,yyVel,mass,imageFile);
planets[i]=p;
}
return planets;
}
public static void main(String[] args){
double T=Double.parseDouble(args[0]);
double dt=Double.parseDouble(args[1]);
String filename="./"+args[2];
Double Radius=readRadius(filename);
Planet[] planets=readPlanets(filename);
int N=planets.length;
StdDraw.setXscale(-Radius,Radius);
StdDraw.setYscale(-Radius,Radius);
StdDraw.enableDoubleBuffering();
//StdDraw.picture(0,0,"images/starfield.jpg");
//for(int i=0;i<N;i++)
// planets[i].draw();
double t=0;
while(t<=T){
double[] xForces=new double[N];
double[] yForces=new double[N];
for(int i=0;i<N;i++){
xForces[i]=planets[i].calcNetForceExertedByX(planets);
yForces[i]=planets[i].calcNetForceExertedByY(planets);
}
for (int i=0;i<N;i++){
planets[i].update(dt,xForces[i],yForces[i]);
}
StdDraw.picture(0,0,"images/starfield.jpg");
for(int i=0;i<N;i++){
planets[i].draw();
}
StdDraw.show();
StdDraw.pause(10);
t+=dt;
}
StdOut.printf("%d\n", planets.length);
StdOut.printf("%.2e\n", Radius);
for (int i = 0; i < planets.length; i++) {
StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
planets[i].xxPos, planets[i].yyPos, planets[i].xxVel,
planets[i].yyVel, planets[i].mass, planets[i].imgFileName);
}
}
}
恰当的位置绘制行星于背景
StdDraw.setXscale(-Radius,Radius);
StdDraw.setYscale(-Radius,Radius);
StdDraw.picture(0,0,"images/starfield.jpg");
for(int i=0;i<N;i++)
planets[i].draw();
标签:yyPos,xxPos,double,Project0,Planet,other,simulation,public,Nbody
From: https://www.cnblogs.com/ahnultq/p/18165205