insert method

int insert(
  1. Rect rect
)

Insert an rectangle into the QuadTree. Returns the identifier of the object in the QuadTree.

Implementation

int insert(ui.Rect rect) {
  assert(rect.isFinite, 'The rectangle must be finite.');

  // Get the root node of the QuadTree
  // or create a new one if it does not exist.
  final root = _root ??= _createNode(
    parent: null,
    boundary: boundary,
  );

  // Create a new object in the QuadTree.
  final objectId = _getNextObjectId();
  // Increase the number of active objects in the QuadTree.
  _length++;

  final offset = objectId * _objectSize;
  // Store the object's coordinates in the objects array.
  _objects
    ..[offset + 0] = rect.left
    ..[offset + 1] = rect.top
    ..[offset + 2] = rect.width
    ..[offset + 3] = rect.height;

  // Find the node to insert the object
  final nodeId = root._insert(
    objectId,
    rect.left,
    rect.top,
    rect.width,
    rect.height,
  );

  // Store the reference between the id and the current node.
  _id2node[objectId] = nodeId;

  return objectId;
}