public QuadrantNode(QuadrantNode Parent, Rectangle Bounds, QuadTree <T> Owner) { this.Parent = Parent; if (Bounds.IsEmpty) { throw new ArgumentException(nameof(Bounds) + " cannot be empty"); } this.BoundingRect = Bounds; this.Owner = Owner; }
public bool Remove(T Node) { QuadrantNode Parent = null; if (Table.TryGetValue(Node, out Parent)) { Parent.RemoveNode(Node); _table.Remove(Node); return(true); } return(false); }
public void Insert(T Node, Rectangle Bounds) { if (Bounds.IsEmpty) { throw new ArgumentException(nameof(Bounds) + " cannot be empty"); } if (Root == null) { Root = new QuadrantNode(null, Bounds, this); _table = new Dictionary <T, QuadrantNode>(); } QuadrantNode Parent = Root.Insert(Node, Bounds); _table[Node] = Parent; }
internal QuadrantNode Insert(T Node, Rectangle Bounds) { if (Bounds.IsEmpty) { throw new ArgumentException(nameof(Bounds) + " cannot be empty"); } QuadrantNode toInsert = this; while (true) { Rectangle R0 = toInsert.BoundingRect; float halfWidth = R0.Width / 2; if (halfWidth < 1) { halfWidth = 1; } float halfHeight = R0.Height / 2; if (halfHeight < 1) { halfHeight = 1; } Vector2 HalfExtent = new Vector2(halfWidth, halfHeight); Rectangle BottomLeft = Rectangle.FromMinAndSize(R0.LowerLeft, HalfExtent); Rectangle TopLeft = Rectangle.FromMinAndSize(new Vector2(R0.Left, R0.Center.Y), HalfExtent); Rectangle TopRight = Rectangle.FromMinAndSize(R0.Center, HalfExtent); Rectangle BottomRight = Rectangle.FromMinAndSize(new Vector2(R0.Center.X, R0.Bottom), HalfExtent); QuadrantNode child = null; if (TopLeft.Contains(Bounds, Owner.CompareMode)) { if (toInsert.UpperLeft == null) { toInsert.UpperLeft = new QuadrantNode(toInsert, TopLeft, Owner); } child = toInsert.UpperLeft; } else if (TopRight.Contains(Bounds, Owner.CompareMode)) { if (toInsert.UpperRight == null) { toInsert.UpperRight = new QuadrantNode(toInsert, TopRight, Owner); } child = toInsert.UpperRight; } else if (BottomLeft.Contains(Bounds, Owner.CompareMode)) { if (toInsert.LowerLeft == null) { toInsert.LowerLeft = new QuadrantNode(toInsert, BottomLeft, Owner); } child = toInsert.LowerLeft; } else if (BottomRight.Contains(Bounds, Owner.CompareMode)) { if (toInsert.LowerRight == null) { toInsert.LowerRight = new QuadrantNode(toInsert, BottomRight, Owner); } child = toInsert.LowerRight; } if (child != null) { toInsert = child; } else { //Insert the data if (toInsert.Nodes == null) { toInsert.Nodes = new List <Tuple <Rectangle, T> >(); } toInsert.Nodes.Add(new Tuple <Rectangle, T>(Bounds, Node)); return(toInsert); } } }