extrapolateStop method Null safety

double extrapolateStop(
  1. List<double> stops,
  2. double t
)

Extrapolate the stop at position t from a configured list of stops.

This process considers the actual beginning, ending, and intermediate values at each configured stop and returns a new stop that would resemble the place along this stops timeline of given length represented by keyframe t.


For example, if stops is [0.0, 0.5, 0.75, 0.9] with a length of four, a requested t percentage of t: 0.8 returns the value 0.78.

The value returned may be expected to be 0.8 itself; however consider the stops list only ranges 0.0 .. 0.9.

This method starts with the lowest available stop that at least is greater than the truncated "position" of this t along a the list of stops.length.

  • As above, with t: 0.8, the position of this extrapolated stop is 0.8 * stops.length (length is 4) = 3.2

The individual "progress" of this extrapolated stop is 20% (3.2 - 3) of the way from the third stop to the fourth stop, 0.75 and 0.9 in this case.

  • 0.75 + (0.9 - 0.75) * 0.2 = 0.78

Implementation

static double extrapolateStop(List<double> stops, double t) {
  final position = t * stops.length;
  final progress = position - position.truncate();

  final lastIndex = stops
      .lastIndexWhere((double s) => s <= position.truncate() / stops.length);
  final next = stops.firstWhere((double s) => s >= t, orElse: () => 1.0);

  final result = stops[lastIndex] + (next - stops[lastIndex]) * progress;
  // print('\nTARGET: $t; position: $position out of ${stops.length} | '
  //     'lastIndex: $lastIndex, next original stop: $next, \nresult: $result');
  return result;
}