snap static method

double snap(
  1. double value,
  2. double start,
  3. double end,
  4. double step,
)

Snaps value to the nearest discrete position for the given range.

Implementation

static double snap(double value, double start, double end, double step) {
  final positions = discreteValues(start: start, end: end, step: step);
  if (positions.isEmpty) return value.clamp(start, end);
  double nearest = positions.first;
  double bestDist = (value - nearest).abs();
  for (final candidate in positions) {
    final dist = (value - candidate).abs();
    if (dist < bestDist) {
      bestDist = dist;
      nearest = candidate;
    }
  }
  return nearest;
}