Commit ab7a6dd6 authored by Chinmay Garde's avatar Chinmay Garde

Add accessors for spring type

parent bcf1f8d0
......@@ -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);
......
......@@ -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));
......
......@@ -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);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment