01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.motion;
21 
22 import js.Vars;
23 import jsx.core.ArrayLikes;
24 
25 /**
26  * <p>Defines motions with the specified speed motion.</p>
27  * <p>A speed motion is a motion that provides speed data for other motions.</p>
28  * 
29  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
30  */
31 public final class Speed extends Interpolator
32 {
33     private final Motion m;
34 
35     /**
36      * <p>The typical constructor to construct a motion of this type.</p>
37      * @param m Specifies a motion that has speeds for the motion being created.
38      * @since 1.0
39      */
40     public Speed(Motion m) {
41         super(new Vars<Number>().var());
42         this.m = m;
43     }
44 
45     /**
46      * <p>Resets the motion object to provide data for a specified number of steps.</p>
47      * @param steps The total number of steps in which this motion object will provide 
48      * stepping data.
49      * @since 1.0
50      */
51     @Override
52     public final void init(int steps) {
53         if (steps == ArrayLikes.length(values)) {
54             return;
55         }
56         m.init(steps);
57         ArrayLikes.splice(values, 0);
58         double x = 0;
59         for (int i = 0; i < steps; i++) {
60             x = x + m.get(i);
61             values.set(i, x);
62         }
63         normalize(values);
64     }
65 
66     /**
67      * <p>Gets the datum of a float number for the specified step.</p>
68      * @param i The step number. It must be within the number of steps previously initialized 
69      * with the method {@link #init(int)}.
70      * @return The float datum for the <tt>i</tt>th step.
71      * @since 1.0
72      */
73     @Override
74     public final double get(int i) {
75         return values.get(i).doubleValue();
76     }
77 }