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
8c91b9b7
Commit
8c91b9b7
authored
Jul 06, 2015
by
Chinmay Garde
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement simulation groups for kinetic scrolling
parent
5a439792
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
144 additions
and
45 deletions
+144
-45
newton.dart
packages/newton/lib/newton.dart
+1
-1
fall.dart
packages/newton/lib/src/fall.dart
+0
-8
scroll.dart
packages/newton/lib/src/scroll.dart
+45
-2
simulation.dart
packages/newton/lib/src/simulation.dart
+1
-3
simulation_group.dart
packages/newton/lib/src/simulation_group.dart
+42
-0
spring_solution.dart
packages/newton/lib/src/spring_solution.dart
+55
-31
No files found.
packages/newton/lib/newton.dart
View file @
8c91b9b7
...
...
@@ -7,9 +7,9 @@ library newton;
import
'dart:math'
as
Math
;
part
'src/simulation.dart'
;
part
'src/simulation_group.dart'
;
part
'src/utils.dart'
;
part
'src/fall.dart'
;
part
'src/friction.dart'
;
part
'src/gravity.dart'
;
part
'src/scroll.dart'
;
...
...
packages/newton/lib/src/fall.dart
deleted
100644 → 0
View file @
5a439792
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of
newton
;
// TODO(csg): Composite simulation
class
Fall
{}
packages/newton/lib/src/scroll.dart
View file @
8c91b9b7
...
...
@@ -4,5 +4,48 @@
part of
newton
;
// TODO(csg): Composite simulation
class
Scroll
{}
/// Simulates kinetic scrolling behavior between a leading and trailing
/// boundary. Friction is applied within the extends and a spring action applied
/// at the boundaries. This simulation can only step forward.
class
Scroll
extends
SimulationGroup
{
final
double
_leadingExtent
;
final
double
_trailingExtent
;
final
SpringDesc
_springDesc
;
final
double
_drag
;
bool
_isSpringing
=
false
;
Simulation
_currentSimulation
;
Scroll
(
double
position
,
double
velocity
,
double
leading
,
double
trailing
,
SpringDesc
spring
,
double
drag
)
:
_leadingExtent
=
leading
,
_trailingExtent
=
trailing
,
_springDesc
=
spring
,
_drag
=
drag
{
_chooseSimulation
(
position
,
velocity
);
}
@override
void
step
(
double
time
)
=>
_chooseSimulation
(
_currentSimulation
.
x
(
time
),
_currentSimulation
.
dx
(
time
));
@override
Simulation
get
currentSimulation
=>
_currentSimulation
;
void
_chooseSimulation
(
double
position
,
double
velocity
)
{
/// This simulation can only step forward
if
(!
_isSpringing
)
{
if
(
position
>
_trailingExtent
)
{
_isSpringing
=
true
;
_currentSimulation
=
new
Spring
(
_springDesc
,
position
,
_trailingExtent
,
velocity
);
}
else
if
(
position
<
_leadingExtent
)
{
_isSpringing
=
true
;
_currentSimulation
=
new
Spring
(
_springDesc
,
position
,
_leadingExtent
,
velocity
);
}
}
else
if
(
_currentSimulation
==
null
)
{
_currentSimulation
=
new
Friction
(
_drag
,
position
,
velocity
);
}
}
}
packages/newton/lib/src/simulation.dart
View file @
8c91b9b7
...
...
@@ -16,9 +16,7 @@ abstract class Simulatable {
/// instance of a simulation and query the same for the position and velocity
/// of the body at a given interval.
///
/// Note: All operation on subclasses of Simulation are idempotent. Composite
/// simulations are not guaranteed to be idempotent however. FIXME(csg): How do
/// I make this apparent?
/// Note: All operations on subclasses of Simulation are idempotent.
abstract
class
Simulation
implements
Simulatable
{
/// Returns if the simulation is done at a given time
...
...
packages/newton/lib/src/simulation_group.dart
0 → 100644
View file @
8c91b9b7
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of
newton
;
/// The abstract base class of all composite simulations. Concrete subclasses
/// must implement the appropriate methods to select the appropriate simulation
/// at a given time interval. The simulation group takes care to call the `step`
/// method at appropriate intervals. If more fine grained control over the the
/// step is necessary, subclasses may override the `Simulatable` methods.
abstract
class
SimulationGroup
extends
Simulation
{
Simulation
get
currentSimulation
;
void
step
(
double
time
);
double
x
(
double
time
)
{
_stepIfNecessary
(
time
);
return
currentSimulation
.
x
(
time
);
}
double
dx
(
double
time
)
{
_stepIfNecessary
(
time
);
return
currentSimulation
.
dx
(
time
);
}
@override
bool
isDone
(
double
time
)
{
_stepIfNecessary
(
time
);
return
currentSimulation
.
isDone
(
time
);
}
double
_lastStep
=
-
1.0
;
void
_stepIfNecessary
(
double
time
)
{
if
(
_nearEqual
(
_lastStep
,
time
))
{
return
;
}
_lastStep
=
time
;
step
(
time
);
}
}
packages/newton/lib/src/spring_solution.dart
View file @
8c91b9b7
...
...
@@ -23,63 +23,87 @@ abstract class _SpringSolution implements Simulatable {
}
class
_CriticalSolution
implements
_SpringSolution
{
double
r
,
c1
,
c2
;
final
double
_r
,
_c1
,
_
c2
;
_CriticalSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
r
=
-
desc
.
damping
/
(
2.0
*
desc
.
mass
);
c1
=
distance
;
c2
=
velocity
/
(
r
*
distance
);
factory
_CriticalSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
final
double
r
=
-
desc
.
damping
/
(
2.0
*
desc
.
mass
);
final
double
c1
=
distance
;
final
double
c2
=
velocity
/
(
r
*
distance
);
return
new
_CriticalSolution
.
withArgs
(
r
,
c1
,
c2
);
}
double
x
(
double
time
)
=>
(
c1
+
c2
*
time
)
*
Math
.
pow
(
Math
.
E
,
r
*
time
);
_CriticalSolution
.
withArgs
(
double
r
,
double
c1
,
double
c2
)
:
_r
=
r
,
_c1
=
c1
,
_c2
=
c2
;
double
x
(
double
time
)
=>
(
_c1
+
_c2
*
time
)
*
Math
.
pow
(
Math
.
E
,
_r
*
time
);
double
dx
(
double
time
)
{
final
double
power
=
Math
.
pow
(
Math
.
E
,
r
*
time
);
return
r
*
(
c1
+
c2
*
time
)
*
power
+
c2
*
power
;
final
double
power
=
Math
.
pow
(
Math
.
E
,
_
r
*
time
);
return
_r
*
(
_c1
+
_c2
*
time
)
*
power
+
_
c2
*
power
;
}
}
class
_OverdampedSolution
implements
_SpringSolution
{
double
r1
,
r2
,
c1
,
c2
;
final
double
_r1
,
_r2
,
_c1
,
_
c2
;
_OverdampedSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
double
cmk
=
factory
_OverdampedSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
final
double
cmk
=
desc
.
damping
*
desc
.
damping
-
4
*
desc
.
mass
*
desc
.
springConstant
;
r1
=
(-
desc
.
damping
-
Math
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
r2
=
(-
desc
.
damping
+
Math
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
c2
=
(
velocity
-
r1
*
distance
)
/
(
r2
-
r1
);
c1
=
distance
-
c2
;
final
double
r1
=
(-
desc
.
damping
-
Math
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
final
double
r2
=
(-
desc
.
damping
+
Math
.
sqrt
(
cmk
))
/
(
2.0
*
desc
.
mass
);
final
double
c2
=
(
velocity
-
r1
*
distance
)
/
(
r2
-
r1
);
final
double
c1
=
distance
-
c2
;
return
new
_OverdampedSolution
.
withArgs
(
r1
,
r2
,
c1
,
c2
);
}
_OverdampedSolution
.
withArgs
(
double
r1
,
double
r2
,
double
c1
,
double
c2
)
:
_r1
=
r1
,
_r2
=
r2
,
_c1
=
c1
,
_c2
=
c2
;
double
x
(
double
time
)
=>
(
c1
*
Math
.
pow
(
Math
.
E
,
r1
*
time
)
+
c2
*
Math
.
pow
(
Math
.
E
,
r2
*
time
));
(
_c1
*
Math
.
pow
(
Math
.
E
,
_r1
*
time
)
+
_c2
*
Math
.
pow
(
Math
.
E
,
_
r2
*
time
));
double
dx
(
double
time
)
=>
(
c1
*
r1
*
Math
.
pow
(
Math
.
E
,
r1
*
time
)
+
c2
*
r2
*
Math
.
pow
(
Math
.
E
,
r2
*
time
));
double
dx
(
double
time
)
=>
(
_c1
*
_r1
*
Math
.
pow
(
Math
.
E
,
_
r1
*
time
)
+
_c2
*
_r2
*
Math
.
pow
(
Math
.
E
,
_
r2
*
time
));
}
class
_UnderdampedSolution
implements
_SpringSolution
{
double
w
,
r
,
c1
,
c2
;
final
double
_w
,
_r
,
_c1
,
_
c2
;
_UnderdampedSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
w
=
Math
.
sqrt
(
4.0
*
desc
.
mass
*
desc
.
springConstant
-
factory
_UnderdampedSolution
(
SpringDesc
desc
,
double
distance
,
double
velocity
)
{
final
double
w
=
Math
.
sqrt
(
4.0
*
desc
.
mass
*
desc
.
springConstant
-
desc
.
damping
*
desc
.
damping
)
/
(
2.0
*
desc
.
mass
);
r
=
-(
desc
.
damping
/
2.0
*
desc
.
mass
);
c1
=
distance
;
c2
=
(
velocity
-
r
*
distance
)
/
w
;
final
double
r
=
-(
desc
.
damping
/
2.0
*
desc
.
mass
);
final
double
c1
=
distance
;
final
double
c2
=
(
velocity
-
r
*
distance
)
/
w
;
return
new
_UnderdampedSolution
.
withArgs
(
w
,
r
,
c1
,
c2
);
}
double
x
(
double
time
)
=>
Math
.
pow
(
Math
.
E
,
r
*
time
)
*
(
c1
*
Math
.
cos
(
w
*
time
)
+
c2
*
Math
.
sin
(
w
*
time
));
_UnderdampedSolution
.
withArgs
(
double
w
,
double
r
,
double
c1
,
double
c2
)
:
_w
=
w
,
_r
=
r
,
_c1
=
c1
,
_c2
=
c2
;
double
x
(
double
time
)
=>
Math
.
pow
(
Math
.
E
,
_r
*
time
)
*
(
_c1
*
Math
.
cos
(
_w
*
time
)
+
_c2
*
Math
.
sin
(
_w
*
time
));
double
dx
(
double
time
)
{
final
double
power
=
Math
.
pow
(
Math
.
E
,
r
*
time
);
final
double
cosine
=
Math
.
cos
(
w
*
time
);
final
double
sine
=
Math
.
sin
(
w
*
time
);
final
double
power
=
Math
.
pow
(
Math
.
E
,
_
r
*
time
);
final
double
cosine
=
Math
.
cos
(
_
w
*
time
);
final
double
sine
=
Math
.
sin
(
_
w
*
time
);
return
power
*
(
c2
*
w
*
cosine
-
c1
*
w
*
sine
)
+
r
*
power
*
(
c2
*
sine
+
c1
*
cosine
);
return
power
*
(
_c2
*
_w
*
cosine
-
_c1
*
_
w
*
sine
)
+
_r
*
power
*
(
_c2
*
sine
+
_
c1
*
cosine
);
}
}
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