/// <summary> /// Get list of nodes that intersect the given bounds. /// </summary> /// <param name="bounds">The bounds to test</param> /// <returns>The list of nodes intersecting the given bounds</returns> IEnumerable <QuadNode> GetNodes(Rect bounds) { List <QuadNode> result = new List <QuadNode>(); if (_root != null) { _root.GetIntersectingNodes(result, bounds); } return(result); }
/// <summary> /// Rebuild all the Quadrants according to the current QuadTree Bounds. /// </summary> private void ReIndex() { Quadrant oldRoot = this.root; this.root = new Quadrant(this.realExtent); if (oldRoot != null) { foreach (var node in oldRoot.GetIntersectingNodes(this.realExtent)) { // Todo: It would be more efficient if we added a code path that allowed reuse of the QuadNode wrappers. Insert(node.Item1.Node, node.Item1.Bounds, node.Item1.Priority); } } }
/// <summary> /// Returns all nodes in this quadrant that intersect the given bounds. /// The nodes are returned in pretty much random order as far as the caller is concerned. /// </summary> /// <param name="nodes">List of nodes found in the given bounds</param> /// <param name="bounds">The bounds that contains the nodes you want returned</param> internal void GetIntersectingNodes(List <QuadNode> nodes, Rect bounds) { if (bounds.IsEmpty) { return; } double w = _bounds.Width / 2; double h = _bounds.Height / 2; // assumption that the Rect struct is almost as fast as doing the operations // manually since Rect is a value type. Rect topLeft = new Rect(_bounds.Left, _bounds.Top, w, h); Rect topRight = new Rect(_bounds.Left + w, _bounds.Top, w, h); Rect bottomLeft = new Rect(_bounds.Left, _bounds.Top + h, w, h); Rect bottomRight = new Rect(_bounds.Left + w, _bounds.Top + h, w, h); // See if any child quadrants completely contain this node. if (topLeft.IntersectsWith(bounds) && _topLeft != null) { _topLeft.GetIntersectingNodes(nodes, bounds); } if (topRight.IntersectsWith(bounds) && _topRight != null) { _topRight.GetIntersectingNodes(nodes, bounds); } if (bottomLeft.IntersectsWith(bounds) && _bottomLeft != null) { _bottomLeft.GetIntersectingNodes(nodes, bounds); } if (bottomRight.IntersectsWith(bounds) && _bottomRight != null) { _bottomRight.GetIntersectingNodes(nodes, bounds); } GetIntersectingNodes(_nodes, nodes, bounds); }