mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Fixes a precision bug was introduced in 784e6d7, which improved the precision of lerpDouble when the extrema differed significantly in magnitude. _lerpInt doesn't have this issue since the extrema are passed as 64-bit twos-complement values, therefore the difference will always be accurate so long as it doesn't overflow. This reverts the _lerpInt implementation to the original a + (b - a) * t, but adds a test to avoid a regression if anyone is tempted to make it consistent with the others.
51 lines
1.5 KiB
Dart
51 lines
1.5 KiB
Dart
// Copyright 2013 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// @dart = 2.10
|
|
|
|
part of dart.ui;
|
|
|
|
/// Linearly interpolate between two numbers, `a` and `b`, by an extrapolation
|
|
/// factor `t`.
|
|
///
|
|
/// When `a` and `b` are equal or both NaN, `a` is returned. Otherwise, if
|
|
/// `a`, `b`, and `t` are required to be finite or null, and the result of `a +
|
|
/// (b - a) * t` is returned, where nulls are defaulted to 0.0.
|
|
double? lerpDouble(num? a, num? b, double t) {
|
|
if (a == b || (a?.isNaN == true) && (b?.isNaN == true))
|
|
return a?.toDouble();
|
|
a ??= 0.0;
|
|
b ??= 0.0;
|
|
assert(a.isFinite, 'Cannot interpolate between finite and non-finite values');
|
|
assert(b.isFinite, 'Cannot interpolate between finite and non-finite values');
|
|
assert(t.isFinite, 't must be finite when interpolating between values');
|
|
return a * (1.0 - t) + b * t;
|
|
}
|
|
|
|
/// Linearly interpolate between two doubles.
|
|
///
|
|
/// Same as [lerpDouble] but specialized for non-null `double` type.
|
|
double _lerpDouble(double a, double b, double t) {
|
|
return a * (1.0 - t) + b * t;
|
|
}
|
|
|
|
/// Linearly interpolate between two integers.
|
|
///
|
|
/// Same as [lerpDouble] but specialized for non-null `int` type.
|
|
double _lerpInt(int a, int b, double t) {
|
|
return a + (b - a) * t;
|
|
}
|
|
|
|
/// Same as [num.clamp] but specialized for non-null [int].
|
|
int _clampInt(int value, int min, int max) {
|
|
assert(min <= max);
|
|
if (value < min) {
|
|
return min;
|
|
} else if (value > max) {
|
|
return max;
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|