-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadtree.java
More file actions
161 lines (136 loc) · 4.26 KB
/
Quadtree.java
File metadata and controls
161 lines (136 loc) · 4.26 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* File: Quadtree.java
* Date: 4/20/17
* Student: William Tyas
* ID: W01203451
* Class: CSCI 241, Spring 2017
* Description: A tree that stores objects for the purpose of optimization
*/
import java.util.*;
public class Quadtree {
private ArrayList<Sphere> intersectSpheres; //spheres associated with node
private cell cell; //cell describing
private Quadtree parent;
private Quadtree current;
private Quadtree ul, ur, ll, lr; //children
//constructor that generates an empty node
public Quadtree() {
this.cell = null;
this.intersectSpheres = new ArrayList<Sphere>();
this.parent = null;
this.current = this;
this.ul = null;
this.ur = null;
this.ll = null;
this.lr = null;
}
// Returns a tree referencing a cell and four empty subtrees
public Quadtree(cell cell) {
this.cell = cell;
this.intersectSpheres = new ArrayList<Sphere>();
this.parent = null;
this.ul = null;
this.ur = null;
this.ll = null;
this.lr = null;
}
// Returns a tree referencing four subtrees
public Quadtree(Quadtree ul, Quadtree ur, Quadtree ll, Quadtree lr) {
this(null, ul, ur, ll, lr);
}
// Returns a tree referencing value and four subtrees
public Quadtree(Sphere value, Quadtree ul, Quadtree ur, Quadtree ll, Quadtree lr) {
if (ul == null) {
this.ul = new Quadtree();
}
setUL(ul);
if (ur == null) {
this.ur = new Quadtree();
}
setUR(ur);
if (ll == null) {
this.ll = new Quadtree();
}
setLL(ll);
if (lr == null) {
this.lr = new Quadtree();
}
setLR(lr);
}
// returns reference to (possibly empty) ul subtree
public Quadtree ul() {
return this.ul;
}
// returns reference to (possibly empty) ur subtree
public Quadtree ur() {
return this.ur;
}
// returns reference to (possibly empty) ll subtree
public Quadtree ll() {
return this.ll;
}
// returns reference to (possibly empty) lr subtree
public Quadtree lr() {
return this.lr;
}
//returns reference to parent node, or null
public Quadtree parent() {
return this.parent;
}
public cell getCell() {
return this.cell;
}
// Set the cell of this quadtree
public void setCell(cell cell) {
this.cell = cell;
}
// Set the children of the current quadtree
public void setQuadtree(Quadtree newLL, Quadtree newUL, Quadtree newLR, Quadtree newUR) {
setLL(newLL);
setUL(newUL);
setLR(newLR);
setUR(newUR);
}
//re-parents this node to parent reference, or null
public void setParent(Quadtree newParent) {
if (newParent.getCell() == null) {
this.parent = newParent;
}
this.parent = newParent;
}
//post: sets ul subtree to newUL and re-parents newUL if not null
public void setUL(Quadtree newUL) {
if (newUL.getCell() == null) return;
this.ul = newUL;
this.ul.setParent(this);
}
//post: sets ur subtree to newUR and re-parents newUR if not null
public void setUR(Quadtree newUR) {
if (newUR.getCell() == null) return;
this.ur = newUR;
this.ur.setParent(this);
}
//post: sets ll subtree to newLL and re-parents newLL if not null
public void setLL(Quadtree newLL) {
if (newLL.getCell() == null) return;
this.ll = newLL;
this.ll.setParent(this);
}
//post: sets lr subtree to newLR and re-parents newLR if not null
public void setLR(Quadtree newLR) {
if (newLR.getCell() == null) return;
this.lr = newLR;
this.lr.setParent(this);
}
// returns spheres associated with this node
public ArrayList<Sphere> getSpheres() {
return this.intersectSpheres;
}
// sets the value associated with this node
public void setValue(Sphere value) {
this.intersectSpheres.add(value);
}
// Checks if quadtree is empty
public boolean isEmpty() {
return (this.parent == null && this.ul == null && this.ur == null && this.ll == null && this.lr == null);
}
}