forked from catwood16/projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbody.java
138 lines (119 loc) · 4.09 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import princeton.StdAudio;
import princeton.StdDraw;
public class NBody extends Application {
public static final double G = 6.67E-11;
/**
* returns Euclidean distance between (x1, y1) and (x2, y2)
*
* @param x1
* x-coordinate of point 1
* @param y1
* y-coordinate of point 1
* @param x2
* x-coordinate of point 2
* @param y2
* y-coordinate of point 2
* @return Euclidean distance between (x1, y1) and (x2, y2)
*/
public double distance(double x1, double y1, double x2, double y2) {
//TODO: Complete distance
double sqrr = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1);
double r = Math.sqrt(sqrr);
return r;
}
/**
* return the magnitude of the gravitational force between two bodies of
* mass m1 and m2 that are distance r apart
*
* @param m1
* mass of body 1 in kg
* @param m2
* mass of body 2 in kg
* @param r
* distance in m
* @return force between m1 and m2 that are distance r apart
*/
public double force(double m1, double m2, double r) {
//TODO: Complete force
double force = G*m1*m2/(r*r);
return force;
}
/**
* Returns the x positions and y positions of bodies
* @param totalTime The total amount of universe time to run for
* @param timeStep The value of delta t in the equations to calculate position
* @param info The scanner with info about the initial conditions of the
* bodies
* @return an array whose first element is the x positions of the bodies,
* and whose second element is the y positions of the bodies at time
* t = totalTime
*/
public double[][] positions(Scanner info, int totalTime, int timeStep) {
//TODO: Complete positions
int N = info.nextInt();
double UR = info.nextDouble();
double[] xpos = new double[N-1];
for (int i = 0; i < xpos.length; i++){
xpos[0] = info.;
}
System.out.println("" + N);
System.out.println("" + UR);
double[][] output = new double[0][2]; //Replace 0 with the number of
//bodies, read from the file
return output;
}
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
Scanner info = openFile();
int time = 10000000;
int dt = 25000;
if (info != null) {
//StdAudio.play("data/2001.mid");
NBody myNBody = new NBody();
double[][] results = myNBody.positions(info, time, dt);
for(int i = 0; i < results.length; i++) {
for(int j = 0; j < results[i].length; j++) {
System.out.print(results[i][j]+" ");
}
System.out.println();
}
StdDraw.clear();
StdAudio.close();
}
}
/**
* Displays file chooser for browsing in the project directory. and opens
* the selected file
*
* @return a new Scanner that produces values scanned from the selected
* file. null if file could not be opened or was not selected
*/
public static Scanner openFile() {
System.out.println("Opening file chooser");
FileChooser chooser = new FileChooser();
chooser.setTitle("Open Configuration File");
chooser.setInitialDirectory(new File("data"));
File file = chooser.showOpenDialog(new Stage());
if (file != null) {
try {
System.out.println("Opening: " + file.getName() + "");
return new Scanner(file);
}
catch (FileNotFoundException fnf) {
throw new RuntimeException(fnf);
}
}
else {
System.out.println("File open canceled");
return null;
}
}
}