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 11 12 13 14 15 16 17
/// 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
/// particle's initial velocity being up, and the accerelation being down, and
/// 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 26 27 28 29 30 31 32 33 34
  ClampedSimulation(this.simulation, {
    this.xMin: double.NEGATIVE_INFINITY,
    this.xMax: double.INFINITY,
    this.dxMin: double.NEGATIVE_INFINITY,
    this.dxMax: double.INFINITY
  }) {
    assert(simulation != null);
    assert(xMax >= xMin);
    assert(dxMax >= dxMin);
  }

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

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

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

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

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

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

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

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