-
Notifications
You must be signed in to change notification settings - Fork 0
/
NBody.java
78 lines (60 loc) · 1.85 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
74
75
76
77
78
import java.util.*;
// import Planet.java;
public class NBody{
public static double readRadius(String afile){
In in = new In(afile);
int numberOfPlanets = in.readInt();
double radius = in.readDouble();
return radius;
}
public static Planet[] readPlanets(String afile){
In in = new In(afile);
in.readInt();
in.readDouble();
Planet[] allPlanets = new Planet[5];
for(int i = 0; i < allPlanets.length; i++){
double xxPos = in.readDouble();
double yyPos = in.readDouble();
double xxVel = in.readDouble();
double yyVel = in.readDouble();
double mass = in.readDouble();
String name = in.readString();
Planet aPlanet = new Planet(xxPos,yyPos,xxVel,yyVel,mass,name);
allPlanets[i] = aPlanet;
}
return allPlanets;
}
public static void main(String[] args){
double T = Double.parseDouble(args[0]);
double dt = Double.parseDouble(args[1]);
String filename = args[2];
double radiusOfUniverse = readRadius(filename);
Planet[] allPlanets = readPlanets(filename);
String imageToDraw = "images/starfield.jpg";
StdDraw.setScale(-2 * radiusOfUniverse, 2 * radiusOfUniverse);
StdDraw.picture(0, 0, imageToDraw);
StdDraw.show();
for(Planet aPlanet : allPlanets){
aPlanet.draw();
}
/** prevent flickering in the animation*/
StdDraw.enableDoubleBuffering();
int atime = 0;
while (atime != T){
Double[] xForces = new Double[allPlanets.length];
Double[] yForces = new Double[allPlanets.length];
for(int i=0; i < allPlanets.length; i++){
Planet aPlanet = allPlanets[i];
xForces[i] = aPlanet.calcNetForceExertedByX(allPlanets);
yForces[i] = aPlanet.calcNetForceExertedByY(allPlanets);
aPlanet.update(atime, xForces[i], yForces[i]);
}
StdDraw.picture(0, 0, imageToDraw);
StdDraw.show();
for(Planet aPlanet: allPlanets)
aPlanet.draw();
StdDraw.pause(200);
atime += dt;
}
}
}