From ab7a6dd6c2e9d69cac19b4317a99a2823d067247 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 12:48:52 -0700 Subject: [PATCH] Add accessors for spring type --- packages/newton/lib/src/spring.dart | 9 +++++++++ packages/newton/lib/src/spring_solution.dart | 11 +++++++++++ packages/newton/test/newton_test.dart | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/packages/newton/lib/src/spring.dart b/packages/newton/lib/src/spring.dart index aee59baea9..f23ce27b13 100644 --- a/packages/newton/lib/src/spring.dart +++ b/packages/newton/lib/src/spring.dart @@ -17,12 +17,19 @@ class SpringDesc { final double damping; SpringDesc(this.mass, this.springConstant, this.damping); + + /// Create a spring given the mass, spring constant and the damping ratio. The + /// damping ratio is especially useful trying to determing the type of spring + /// to create. A ratio of 1.0 creates a critically damped spring, > 1.0 + /// creates an overdamped spring and < 1.0 an underdamped one. SpringDesc.withDampingRatio(double mass, double springConstant, double zeta) : this.mass = mass, this.springConstant = springConstant, this.damping = zeta * 2.0 * Math.sqrt(mass * springConstant); } +enum SpringType { unknown, criticallyDamped, underDamped, overDamped, } + /// Creates a spring simulation. Depending on the spring description, a /// critically, under or overdamped spring will be created. class Spring extends Simulation { @@ -36,6 +43,8 @@ class Spring extends Simulation { : this._endPosition = end, _solution = new _SpringSolution(desc, start - end, velocity); + SpringType get type => _solution.type; + double x(double time) => _endPosition + _solution.x(time); double dx(double time) => _solution.dx(time); diff --git a/packages/newton/lib/src/spring_solution.dart b/packages/newton/lib/src/spring_solution.dart index 511b32a293..fe57f7c08b 100644 --- a/packages/newton/lib/src/spring_solution.dart +++ b/packages/newton/lib/src/spring_solution.dart @@ -20,6 +20,8 @@ abstract class _SpringSolution implements Simulatable { return null; } + + SpringType get type; } class _CriticalSolution implements _SpringSolution { @@ -32,6 +34,9 @@ class _CriticalSolution implements _SpringSolution { return new _CriticalSolution.withArgs(r, c1, c2); } + @override + SpringType get type => SpringType.criticallyDamped; + _CriticalSolution.withArgs(double r, double c1, double c2) : _r = r, _c1 = c1, @@ -67,6 +72,9 @@ class _OverdampedSolution implements _SpringSolution { _c1 = c1, _c2 = c2; + @override + SpringType get type => SpringType.overDamped; + double x(double time) => (_c1 * Math.pow(Math.E, _r1 * time) + _c2 * Math.pow(Math.E, _r2 * time)); @@ -95,6 +103,9 @@ class _UnderdampedSolution implements _SpringSolution { _c1 = c1, _c2 = c2; + @override + SpringType get type => SpringType.underDamped; + double x(double time) => Math.pow(Math.E, _r * time) * (_c1 * Math.cos(_w * time) + _c2 * Math.sin(_w * time)); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index de86bbe61d..54c7daa223 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -56,4 +56,22 @@ void main() { expect(gravity.x(2.5), 725); expect(gravity.dx(2.5), 500.0); }); + + test('spring_types', () { + var crit = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 1.0), 0.0, 300.0, 0.0); + expect(crit.type, SpringType.criticallyDamped); + + var under = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 0.75), 0.0, 300.0, 0.0); + expect(under.type, SpringType.underDamped); + + var over = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 1.25), 0.0, 300.0, 0.0); + expect(over.type, SpringType.overDamped); + + // Just so we don't forget how to create a desc without the ratio. + var other = new Spring(new SpringDesc(1.0, 100.0, 20.0), 0.0, 20.0, 20.0); + expect(other.type, SpringType.criticallyDamped); + }); }