Java Reference
In-Depth Information
Listing 5-9.
StepInterpolator.fx
public class StepInterpolator extends Interpolator{
public var interpolator = Interpolator.LINEAR;
public var steps = 7;
public override function interpolate(start:Object,end:Object,fraction:Number):Object{
var s = (start as Number);
var e = (end as Number);
var range = e-s;
var value:Number = interpolator.interpolate(start, end, fraction) as Number;
var stepSize = range/steps;
var total = e;
for (r in [1..steps-1]){
if (value < stepSize*(r)){
total = (r-1)*(range/(steps - 1.0));
break;
}
}
return total;
}
}
When creating a
StepInterpolator
as shown in Listing 5-9, the number of
steps
can be specified, as
well as another
Interpolator
to be broken into discrete steps. The function
interpolate
passes the
variables
start
,
end,
and
fraction
into the reference
interpolator
. The returned value is then checked
to see which step it most closely matches. Figure 5-8 shows the step function being applied to the default
linear interpolator, and Figure 5-9 shows a step interpolator applied to the polynomial interpolator used
earlier.