src/Quadtree.cs
/// <summary>Updates the given item from the tree if it exists.</summary>
/// <returns>True if item is in the tree and has been updated, otherwise false.</returns>
Updates the given item from the tree if it exists.
Updates the given item from the tree if it exists .
public bool Update(T item) {
            var xy = item.Bounds.Center.ToPoint();
            if (TryExpandTree(xy))
                return true;
            if (!_item.TryGetValue(item, out var v))
                return false;
            if (v.Node == _root) {
                _item[item] = (v.Node, xy);
                if (_root.ItemCount > NodesCapacity)
                    _nodesToSubdivide.Add(_root);
                _nodesToGrow.Add(_root);
                QueueClean();
                return true;
            }
            int halfWidth = _bounds.Width >> v.Node.Depth,
                halfHeight = _bounds.Height >> v.Node.Depth;
            var bounds = new Rectangle(v.Node.cX - halfWidth, v.Node.cY - halfHeight, halfWidth << 1, halfHeight << 1);
            if (bounds.Contains(xy)) {
                _item[item] = (v.Node, xy);
                _nodesToGrow.Add(v.Node);
                QueueClean();
                return true;
            }
            v.Node.Remove(item);
            _nodesToGrow.Add(v.Node);
            _nodesToClean.Add(v.Node.Parent);
            var n = v.Node;
            do {
                if (n.Parent == null)
                    break;
                halfWidth = _bounds.Width >> n.Depth;
                halfHeight = _bounds.Height >> n.Depth;
                bounds = new Rectangle(n.cX - halfWidth, n.cY - halfHeight, halfWidth << 1, halfHeight << 1);
                if (bounds.Contains(xy)) {
                    n = n.Parent;
                    break;
                }
                n = n.Parent;
            }
            while (true);
            var n2 = Insert(item, n, xy);
            _item[item] = (n2, xy);
            if (n2.ItemCount > NodesCapacity && n2.Depth < MaxDepth)
                _nodesToSubdivide.Add(n2);
            _nodesToGrow.Add(n2);
            QueueClean();
            return true;
        }
public bool Update ( T item ) { var xy = item . Bounds . Center . ToPoint ( ) ; if ( TryExpandTree ( xy ) ) return true ; if ( ! _item . TryGetValue ( item , out var v ) ) return false ; if ( v . Node == _root ) { _item [ item ] = ( v . Node , xy ) ; if ( _root . ItemCount > NodesCapacity ) _nodesToSubdivide . Add ( _root ) ; _nodesToGrow . Add ( _root ) ; QueueClean ( ) ; return true ; } int halfWidth = _bounds . Width >> v . Node . Depth , halfHeight = _bounds . Height >> v . Node . Depth ; var bounds = new Rectangle ( v . Node . cX - halfWidth , v . Node . cY - halfHeight , halfWidth << 1 , halfHeight << 1 ) ; if ( bounds . Contains ( xy ) ) { _item [ item ] = ( v . Node , xy ) ; _nodesToGrow . Add ( v . Node ) ; QueueClean ( ) ; return true ; } v . Node . Remove ( item ) ; _nodesToGrow . Add ( v . Node ) ; _nodesToClean . Add ( v . Node . Parent ) ; var n = v . Node ; do { if ( n . Parent == null ) break ; halfWidth = _bounds . Width >> n . Depth ; halfHeight = _bounds . Height >> n . Depth ; bounds = new Rectangle ( n . cX - halfWidth , n . cY - halfHeight , halfWidth << 1 , halfHeight << 1 ) ; if ( bounds . Contains ( xy ) ) { n = n . Parent ; break ; } n = n . Parent ; } while ( true ) ; var n2 = Insert ( item , n , xy ) ; _item [ item ] = ( n2 , xy ) ; if ( n2 . ItemCount > NodesCapacity && n2 . Depth < MaxDepth ) _nodesToSubdivide . Add ( n2 ) ; _nodesToGrow . Add ( n2 ) ; QueueClean ( ) ; return true ; }
Updates the given item from the tree if it exists.
Updates the given item from the tree if it exists .
//if (Math.Sign(xy.X - v.Node.Parent.cX) == Math.Sign(v.XY.X - v.Node.Parent.cX) && Math.Sign(xy.Y - v.Node.Parent.cY) == Math.Sign(v.XY.Y - v.Node.Parent.cY)) { //if (Math.Sign(xy.X - n.Parent.cX) == Math.Sign(v.XY.X - n.Parent.cX) && Math.Sign(xy.Y - n.Parent.cY) == Math.Sign(v.XY.Y - n.Parent.cY)) {
True if item is in the tree and has been updated, otherwise false.
True if item is in the tree and has been updated otherwise false .
item
T