-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanet.java
More file actions
executable file
·102 lines (69 loc) · 2.05 KB
/
Planet.java
File metadata and controls
executable file
·102 lines (69 loc) · 2.05 KB
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
import java.awt.Color;
/*
Brendan DeMilt, Chris Pan
Period: 8
Stores celestial body information
*/
public class Planet{
private double x,y,vx,vy,fx,fy,m;
private double G = 6.67e-11;
//initializes planet
public Planet(double xe, double ye, double vex, double vey, double mass){
x = xe;
y = ye;
vx = vex;
vy = vey;
m = mass;
}
public double getMass(){
return this.m;
}
//calculates euclidean distance between bodies
public double distance(Planet b){
double dx = b.x-this.x;
double dy = b.y-this.y;
return Math.sqrt(dx*dx+dy*dy);
}
//returns body position coordinates
public double[] getPos(){
double[] pos = {this.x,this.y};
return pos;
}
//adds to the net force if the body
public void addForce(Planet p){
//this double ensures the planets don't get close enough to go haywire
double setDistance = 9e3;
if(this!=p){
double f = (G*this.m*p.m)/((distance(p)*distance(p) + setDistance*setDistance));
fx += f*(p.x-this.x)/distance(p);
fy += f*(p.y-this.y)/distance(p);}
}
//used if this object is inside an internal node, updates its center of mass
public Planet newCOM(Planet p){
double newMass = this.m + p.m;
double newX = ((this.x*this.m)+ (p.x*p.m))/newMass;
double newY = ((this.y*this.m)+ (p.y*p.m))/newMass;
return new Planet(newX,newY, 0,0,newMass);
}
//determines if a planet is inside of a square
public boolean isIn(Square s){
return s.contains(this);
}
//applies net force to accelerate the planet in a given direction
public void updatebody(double t){
this.vx+= fx*t/this.m;
this.vy+= fy*t/this.m;
this.x += vx*t;
this.y += vy*t;
}
//resets net force
public void resetForce(){
this.fx = 0;
this.fy = 0;
}
//draws the planet
public void draw(){
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.point(this.x,this.y);
}
}