A quadtree node with up to four children, but no own data @param element data type
| 231 | * @param <T> element data type |
| 232 | */ |
| 233 | private class QuadtreeNode<T> implements QuadtreeElement<T>{ |
| 234 | |
| 235 | QuadtreeElement<T> parent; |
| 236 | int length; // size in each direction |
| 237 | // max amount of children below this node: (2 * length)^2 |
| 238 | final int x, y; |
| 239 | private final QuadtreeElement<T> elements[] = new QuadtreeElement[4]; |
| 240 | |
| 241 | // fake enum (no not-static enums in Java?) |
| 242 | private static final int NW = 0, NE = 1, SW = 2, SE = 3; |
| 243 | |
| 244 | /** |
| 245 | * Constructs a new node |
| 246 | * @param _parent parent node or null (root) |
| 247 | * @param _x coordinate next to the center (-0.5, -0.5) |
| 248 | * @param _y coordinate next to the center (-0.5, -0.5) |
| 249 | * @param _length size in each direction |
| 250 | */ |
| 251 | public QuadtreeNode(QuadtreeNode<T> parent, int x, int y, int length) { |
| 252 | this.parent = parent; |
| 253 | this.x = x; |
| 254 | this.y = y; |
| 255 | this.length = length; |
| 256 | for(int i = 0; i < 4; ++i){ |
| 257 | elements[i] = null; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Gets the index of the child at x, y, or -1 if outside of this node |
| 263 | * @param x |
| 264 | * @param y |
| 265 | * @return index or -1 |
| 266 | */ |
| 267 | private int getChildNum(int x, int y){ |
| 268 | // check whether the child is in this node's range |
| 269 | if(x < (this.x - length + 1) || y < (this.y - length + 1) || |
| 270 | x > (this.x + length) || y > (this.y + length)){ |
| 271 | return -1; |
| 272 | } |
| 273 | // calculate child num |
| 274 | int id = 0; |
| 275 | if(x > this.x){ |
| 276 | id = 1; |
| 277 | } |
| 278 | if(y > this.y){ |
| 279 | id |= 2; |
| 280 | } |
| 281 | return id; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Gets the index of the child at x, y, or -1 if outside of this node |
| 286 | * for "static class simulation" |
| 287 | * @param x |
| 288 | * @param y |
| 289 | * @return index or -1 |
| 290 | */ |
nothing calls this directly
no outgoing calls
no test coverage detected