-
Notifications
You must be signed in to change notification settings - Fork 0
/
NBody.java
70 lines (63 loc) · 2.06 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
public class NBody {
public static double readRadius(String file)
{
In in = new In(file);
in.readDouble();
return in.readDouble();
}
public static Planet [] readPlanets(String file)
{
In in = new In(file);
Planet [] ps = new Planet[in.readInt()];
in.readDouble();
for(int i=0; i<ps.length; ++i)
{
ps[i] = new Planet(in.readDouble(),in.readDouble(), in.readDouble(),
in.readDouble(), in.readDouble(), in.readString());
}
return ps;
}
public static void main (String [] args)
{
Double T = Double.parseDouble(args[0]),
dt = Double.parseDouble(args[1]);
String filename = args[2];
double radius = readRadius(filename);
StdDraw.enableDoubleBuffering();
StdDraw.setScale(0-radius,radius);
StdDraw.clear();
StdDraw.picture(0,0, "images/starfield.jpg");
Planet [] ps = readPlanets(filename);
for(Planet p : ps)
{
p.draw();
}
double t = 0;
while(t < T)
{
double [] xForces = new double [ps.length];
double [] yForces = new double [ps.length];
for(int i=0; i < ps.length; ++i)
{
xForces[i] = ps[i].calcNetForceExertedByX(ps);
yForces[i] = ps[i].calcNetForceExertedByY(ps);
}
StdDraw.picture(0,0, "images/starfield.jpg");
for(int i=0; i<ps.length; ++i)
{
ps[i].update(dt, xForces[i], yForces[i]);
ps[i].draw();
}
StdDraw.show();
StdDraw.pause(20);
t += dt;
}
StdOut.printf("%d\n", ps.length);
StdOut.printf("%.2e\n", radius);
for (int i = 0; i < ps.length; i++) {
StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
ps[i].xxPos, ps[i].yyPos, ps[i].xxVel,
ps[i].yyVel, ps[i].mass, ps[i].imgFileName);
}
}
};