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
7b77043e
Commit
7b77043e
authored
Jul 09, 2015
by
Chinmay Garde
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Newton: Address initial code review concerns
parent
be5e52bc
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
61 additions
and
45 deletions
+61
-45
newton.dart
packages/newton/lib/newton.dart
+1
-1
friction_simulation.dart
packages/newton/lib/src/friction_simulation.dart
+3
-3
scroll_simulation.dart
packages/newton/lib/src/scroll_simulation.dart
+2
-2
spring_simulation.dart
packages/newton/lib/src/spring_simulation.dart
+16
-7
spring_solution.dart
packages/newton/lib/src/spring_solution.dart
+18
-17
newton_test.dart
packages/newton/test/newton_test.dart
+21
-15
No files found.
packages/newton/lib/newton.dart
View file @
7b77043e
...
...
@@ -4,7 +4,7 @@
library
newton
;
import
'dart:math'
as
M
ath
;
import
'dart:math'
as
m
ath
;
part
'src/simulation.dart'
;
part
'src/simulation_group.dart'
;
...
...
packages/newton/lib/src/friction_simulation.dart
View file @
7b77043e
...
...
@@ -12,14 +12,14 @@ class FrictionSimulation extends Simulation {
FrictionSimulation
(
double
drag
,
double
position
,
double
velocity
)
:
_drag
=
drag
,
_dragLog
=
M
ath
.
log
(
drag
),
_dragLog
=
m
ath
.
log
(
drag
),
_x
=
position
,
_v
=
velocity
;
double
x
(
double
time
)
=>
_x
+
_v
*
M
ath
.
pow
(
_drag
,
time
)
/
_dragLog
-
_v
/
_dragLog
;
_x
+
_v
*
m
ath
.
pow
(
_drag
,
time
)
/
_dragLog
-
_v
/
_dragLog
;
double
dx
(
double
time
)
=>
_v
*
M
ath
.
pow
(
_drag
,
time
);
double
dx
(
double
time
)
=>
_v
*
m
ath
.
pow
(
_drag
,
time
);
@override
bool
isDone
(
double
time
)
=>
dx
(
time
).
abs
()
<
1.0
;
...
...
packages/newton/lib/src/scroll_simulation.dart
View file @
7b77043e
...
...
@@ -10,7 +10,7 @@ part of newton;
class
ScrollSimulation
extends
SimulationGroup
{
final
double
_leadingExtent
;
final
double
_trailingExtent
;
final
SpringDesc
_springDesc
;
final
SpringDesc
ription
_springDesc
;
final
double
_drag
;
bool
_isSpringing
=
false
;
...
...
@@ -18,7 +18,7 @@ class ScrollSimulation extends SimulationGroup {
double
_offset
=
0.0
;
ScrollSimulation
(
double
position
,
double
velocity
,
double
leading
,
double
trailing
,
SpringDesc
spring
,
double
drag
)
double
trailing
,
SpringDesc
ription
spring
,
double
drag
)
:
_leadingExtent
=
leading
,
_trailingExtent
=
trailing
,
_springDesc
=
spring
,
...
...
packages/newton/lib/src/spring_simulation.dart
View file @
7b77043e
...
...
@@ -4,7 +4,7 @@
part of
newton
;
class
SpringDesc
{
class
SpringDesc
ription
{
/// The mass of the spring (m)
final
double
mass
;
...
...
@@ -16,16 +16,24 @@ class SpringDesc {
/// constructor provided for this purpose
final
double
damping
;
SpringDesc
(
this
.
mass
,
this
.
springConstant
,
this
.
damping
);
SpringDescription
({
double
mass
,
double
springConstant
,
double
damping
})
:
mass
=
mass
,
springConstant
=
springConstant
,
damping
=
damping
{
assert
(
mass
!=
null
);
assert
(
springConstant
!=
null
);
assert
(
damping
!=
null
);
}
/// 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
);
SpringDescription
.
withDampingRatio
(
{
double
mass
,
double
springConstant
,
double
ratio:
1.0
})
:
mass
=
mass
,
springConstant
=
springConstant
,
damping
=
ratio
*
2.0
*
math
.
sqrt
(
mass
*
springConstant
);
}
enum
SpringType
{
unknown
,
criticallyDamped
,
underDamped
,
overDamped
,
}
...
...
@@ -39,7 +47,8 @@ class SpringSimulation extends Simulation {
/// A spring description with the provided spring description, start distance,
/// end distance and velocity.
SpringSimulation
(
SpringDesc
desc
,
double
start
,
double
end
,
double
velocity
)
SpringSimulation
(
SpringDescription
desc
,
double
start
,
double
end
,
double
velocity
)
:
this
.
_endPosition
=
end
,
_solution
=
new
_SpringSolution
(
desc
,
start
-
end
,
velocity
);
...
...
packages/newton/lib/src/spring_solution.dart
View file @
7b77043e
...
...
@@ -6,7 +6,7 @@ part of newton;
abstract
class
_SpringSolution
implements
Simulatable
{
factory
_SpringSolution
(
SpringDesc
desc
,
double
initialPosition
,
double
initialVelocity
)
{
SpringDesc
ription
desc
,
double
initialPosition
,
double
initialVelocity
)
{
double
cmk
=
desc
.
damping
*
desc
.
damping
-
4
*
desc
.
mass
*
desc
.
springConstant
;
...
...
@@ -27,7 +27,8 @@ abstract class _SpringSolution implements Simulatable {
class
_CriticalSolution
implements
_SpringSolution
{
final
double
_r
,
_c1
,
_c2
;
factory
_CriticalSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
factory
_CriticalSolution
(
SpringDescription
desc
,
double
distance
,
double
velocity
)
{
final
double
r
=
-
desc
.
damping
/
(
2.0
*
desc
.
mass
);
final
double
c1
=
distance
;
final
double
c2
=
velocity
/
(
r
*
distance
);
...
...
@@ -41,10 +42,10 @@ class _CriticalSolution implements _SpringSolution {
_c1
=
c1
,
_c2
=
c2
;
double
x
(
double
time
)
=>
(
_c1
+
_c2
*
time
)
*
Math
.
pow
(
M
ath
.
E
,
_r
*
time
);
double
x
(
double
time
)
=>
(
_c1
+
_c2
*
time
)
*
math
.
pow
(
m
ath
.
E
,
_r
*
time
);
double
dx
(
double
time
)
{
final
double
power
=
Math
.
pow
(
M
ath
.
E
,
_r
*
time
);
final
double
power
=
math
.
pow
(
m
ath
.
E
,
_r
*
time
);
return
_r
*
(
_c1
+
_c2
*
time
)
*
power
+
_c2
*
power
;
}
}
...
...
@@ -53,12 +54,12 @@ class _OverdampedSolution implements _SpringSolution {
final
double
_r1
,
_r2
,
_c1
,
_c2
;
factory
_OverdampedSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
SpringDesc
ription
desc
,
double
distance
,
double
velocity
)
{
final
double
cmk
=
desc
.
damping
*
desc
.
damping
-
4
*
desc
.
mass
*
desc
.
springConstant
;
final
double
r1
=
(-
desc
.
damping
-
M
ath
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
final
double
r2
=
(-
desc
.
damping
+
M
ath
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
final
double
r1
=
(-
desc
.
damping
-
m
ath
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
final
double
r2
=
(-
desc
.
damping
+
m
ath
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
final
double
c2
=
(
velocity
-
r1
*
distance
)
/
(
r2
-
r1
);
final
double
c1
=
distance
-
c2
;
...
...
@@ -74,18 +75,18 @@ class _OverdampedSolution implements _SpringSolution {
SpringType
get
type
=>
SpringType
.
overDamped
;
double
x
(
double
time
)
=>
(
_c1
*
Math
.
pow
(
Math
.
E
,
_r1
*
time
)
+
_c2
*
Math
.
pow
(
M
ath
.
E
,
_r2
*
time
));
(
_c1
*
math
.
pow
(
math
.
E
,
_r1
*
time
)
+
_c2
*
math
.
pow
(
m
ath
.
E
,
_r2
*
time
));
double
dx
(
double
time
)
=>
(
_c1
*
_r1
*
Math
.
pow
(
M
ath
.
E
,
_r1
*
time
)
+
_c2
*
_r2
*
Math
.
pow
(
M
ath
.
E
,
_r2
*
time
));
double
dx
(
double
time
)
=>
(
_c1
*
_r1
*
math
.
pow
(
m
ath
.
E
,
_r1
*
time
)
+
_c2
*
_r2
*
math
.
pow
(
m
ath
.
E
,
_r2
*
time
));
}
class
_UnderdampedSolution
implements
_SpringSolution
{
final
double
_w
,
_r
,
_c1
,
_c2
;
factory
_UnderdampedSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
final
double
w
=
M
ath
.
sqrt
(
4.0
*
desc
.
mass
*
desc
.
springConstant
-
SpringDesc
ription
desc
,
double
distance
,
double
velocity
)
{
final
double
w
=
m
ath
.
sqrt
(
4.0
*
desc
.
mass
*
desc
.
springConstant
-
desc
.
damping
*
desc
.
damping
)
/
(
2.0
*
desc
.
mass
);
final
double
r
=
-(
desc
.
damping
/
2.0
*
desc
.
mass
);
...
...
@@ -103,13 +104,13 @@ class _UnderdampedSolution implements _SpringSolution {
SpringType
get
type
=>
SpringType
.
underDamped
;
double
x
(
double
time
)
=>
Math
.
pow
(
M
ath
.
E
,
_r
*
time
)
*
(
_c1
*
Math
.
cos
(
_w
*
time
)
+
_c2
*
M
ath
.
sin
(
_w
*
time
));
double
x
(
double
time
)
=>
math
.
pow
(
m
ath
.
E
,
_r
*
time
)
*
(
_c1
*
math
.
cos
(
_w
*
time
)
+
_c2
*
m
ath
.
sin
(
_w
*
time
));
double
dx
(
double
time
)
{
final
double
power
=
Math
.
pow
(
M
ath
.
E
,
_r
*
time
);
final
double
cosine
=
M
ath
.
cos
(
_w
*
time
);
final
double
sine
=
M
ath
.
sin
(
_w
*
time
);
final
double
power
=
math
.
pow
(
m
ath
.
E
,
_r
*
time
);
final
double
cosine
=
m
ath
.
cos
(
_w
*
time
);
final
double
sine
=
m
ath
.
sin
(
_w
*
time
);
return
power
*
(
_c2
*
_w
*
cosine
-
_c1
*
_w
*
sine
)
+
_r
*
power
*
(
_c2
*
sine
+
_c1
*
cosine
);
...
...
packages/newton/test/newton_test.dart
View file @
7b77043e
...
...
@@ -58,27 +58,32 @@ void main() {
});
test
(
'spring_types'
,
()
{
var
crit
=
new
SpringSimulation
(
new
SpringDesc
.
withDampingRatio
(
1.0
,
100.0
,
1
.0
),
0.0
,
300.0
,
0.0
);
var
crit
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100
.0
),
0.0
,
300.0
,
0.0
);
expect
(
crit
.
type
,
SpringType
.
criticallyDamped
);
var
under
=
new
SpringSimulation
(
new
SpringDesc
.
withDampingRatio
(
1.0
,
100.0
,
0.75
),
0.0
,
300.0
,
0.0
);
crit
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
1.0
),
0.0
,
300.0
,
0.0
);
expect
(
crit
.
type
,
SpringType
.
criticallyDamped
);
var
under
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
0.75
),
0.0
,
300.0
,
0.0
);
expect
(
under
.
type
,
SpringType
.
underDamped
);
var
over
=
new
SpringSimulation
(
new
SpringDesc
.
withDampingRatio
(
1.0
,
100.0
,
1.25
),
0.0
,
300.0
,
0.0
);
var
over
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
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
SpringSimulation
(
new
SpringDesc
(
1.0
,
100.0
,
20.0
),
0.0
,
20.0
,
20.0
);
var
other
=
new
SpringSimulation
(
new
SpringDescription
(
mass:
1.0
,
springConstant:
100.0
,
damping:
20.0
),
0.0
,
20.0
,
20.0
);
expect
(
other
.
type
,
SpringType
.
criticallyDamped
);
});
test
(
'crit_spring'
,
()
{
var
crit
=
new
SpringSimulation
(
new
SpringDesc
.
withDampingRatio
(
1.0
,
100.0
,
1.0
),
0.0
,
500.0
,
0.0
);
var
crit
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
1.0
),
0.0
,
500.0
,
0.0
);
expect
(
crit
.
type
,
SpringType
.
criticallyDamped
);
expect
(
crit
.
isDone
(
0.0
),
false
);
...
...
@@ -99,8 +104,8 @@ void main() {
});
test
(
'overdamped_spring'
,
()
{
var
over
=
new
SpringSimulation
(
new
SpringDesc
.
withDampingRatio
(
1.0
,
100.0
,
1.25
),
0.0
,
500.0
,
0.0
);
var
over
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
1.25
),
0.0
,
500.0
,
0.0
);
expect
(
over
.
type
,
SpringType
.
overDamped
);
expect
(
over
.
isDone
(
0.0
),
false
);
...
...
@@ -118,8 +123,8 @@ void main() {
});
test
(
'underdamped_spring'
,
()
{
var
under
=
new
SpringSimulation
(
new
SpringDesc
.
withDampingRatio
(
1.0
,
100.0
,
0.25
),
0.0
,
300.0
,
0.0
);
var
under
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
0.25
),
0.0
,
300.0
,
0.0
);
expect
(
under
.
type
,
SpringType
.
underDamped
);
expect
(
under
.
isDone
(
0.0
),
false
);
...
...
@@ -135,7 +140,8 @@ void main() {
});
test
(
'test_kinetic_scroll'
,
()
{
var
spring
=
new
SpringDesc
.
withDampingRatio
(
1.0
,
50.0
,
0.5
);
var
spring
=
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
50.0
,
ratio:
0.5
);
var
scroll
=
new
ScrollSimulation
(
100.0
,
800.0
,
0.0
,
300.0
,
spring
,
0.3
);
...
...
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