-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNBody.java
73 lines (67 loc) · 2.54 KB
/
NBody.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class NBody {
private static int N;
private static double R;
public static double readRadius(String fileName) {
In in = new In(fileName);
N = in.readInt();
R = in.readDouble();
return R;
}
public static Planet[] readPlanets(String fileName) {
readRadius(fileName);
In in = new In(fileName);
Planet[] planets = new Planet[N];
in.readInt(); // 跳过行数
in.readDouble(); // 跳过半径
for (int i = 0; i < N; i++) {
//planets[i] = new Planet();
planets[i] = new Planet(in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), in.readString());
// planets[i].xxPos = in.readDouble();
// planets[i].yyPos = in.readDouble();
// planets[i].xxVel = in.readDouble();
// planets[i].yyVel = in.readDouble();
// planets[i].mass = in.readDouble();
// planets[i].imgFileName = in.readString();
}
return planets;
}
private static String imageToDraw = "images/starfield.jpg";
public static void main(String[] args) {
StdDraw.enableDoubleBuffering();
double T = Double.valueOf(args[0]);
double dt = Double.valueOf(args[1]);
String filename = args[2];
Planet[] planets = readPlanets(filename);
StdDraw.setScale(-R, R);
StdDraw.clear();
// StdDraw.show();
// StdDraw.pause(2000);
double time = 0;
while (time <= T) {
StdDraw.clear();
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, imageToDraw);
for (Planet planet : planets) {
planet.draw();
}
StdDraw.show();
StdDraw.pause(10);
time += dt;
}
StdOut.printf("%d\n", planets.length);
StdOut.printf("%.2e\n", R);
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);
}
}
}