-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPlanet.java
98 lines (96 loc) · 2.93 KB
/
Planet.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
/**
* Planet
*/
public class Planet {
public double xxPos;
public double yyPos;
public double xxVel;
public double yyVel;
public double mass;
public String imgFileName;
public Planet(double xP, double yP, double xV, double yV, double m, String img) {
xxPos = xP;
yyPos = yP;
xxVel = xV;
yyVel = yV;
mass = m;
imgFileName = img;
}
// public Planet()
// {
// xxPos = 0;
// yyPos = 0;
// xxVel = 0;
// yyVel = 0;
// mass = 0;
// imgFileName = "";
// }
public Planet(Planet p) {
xxPos = p.xxPos;
yyPos = p.yyPos;
xxVel = p.xxVel;
yyVel = p.yyVel;
mass = p.mass;
imgFileName = p.imgFileName;
}
public double calcDistance(Planet that) {
double dX;
double dY;
dX = (this.xxPos - that.xxPos);
dY = (this.yyPos - that.yyPos);
return Math.pow((dX * dX + dY * dY), 0.5);
}
public double calcForceExertedBy(Planet that) {
return (6.67 * Math.pow(10, -11) * this.mass * that.mass) / (Math.pow((this.calcDistance(that)), 2));
}
public double calcForceExertedByX(Planet that) {
double dX = that.xxPos - this.xxPos;
// if (dX > 0) {
// return this.calcForceExertedBy(that) * dX / this.calcDistance(that);
// } else {
// return -1.0 * this.calcForceExertedBy(that) * dX / this.calcDistance(that);
// }
return this.calcForceExertedBy(that) * dX / this.calcDistance(that);
}
public double calcForceExertedByY(Planet that) {
double dY = that.yyPos - this.yyPos;
// if (dY > 0) {
// return this.calcForceExertedBy(that) * dY / this.calcDistance(that);
// } else {
// return -1.0 * this.calcForceExertedBy(that) * dY / this.calcDistance(that);
// }
return this.calcForceExertedBy(that) * dY / this.calcDistance(that);
}
public double calcNetForceExertedByX(Planet[] planets) {
double totalForce = 0.0;
for (Planet planet : planets) {
if (this.equals(planet)) {
continue;
}
totalForce += this.calcForceExertedByX(planet);
}
return totalForce;
}
public double calcNetForceExertedByY(Planet[] planets) {
double totalForce = 0.0;
for (Planet planet : planets) {
if (this.equals(planet)) {
continue;
}
totalForce += this.calcForceExertedByY(planet);
}
return totalForce;
}
public void update(double s, double xN, double yN) {
double xAcc = xN / mass;
double yAcc = yN / mass;
xxVel += xAcc * s;
yyVel += yAcc * s;
xxPos += xxVel * s;
yyPos += yyVel * s;
}
public void draw() {
StdDraw.picture(xxPos, yyPos, "images/" + imgFileName);
return;
}
}