public IEnumerable <T> Query(Vector2 point, float radius) { var root = rootNode; if (QuadTreeUtils.Overlaps(root.Bounds, point, radius)) { return(root.Query(point, radius)); } return(Enumerable.Empty <T>()); }
private QuadTreeNode <T>[] CreateChildren() { var rects = QuadTreeUtils.DevideQuads(Bounds); var children = new QuadTreeNode <T> [rects.Length]; for (int i = 0; i < children.Length; i++) { children[i] = new QuadTreeNode <T>(tree, this, rects[i], i); } return(children); }
private QuadTreeNode <T> Grow(QuadTreeNode <T> rootNode, Rect bounds) { var newBounds = QuadTreeUtils.GrowQuad(rootNode.Bounds, bounds.center, out int sourceIndex, out int index); var quads = QuadTreeUtils.DevideQuads(newBounds); var children = new QuadTreeNode <T> [4]; for (int i = 0; i < 4; i++) { if (i == index) { children[i] = rootNode; } else { children[i] = new QuadTreeNode <T>(this, quads[i]); } } return(new QuadTreeNode <T>(this, children, newBounds)); }
internal QuadTreeNode <T> Add(T item) { var index = QuadTreeUtils.GetQuadIndex(Bounds, item.Bounds.center); if (bounds.size.x > tree.MinSize.x && bounds.size.y > tree.MinSize.y) { if (children == null) { lock (childrenLock) if (children == null) { children = CreateChildren(); } } if (children[index].Encapsulates(item.Bounds)) { return(children[index].Add(item)); } } this.items.Add(item); InvalidateBounds(); return(this); }
internal bool Encapsulates(Rect bounds) => QuadTreeUtils.Encapsulates(maxBounds, bounds) && this.bounds.Contains(bounds.center);