//done public QuadTree(RectangleFloat bounds, QuadTree parent, int objectLimit = 2) { if (bounds.Width < 2 || bounds.Height < 2) { ObjectLimit = 100; //prevention of rounding issues ^_^ } else { ObjectLimit = objectLimit; } heldObjects = new List<GameObject>(); this.hardBounds = bounds; this.queryBounds = bounds; this.parent = parent; }
//done private void Split() { //setup new hardbounds NW = new QuadTree(new RectangleFloat(hardBounds.Left, hardBounds.Top, hardBounds.Width / 2, hardBounds.Height / 2), this, ObjectLimit+1); NE = new QuadTree(new RectangleFloat(hardBounds.Left + hardBounds.Width / 2, hardBounds.Top, hardBounds.Width / 2, hardBounds.Height / 2), this, ObjectLimit + 1); SW = new QuadTree(new RectangleFloat(hardBounds.Left, hardBounds.Top + hardBounds.Height / 2, hardBounds.Width / 2, hardBounds.Height / 2), this, ObjectLimit + 1); SE = new QuadTree(new RectangleFloat(hardBounds.Left + hardBounds.Width / 2, hardBounds.Top + hardBounds.Height / 2, hardBounds.Width / 2, hardBounds.Height / 2), this, ObjectLimit + 1); //no tricks, this um err, it attempts to insert the each object into the new subquads // on failure it does a master insert var temp = heldObjects; heldObjects = null; foreach (GameObject obj in temp) { if (!Insert(obj)) { MasterInsert(obj); } } }
private void Consolidate() { heldObjects = GatherAll(); foreach (var obj in heldObjects) { obj.CurrentQuad = this; } NW = NE = SW = SE = null; }