clampMagnitude method
Return a velocity whose magnitude has been clamped to minValue
and maxValue
.
If the magnitude of this Velocity is less than minValue then return a new
Velocity with the same direction and with magnitude minValue
. Similarly,
if the magnitude of this Velocity is greater than maxValue then return a
new Velocity with the same direction and magnitude maxValue
.
If the magnitude of this Velocity is within the specified bounds then just return this.
Implementation
Velocity clampMagnitude(double minValue, double maxValue) {
assert(minValue != null && minValue >= 0.0);
assert(maxValue != null && maxValue >= 0.0 && maxValue >= minValue);
final double valueSquared = pixelsPerSecond.distanceSquared;
if (valueSquared > maxValue * maxValue)
return Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * maxValue);
if (valueSquared < minValue * minValue)
return Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * minValue);
return this;
}