Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
ab7a6dd6
Commit
ab7a6dd6
authored
Jul 06, 2015
by
Chinmay Garde
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add accessors for spring type
parent
bcf1f8d0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
0 deletions
+38
-0
spring.dart
packages/newton/lib/src/spring.dart
+9
-0
spring_solution.dart
packages/newton/lib/src/spring_solution.dart
+11
-0
newton_test.dart
packages/newton/test/newton_test.dart
+18
-0
No files found.
packages/newton/lib/src/spring.dart
View file @
ab7a6dd6
...
...
@@ -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
);
...
...
packages/newton/lib/src/spring_solution.dart
View file @
ab7a6dd6
...
...
@@ -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
));
...
...
packages/newton/test/newton_test.dart
View file @
ab7a6dd6
...
...
@@ -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
);
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment