/// <summary> /// Creates a new QuadNode and child nodes if this QuadNode is not a leaf. /// </summary> /// <param name="boundingBox">The Rectangle defining the world-space coverage area of this QuadNode.</param> /// <param name="parent">The QuadNode parent of this QuadNode.</param> /// <param name="isLeaf">Whether or not this QuadNode is a leaf node in the QuadTree.</param> /// <param name="myDepth">The depth of this QuadNode.</param> /// <param name="maxDepth">The maximum depth that the QuadTree supports.</param> public QuadNode(RectangleF boundingBox, QuadNode <T> parent, bool isLeaf, int myDepth, int maxDepth) { BoundingBox = boundingBox; Parent = parent; IsLeaf = isLeaf; HasItems = false; MyDepth = myDepth; collidableObjects = new List <T>(); //Construct the children QuadNodes if this QuadNode is not a leaf. if (!isLeaf) { //Shouldn't have to worry about this...but we might as well play it safe. //If the depth of this QuadNode is the max depth then simply mark it as a leaf node and be happy. if (myDepth == maxDepth) { IsLeaf = true; } else { int newDepth = myDepth + 1; float halfWidth = BoundingBox.Width / 2; float halfHeight = BoundingBox.Height / 2; bool areLeaves = false; children = new QuadNode <T> [4]; if (newDepth == maxDepth) { areLeaves = true; } //Create the 4 child QuadNodes, each controlling a distinct quadrant of this QuadNode's Bounding Box. children[0] = new QuadNode <T>(new RectangleF(BoundingBox.X, BoundingBox.Y, halfWidth, halfHeight), this, areLeaves, newDepth, maxDepth); //Top-Left children[1] = new QuadNode <T>(new RectangleF(BoundingBox.X + halfWidth, BoundingBox.Y, halfWidth, halfHeight), this, areLeaves, newDepth, maxDepth); //Top-Right children[2] = new QuadNode <T>(new RectangleF(BoundingBox.X, BoundingBox.Y + halfHeight, halfWidth, halfHeight), this, areLeaves, newDepth, maxDepth); //Bottom-Left children[3] = new QuadNode <T>(new RectangleF(BoundingBox.X + halfWidth, BoundingBox.Y + halfHeight, halfWidth, halfHeight), this, areLeaves, newDepth, maxDepth); //Bottom-Right } } }
/// <summary> /// Creates a new QuadTree covering the given area. /// </summary> /// <param name="maxDepth">The maximum depth of the QuadTree.</param> /// <param name="worldSpace">The world-space area covered by this QuadTree.</param> public QuadTreeOld(int maxDepth, RectangleF worldSpace) { root = new QuadNode <T>(worldSpace, null, false, 0, maxDepth); this.maxDepth = maxDepth; }