remove method

bool remove(
  1. int objectId
)

Removes objectId from the Quadtree if it exists. After removal, tries merging nodes upward if possible.

Implementation

bool remove(int objectId) {
  if (objectId < 0 || objectId >= _id2node.length) return false; // Invalid id
  final node = _nodes[_id2node[objectId]];
  if (node == null) return false; // Node not found

  // Remove the object directly from the node and mark the node and its
  // parents as dirty.
  if (!node._remove(objectId)) return false; // Object not found in the node

  _length--; // Decrease the length of the QuadTree
  _id2node[objectId] = 0; // Remove the reference to the node

  // Mark the node and all its parents as dirty
  // and possibly needs optimization.
  // Also decrease the length of the node and all its parents.
  for (QuadTree$Node? n = node; n != null; n = n.parent) {
    n._dirty = true;
    n._length--;
  }

  // Resize recycled ids array if needed
  if (_recycledIdsCount == _recycledIds.length)
    _recycledIds = _resizeUint32List(
      _recycledIds,
      _recycledIds.length << 1,
    );
  _recycledIds[_recycledIdsCount++] = objectId;

  return true;
}