linearNiceRange function

List<num> linearNiceRange(
  1. num min,
  2. num max,
  3. int count
)

Calculates nice range for LinearScaleConv.

Implementation

List<num> linearNiceRange(
  num min,
  num max,
  int count,
) {
  // From d3-scale.

  final d = [min, max];
  int i0 = 0;
  int i1 = d.length - 1;
  num start = d[i0];
  num stop = d[i1];
  late num step;

  if (stop < start) {
    final tempValue = start;
    start = stop;
    stop = tempValue;
    final tempIndex = i0;
    i0 = i1;
    i1 = tempIndex;
  }

  // If min and max are same, return directly.
  if (stop - start < 1e-15 || count <= 0) {
    return [start, stop];
  }

  step = _tickIncrement(start, stop, count);

  if (step > 0) {
    start = (start / step).floor() * step;
    stop = (stop / step).ceil() * step;
    step = _tickIncrement(start, stop, count);
  } else if (step < 0) {
    start = (start * step).ceil() / step;
    stop = (stop * step).floor() / step;
    step = _tickIncrement(start, stop, count);
  }

  if (step > 0) {
    d[i0] = (start / step).floor() * step;
    d[i1] = (stop / step).ceil() * step;
  } else if (step < 0) {
    d[i0] = (start * step).ceil() / step;
    d[i1] = (stop * step).floor() / step;
  }
  return d;
}