func New(cfg Config, bounds geom.Rect) (me *Tree) { me = &Tree{ cfg: cfg, UpperBounds: bounds, Partition: bounds.Min.Plus(bounds.Max).Times(0.5), } me.Bounds = geom.NilRect() return }
func (me *Tree) Remove(element Item) (removed bool) { dbg("removing %v", element) if Debug { println(element) } Indent++ defer func() { Indent-- }() if !geom.RectsIntersect(me.UpperBounds, element.Bounds()) { dbg("out of bounds") return } defer func() { if removed { me.Count = 0 me.Bounds = geom.NilRect() if me.BigElements != nil { me.Count += len(me.BigElements) for elem := range me.BigElements { me.Bounds.ExpandToContainRect(elem.Bounds()) } } if me.Elements != nil { me.Count += len(me.Elements) for elem := range me.Elements { me.Bounds.ExpandToContainRect(elem.Bounds()) } } if me.Subtrees[0] != nil { for _, t := range me.Subtrees { if t.Count != 0 { me.Count += t.Count me.Bounds.ExpandToContainRect(t.Bounds) } } } } }() dbg("BigElements: %v", me.BigElements) dbg("Elements: %v", me.Elements) if me.BigElements != nil { for elem := range me.BigElements { if element.Equals(elem) { delete(me.BigElements, elem) removed = true } } } if me.Elements != nil { for elem := range me.Elements { if element.Equals(elem) { delete(me.Elements, elem) removed = true } } } for _, t := range me.Subtrees { if t == nil { continue } if t.Remove(element) { removed = true } } return }