Skip to content

Commit 81b3e4d

Browse files
committed
10/19/2019 proj0
1 parent 641d7d7 commit 81b3e4d

30 files changed

+300
-4
lines changed

lab/lab2/.idea/workspace.xml

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proj/proj0/.idea/$PRODUCT_WORKSPACE_FILE$

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proj/proj0/.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proj/proj0/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proj/proj0/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proj/proj0/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proj/proj0/.vscode/launch.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "CodeLens (Launch) - NBody",
10+
"request": "launch",
11+
"mainClass": "NBody"
12+
},
13+
{
14+
"type": "java",
15+
"name": "CodeLens (Launch) - BasicInDemo",
16+
"request": "launch",
17+
"mainClass": "BasicInDemo"
18+
},
19+
{
20+
"type": "java",
21+
"name": "Debug (Launch) - Current File",
22+
"request": "launch",
23+
"mainClass": "${file}"
24+
},
25+
{
26+
"type": "java",
27+
"name": "Debug (Launch)-LeapYear",
28+
"request": "launch",
29+
"mainClass": "LeapYear"
30+
}
31+
]
32+
}

proj/proj0/In.class

8.43 KB
Binary file not shown.

proj/proj0/NBody.class

2.38 KB
Binary file not shown.

proj/proj0/NBody.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
public class NBody {
3+
4+
public static double readRadius(String file) {
5+
In in = new In(file);
6+
in.readDouble();
7+
double universe_r = in.readDouble();
8+
return universe_r;
9+
}
10+
11+
public static Planet[] readPlanets(String file) {
12+
In in = new In(file);
13+
int count_planets = in.readInt();
14+
in.readDouble();
15+
Planet[] planets_array = new Planet[count_planets];
16+
for (int i =0; i < count_planets; i++) {
17+
double xxPos = in.readDouble();
18+
double yyPos = in.readDouble();
19+
double xxVel = in.readDouble();
20+
double yyVel = in.readDouble();
21+
double mass = in.readDouble();
22+
String imgFileName = in.readString();
23+
planets_array[i] = new Planet(xxPos, yyPos, xxVel, yyVel, mass, imgFileName);
24+
}
25+
return planets_array;
26+
}
27+
public static void main(String[] args) {
28+
29+
// Collecting All Needed Input
30+
double T = Double.valueOf(args[0].toString());
31+
double dt = Double.valueOf(args[1].toString());
32+
String filename = args[2];
33+
double universe_r = readRadius(filename);
34+
String universe_pic = "./images/starfield.jpg";
35+
36+
// Drawing the Background
37+
StdDraw.setScale(-1 * universe_r, universe_r);
38+
StdDraw.picture(0, 0, universe_pic, 2 * universe_r, 2 * universe_r);
39+
Planet[] planets = readPlanets(filename);
40+
for (Planet planet : planets) {
41+
planet.draw();
42+
}
43+
44+
45+
StdDraw.enableDoubleBuffering();
46+
47+
// For each time through the loop, do the following:
48+
for (double running_time = 0; running_time < T; running_time += dt) {
49+
50+
// Calculate the net x and y forces for each planet, storing these in the xForces and yForces arrays respectively.
51+
double[] xForces = new double[planets.length];
52+
double[] yForces = new double[planets.length];
53+
// Create an xForces array and yForces array.
54+
for (int i = 0; i <planets.length; i++) {
55+
xForces[i] = planets[i].calcNetForceExertedByX(planets);
56+
yForces[i] = planets[i].calcNetForceExertedByY(planets);
57+
}
58+
59+
// Call update on each of the planets. This will update each planet’s position, velocity, and acceleration.
60+
for (int i = 0; i <planets.length; i++) {
61+
planets[i].update(dt, xForces[i], yForces[i]);
62+
}
63+
// Draw the background image.
64+
StdDraw.picture(0, 0, universe_pic, 2 * universe_r, 2 * universe_r);
65+
// Draw all of the planets.
66+
for (Planet planet : planets) {
67+
planet.draw();
68+
}
69+
// Show the offscreen buffer.
70+
StdDraw.show();
71+
// Pause the animation for 10 milliseconds
72+
StdDraw.pause(10);
73+
}
74+
// print out the final state of the universe in the same format as the input
75+
StdOut.printf("%d\n", planets.length);
76+
StdOut.printf("%.2e\n", universe_r);
77+
for (int i = 0; i < planets.length; i++) {
78+
StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
79+
planets[i].xxPos, planets[i].yyPos, planets[i].xxVel,
80+
planets[i].yyVel, planets[i].mass, planets[i].imgFileName);
81+
}
82+
83+
}
84+
85+
}

0 commit comments

Comments
 (0)