/* Fixed quadtree implementation using array. Some quadtree implementations can dynamically expand the tree, in which case you will need to use pointers to a node's children and (maybe) parent. But if we pre-construct a full tree up to a certain depth, it is possible to use an array to represent the tree, similar to the implementation of a priority heap: root at index 0 given node at index i, first child at index i*4+1, last child at i*4+4 given node at index i, parent node at (i-1)/4. quads: 0= NW, 1 = NE, 2 = SW, 3 = SE */ import java.util.*; // object that can be inserted into a quadtree must implement the following: interface qtobject extends Comparable { int X(); // x and y coordinates int Y(); int R(); // radius void react(qtobject m); // react on collision (observer pattern update) // included by "extend": int compareTo(qtobject x) } class qnode // a quadtree node { ////////////////// STATIC (GLOBAL) SETUP ///////////////// protected static qnode[] Tree; // global, to be initialized externally public static void setup(qnode[] t) { Tree = t; } public static void setup(int depth, int n, int e, int s, int w) { int m = 0; // compute size based on depth of the tree. int power = 1; for(int i=0;i=Tree.length || i<0) return; // check index Tree[i] = new qnode(i, n, e, s, w); makenode(i*4+1,n,(e+w)/2,(n+s)/2,w); // NW subquad makenode(i*4+2,n,e,(n+s)/2,(e+w)/2); // NE makenode(i*4+3,(n+s)/2,(e+w)/2,s,w); // SW makenode(i*4+4,(n+2)/2,e,s,(e+w)/2); // SE } /////////////////////////////////////////////////////////// int NB, SB, EB, WB; // quadrant boundaries, W,N inclusive //ArrayList Elements; HashSet Elements; // qtobjects that intersect subquads. int index; // index inside global tree array public qnode(int i, int a, int b, int c, int d) { NB=a; EB =b; SB=c; WB=d; index = i; // Elements = new ArrayList(); Elements = new HashSet(); }// constructor protected boolean intersect(qtobject m) // inquad or straddles border: { int x = m.X(), y = m.Y(), r = m.R(); return x>=WB && y=NB && y=WB && x+r=NB && y+r