move method

void move(
  1. int objectId,
  2. double left,
  3. double top, {
  4. double? width,
  5. double? height,
})

Move the object with the given objectId to the new position left (x), top (y).

Optionally, you can also change the width and height of the object.

Implementation

void move(
  int objectId,
  double left,
  double top, {
  double? width,
  double? height,
}) {
  final root = _root;
  if (root == null) return;
  if (objectId < 0 || objectId >= _id2node.length) return;
  final nodeId = _id2node[objectId];
  if (nodeId >= _nodes.length) return;
  final node = _nodes[nodeId];

  final objects = _objects;
  final offset = objectId * _objectSize;

  // Update the object's coordinates.
  objects[offset + 0] = left;
  objects[offset + 1] = top;

  if (width != null) objects[offset + 2] = width;
  if (height != null) objects[offset + 3] = height;

  // Get the object's width and height.
  final w = width ?? objects[offset + 2];
  final h = height ?? objects[offset + 3];

  // Check if the object still fits in the same node's boundary.
  if (node == null) {
    assert(false, 'Current node not found for object with id $objectId.');
    // Insert the object to the QuadTree at the new position.
    final nodeId = root._insert(objectId, left, top, w, h);
    _id2node[objectId] = nodeId;
  } else if (_overlapsLTWH(node.boundary, left, top, w, h)) {
    // The object still fits in the same node's boundary.
    // Coordinate already updated - nothing to do.
  } else {
    // The object moved outside the boundary of the QuadTree.
    // Remove the object from the QuadTree and insert it back.
    // Do not change the object's id.
    node._remove(objectId);

    // 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--;
    }

    // Insert the object back into the QuadTree at the new position
    // with the same id.
    final nodeId = root._insert(objectId, left, top, w, h);
    _id2node[objectId] = nodeId;
  }
}