nearestXForOffset function

double? nearestXForOffset(
  1. BoundingBox bounds,
  2. Set<double> xValues,
  3. double offset,
  4. double width,
)

Implementation

double? nearestXForOffset(
  BoundingBox bounds,
  Set<double> xValues,
  double offset,
  double width,
) {
  if (xValues.isEmpty) {
    return null;
  }

  final fraction = offset / width;
  double minDistance = double.infinity;
  double nearestX = xValues.first;

  for (final value in xValues) {
    final distance = (fraction - bounds.getFractionX(value)).abs();
    if (distance < minDistance) {
      minDistance = distance;
      nearestX = value;
    }
  }

  return nearestX;
}