clamped_simulation.dart 1.96 KB
Newer Older
1
// Copyright 2016 The Chromium Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'simulation.dart';
6

Ian Hickson's avatar
Ian Hickson committed
7 8 9 10
/// A simulation that applies limits to another simulation.
///
/// The limits are only applied to the other simulation's outputs. For example,
/// if a maximum position was applied to a gravity simulation with the
11
/// particle's initial velocity being up, and the acceleration being down, and
Ian Hickson's avatar
Ian Hickson committed
12 13 14 15 16 17
/// the maximum position being between the initial position and the curve's
/// apogee, then the particle would return to its initial position in the same
/// amount of time as it would have if the maximum had not been applied; the
/// difference would just be that the position would be reported as pinned to
/// the maximum value for the times that it would otherwise have been reported
/// as higher.
18
class ClampedSimulation extends Simulation {
Ian Hickson's avatar
Ian Hickson committed
19 20 21 22 23

  /// Creates a [ClampedSimulation] that clamps the given simulation.
  ///
  /// The named arguments specify the ranges for the clamping behavior, as
  /// applied to [x] and [dx].
24 25
  ClampedSimulation(
    this.simulation, {
26 27 28
    this.xMin = double.negativeInfinity,
    this.xMax = double.infinity,
    this.dxMin = double.negativeInfinity,
29
    this.dxMax = double.infinity,
30 31 32
  }) : assert(simulation != null),
       assert(xMax >= xMin),
       assert(dxMax >= dxMin);
33

Ian Hickson's avatar
Ian Hickson committed
34 35
  /// The simulation being clamped. Calls to [x], [dx], and [isDone] are
  /// forwarded to the simulation.
36
  final Simulation simulation;
Ian Hickson's avatar
Ian Hickson committed
37 38

  /// The minimum to apply to [x].
39
  final double xMin;
Ian Hickson's avatar
Ian Hickson committed
40 41

  /// The maximum to apply to [x].
42
  final double xMax;
Ian Hickson's avatar
Ian Hickson committed
43 44

  /// The minimum to apply to [dx].
45
  final double dxMin;
Ian Hickson's avatar
Ian Hickson committed
46 47

  /// The maximum to apply to [dx].
48 49
  final double dxMax;

50
  @override
51
  double x(double time) => simulation.x(time).clamp(xMin, xMax);
52 53

  @override
54
  double dx(double time) => simulation.dx(time).clamp(dxMin, dxMax);
55 56

  @override
57 58
  bool isDone(double time) => simulation.isDone(time);
}