-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.java
128 lines (109 loc) · 4.45 KB
/
Node.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
import java.util.Random;
public class Node {
public double topLeftX;
public double topLeftY;
public double height;
public double width;
public String id;
public int level;
public Node[] quadrant;
public Planet planet;
public double centerX;
public double centerY;
public double mass;
public double xcomp;
public double ycomp;
public Random rand;
public Node(double x, double y, double height, double width, String id, int level){
this.id = id;
this.level = level;
this.topLeftX = x;
this.topLeftY = y;
this.height = height;
this.width = width;
quadrant = new Node[4];
for(int i = 0; i < 4; i++){
quadrant[i] = null;
}
centerX = -1;
centerY = -1;
mass = 0;
planet = null;
xcomp = 0;
ycomp = 0;
rand = new Random();
}
public void addPlanet(Planet planet){ // add(Planet newPlanet)
// if a planet "passes through" this node, it's mass should be included in this node's total
this.mass += planet.mass;
this.xcomp += planet.mass * planet.getX();
this.ycomp += planet.mass * planet.getY();
if(!this.hasPlanet() && !this.hasChildren()){ // does it have child nodes?
// no planet in node and no child nodes -> add planet to node
this.planet = planet;
//System.out.println(this.planet.toString());
}
else{
// planet already in node -> split node into 4, add planets to the corresponding nodes
if (!this.hasChildren()) {
quadrant[0] = new Node(topLeftX, topLeftY, height/2, width/2, id + "-0", this.level + 1);
quadrant[1] = new Node(topLeftX + width/2, topLeftY, height/2, width/2, id + "-1", this.level + 1);
quadrant[2] = new Node(topLeftX + width/2, topLeftY + height/2, height/2, width/2, id + "-2", this.level + 1);
quadrant[3] = new Node(topLeftX, topLeftY + height/2, height/2, width/2, id + "-3", this.level + 1);
}
// If there is a planet in this node already, send it to the children
if(this.hasPlanet()){
if(planet.getX() == this.planet.getX()
&& planet.getY() == this.planet.getY())
{
double snX = rand.nextDouble() * 4 - 2;
double snY = rand.nextDouble() * 4 - 2;
planet.setThisXY(planet.getX()+snX, planet.getY()+snY);
}
sendPlanet(this.planet);
this.planet = null;
}
// Figure out which quadrant a new planet goes to and put it there
sendPlanet(planet);
}
this.centerX = this.xcomp / this.mass;
this.centerY = this.ycomp / this.mass;
}
public boolean hasPlanet(){
return this.planet != null;
}
public boolean hasChildren(){
return this.quadrant[0] != null;
}
public void sendPlanet(Planet planet){
if (planet.getX() < (topLeftX + (width / 2))) {
// left of vertical line -> quadrant 1 or 4
if (planet.getY() < (topLeftY + (height / 2))) {
// above horizontal line -> quadrant 1
quadrant[0].addPlanet(planet);
//System.out.println("Sending planet " + planet.id + " to q1");
}
else if(planet.getY() < (topLeftY + height)){
// below horizontal line -> quadrant 4
quadrant[3].addPlanet(planet);
//System.out.println("Sending planet " + planet.id + " to q4");
}
return;
}
else if(planet.getX() < topLeftX + width){
// right of vertical line -> quadrant 2 or 3
if (planet.getY() < (topLeftY + (height / 2))) {
// above horizontal line -> quadrant 2
quadrant[1].addPlanet(planet);
//System.out.println("Sending planet " + planet.id + " to q2");
}
else if(planet.getY() < (topLeftY + height)){
// below horizontal line -> quadrant 3
quadrant[2].addPlanet(planet);
//System.out.println("Sending planet " + planet.id + " to q3");
}
return;
}
System.err.println("Can't send planet. Something is weird with it. " + planet.toString());
}
}