-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadrantTree.java
More file actions
201 lines (167 loc) · 5.57 KB
/
Copy pathQuadrantTree.java
File metadata and controls
201 lines (167 loc) · 5.57 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
public class QuadrantTree {
private QTreeNode root;
/**
* Creates a QuadrantTree object with the given pixels.
*
* @param thePixels the 2D array of pixels
*/
public QuadrantTree(int[][] thePixels) {
this.root = makeQuadrantTree(thePixels, 0, 0, thePixels.length);
}
/**
* Recursively builds a quadrant tree with the given pixels.
*
* @param pixels the 2D array of pixels
* @param x x-coordinate of the quadrant's top-left corner
* @param y y-coordinate of the quadrant's top-left corner
* @param size the size of the quadrant
* @return the root node of the built quadrant tree
*/
private QTreeNode makeQuadrantTree(int[][] pixels, int x, int y, int size) {
if (size == 1) {
int averageColor = pixels[y][x];
return new QTreeNode(null, x, y, size, averageColor);
// base case if size == 1
} else { // if the size of Q is greater than 1
QTreeNode R1 = makeQuadrantTree(pixels, x, y, size / 2);
QTreeNode R2 = makeQuadrantTree(pixels, x + size / 2, y, size / 2);
QTreeNode R3 = makeQuadrantTree(pixels, x, y + size / 2, size / 2);
QTreeNode R4 = makeQuadrantTree(pixels, x + size / 2, y + size / 2, size / 2);
int averageColor = calculateColor(pixels, x, y, size);
QTreeNode currNode = new QTreeNode(new QTreeNode[] { R1, R2, R3, R4 }, x, y, size, averageColor);
try {
R1.setParent(currNode);
R2.setParent(currNode);
R3.setParent(currNode);
R4.setParent(currNode);
} catch (QTreeException e) {
System.out.println("QTreeException");
}
return currNode;
}
}
/**
* Returns the root node of the quadrant tree.
*
* @return the root node
*/
public QTreeNode getRoot() {
return root;
}
/**
* Performs the calculation to get the average color of a quadrant.
*
* @param pixels the 2D array of pixels
* @param x the x-coordinate of the quadrant's top-left corner
* @param y the y-coordinate of the quadrant's top-left corner
* @param size the size of the quadrant
* @return the average color of the quadrant
*/
private int calculateColor(int[][] pixels, int x, int y, int size) {
if (size != 1) {
int averageColor = Gui.averageColor(pixels, x, y, size);
return averageColor;
}
return pixels[x][y];
}
/**
* Retrieves pixels from the quadrant tree at a specified level.
*
* @param r the root node of the quadrant tree
* @param theLevel the level at which to retrieve pixels
* @return a list of nodes containing pixels at the specified level
*/
public ListNode<QTreeNode> getPixels(QTreeNode r, int theLevel) {
if (r == null) {
return null;
}
if (r.isLeaf() || theLevel == 0) {
ListNode<QTreeNode> QTreeNodeList = new ListNode<QTreeNode>(r);
return QTreeNodeList;
} else {
ListNode<QTreeNode> totalNodes = null;
for (int i = 0; i < 4; i++) {
ListNode<QTreeNode> totalChildNodes = getPixels(r.getChild(i), theLevel - 1);
totalNodes = addLists(totalNodes, totalChildNodes);
}
return totalNodes;
}
}
/**
* Concatenates two lists of tree nodes.
*
* @param firstList is the first list of tree nodes
* @param secondList is the second list of tree nodes
* @return the merged list of tree nodes
*/
private ListNode<QTreeNode> addLists(ListNode<QTreeNode> firstList, ListNode<QTreeNode> secondList) {
if (firstList == null) {
return secondList;
}
ListNode<QTreeNode> newList = firstList;
while (newList.getNext() != null) {
newList = newList.getNext();
}
newList.setNext(secondList);
return firstList;
}
/**
* Finds nodes in the quadrant tree with the same color and level.
*
* @param r the root node of the quadrant tree
* @param theColor the color to match
* @param theLevel the level at which to search
* @return a Duple object containing the list of matching nodes and their count
*/
public Duple findMatching(QTreeNode r, int theColor, int theLevel) {
if (r.isLeaf() || theLevel == 0) {
if (Gui.similarColor(r.getColor(), theColor)) {
ListNode<QTreeNode> treeNodes = new ListNode<>(r);
return new Duple(treeNodes, 1);
} else {
return new Duple(null, 0);
}
} else {
int recursiveCount = 0;
ListNode<QTreeNode> totNodes = null;
for (int i = 0; i < 4; i++) {
Duple child = findMatching(r.getChild(i), theColor, theLevel - 1);
ListNode<QTreeNode> childrenNode = child.getFront();
int childrenCount = child.getCount();
recursiveCount += childrenCount;
totNodes = addLists(totNodes, childrenNode);
}
return new Duple(totNodes, recursiveCount);
}
}
/**
* Finds the node in the quadrant tree at the given level and coordinate.
*
* @param r root node of the quadrant tree
* @param theLevel level at which to search
* @param x x-coordinate of the node
* @param y y-coordinate of the node
* @return the node found at the stated level and coordinate, or null if not
* found
*/
public QTreeNode findNode(QTreeNode r, int theLevel, int x, int y) {
// basecase if r is null or if theLevel
if (r == null) {
return null;
}
if (r.isLeaf() || theLevel == 0) {
if (x >= r.getx() && x < r.getx() + r.getSize() && y >= r.gety() && y < r.gety() + r.getSize()) {
return r;
}
return null;
} else {
QTreeNode qNode = null;
for (int i = 0; i < 4; i++) {
qNode = findNode(r.getChild(i), theLevel - 1, x, y);
if (qNode != null)
return qNode;
}
return qNode;
}
}
}