queryPoint method

List<T> queryPoint(
  1. Offset point, {
  2. double radius = 0,
})

Query objects near a specific point for hit testing.

Returns objects in the cell containing the point plus neighboring cells to handle objects that span cell boundaries. Results are not cached since hit testing typically involves different points each time.

Implementation

List<T> queryPoint(Offset point, {double radius = 0}) {
  final result = <T>[];
  final checkedObjects = <String>{};

  // Calculate the cell range to check
  final minX = ((point.dx - radius) / gridSize).floor();
  final maxX = ((point.dx + radius) / gridSize).floor();
  final minY = ((point.dy - radius) / gridSize).floor();
  final maxY = ((point.dy + radius) / gridSize).floor();

  // Check all cells in range (typically just 1-4 cells for small radius)
  for (var x = minX; x <= maxX; x++) {
    for (var y = minY; y <= maxY; y++) {
      final cellKey = '${x}_$y';
      final objectIds = _spatialGrid[cellKey];

      if (objectIds != null) {
        for (final objectId in objectIds) {
          if (checkedObjects.add(objectId)) {
            final object = _objects[objectId];
            if (object != null) {
              final bounds = object.getBounds();
              // Check if point is within bounds (with optional radius expansion)
              if (radius > 0) {
                if (bounds.inflate(radius).contains(point)) {
                  result.add(object);
                }
              } else {
                if (bounds.contains(point)) {
                  result.add(object);
                }
              }
            }
          }
        }
      }
    }
  }

  return result;
}