Java Reference
In-Depth Information
Quadratic Interpolator
While the spline interpolator can produce a large range of curves, sometimes it is desirable to create an
interpolator that uses a quadratic function to describe this curve. For example, the effect of gravity on a
falling body is best modeled with a quadratic function. A quadratic function has the form:
ax 2 + bx + c = 0
Since it is expected that interpolators produce curves that intersect with 0,0 and 1,1, the formula
above can be simplified to the following:
ax 2 + bx = 1
The variable c can be removed since we always want the curve to pass through the origin, as c
describes where the curve intersects with the y axis. Now, changing a and b will change the shape of the
curve, but the curve must pass through the point 1,1, so only one of the two coefficients can be set by the
application. The other must be calculated based on the value of the other, in order to keep the equation
balanced. This example assumes that a can be set by the application and b will be solved for.
Listing 5-5 shows how a quadratic function is expressed in code.
Listing 5-5. QuadraticInterpolator.fx
public class QuadraticInterpolator extends SimpleInterpolator {
public var a = 1.0 on replace{
b = 1.0 - a;
var b:Number = 1.0 - a;
public override function curve(fraction:Number):Number{
return (a*fraction*fraction + b*fraction);
In Listing 5-5, the function curve takes a fraction and calculates a value based on the values of a and
b . This function will always return 0.0 for the fraction 0.0 (since anything times 0 is 0) and will also return
1.0 for 1.0, since as a changes, b will be recalculated to balance the equation. The screenshot in Listing 5-4
shows this interpolator in action.
The slider at the bottom of Figure 5-4 allows the value of a to be set, and the calculated value of b is
displayed. Note the parabolic curve produced.
Search WWH ::

Custom Search