//Child Node Declearation, can be used a root node public AreaMap(Rectangle area, int depth = 0) { Nodes = new List <T>(); Children = new AreaMap <T> [4]; Area = area; Depth = depth; HasChildren = false; }
//Root Node declearation public AreaMap() { Nodes = new List <T>(); Children = new AreaMap <T> [4]; Area = Rectangle.Empty; Depth = 0; Children[0] = new AreaMap <T>(new Rectangle(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue), Depth + 1); Children[1] = new AreaMap <T>(new Rectangle(int.MinValue, 0, int.MaxValue, int.MaxValue), Depth + 1); Children[2] = new AreaMap <T>(new Rectangle(int.MinValue, 0, int.MaxValue, int.MaxValue), Depth + 1); Children[3] = new AreaMap <T>(new Rectangle(0, 0, int.MaxValue, int.MaxValue), Depth + 1); HasChildren = true; }
public void Split() { if (HasChildren) { throw new Exception("Cannot split when you already have children."); } if (Depth == DepthLimit) { return; } int new_width = this.Area.Width / 2; int new_height = this.Area.Height / 2; if (!new Rectangle(0, 0, new_width, new_height).Contains(MinimumArea)) { return; } Children[0] = new AreaMap <T>(new Rectangle(this.Area.X, this.Area.Y, new_width, new_height), Depth + 1); Children[1] = new AreaMap <T>(new Rectangle(this.Area.X, this.Area.Y + new_height, new_width, new_height), Depth + 1); Children[2] = new AreaMap <T>(new Rectangle(this.Area.X + new_width, this.Area.Y + new_height, new_width, new_height), Depth + 1); Children[3] = new AreaMap <T>(new Rectangle(this.Area.X + new_width, this.Area.Y, new_width, new_height), Depth + 1); HasChildren = true; var toRemove = new List <T>(); foreach (var obj in this.Nodes) { foreach (var child in Children) { if (child.Area.Contains(obj.Area)) { if (child.Add(obj)) { toRemove.Add(obj); } } } } foreach (var obj in toRemove) { this.Nodes.Remove(obj); } }