rangeAroundN function

List<num> rangeAroundN(
  1. double n,
  2. int x, {
  3. bool loose = false,
})

Implementation

List<num> rangeAroundN(double n, int x, {bool loose = false}) {
  // Start from max(1.0, n-x) to ensure numbers are greater than 0
  final double start = n - x < 1.0 ? 1.0 : n - x;

  // End at n+x, and in Dart, the end is exclusive, so we use n+x+0.1 to
  // include n+x in the range
  final double end = n + x + 0.1;

  // Generate the list of numbers from start to end
  final List<num> result = [];

  for (double i = start; i < end; i += 1.0) {
    if (loose && i != n) {
      // If loose is true, convert to int, except for the central value 'n'
      result.add(i.toInt());
    } else {
      // Otherwise, add as double
      result.add(i);
    }
  }

  return result;
}