interpolate method

List<double> interpolate(
  1. int steps, {
  2. bool inclusive = false,
  3. double randomize = 0.0,
  4. int? seed,
})

Returns a list of numbers spanning a defined number of steps between the min and max values.

If inclusive is true, the max value will be included in the returned list, and the length of the list will be steps + 1.

randomize can be used to apply randomness to the output numbers, where a value of 0.0 doesn't apply any randomness to the numbers, while a value of 1.0 will randomize the output numbers within the entire range of the radius around each step.

Implementation

List<double> interpolate(
  int steps, {
  bool inclusive = false,
  double randomize = 0.0,
  int? seed,
}) {
  assert(steps > 0);
  assert(randomize >= 0.0 && randomize <= 1.0);
  math.Random? random;
  final offset = inclusive ? 1 : 0;
  final slice = span / steps;
  final radius = slice / 2;
  final list = List<double>.generate(steps - offset, (index) {
    var randomness = 0.0;
    if (randomize > 0.0) {
      random ??= math.Random(seed);
      randomness = radius - (random!.nextDouble() * slice);
      if (index == 0 && randomness.isNegative) randomness *= -1;
    }
    return ((index + offset) * slice) + min + randomness;
  });
  if (inclusive) {
    if (randomize == 0.0) {
      list.insert(0, min.toDouble());
      list.add(max.toDouble());
    } else {
      random ??= math.Random(seed);
      list.insert(0, (slice / 2) * random!.nextDouble());
      list.add(max - (slice / 2) * random!.nextDouble());
    }
  }
  return list;
}