Performs a Hermite spline interpolation. - Java java.lang

Java examples for java.lang:Math Algorithm

Description

Performs a Hermite spline interpolation.

Demo Code


//package com.java2s;

public class Main {
    /**//w  w  w.  j  ava 2  s .c  o  m
     * Performs a Hermite spline interpolation.
     * 
     * @param value1
     *        Source position.
     * @param tangent1
     *        Source tangent.
     * @param value2
     *        Source position.
     * @param tangent2
     *        Source tangent.
     * @param amount
     *        Weighting factor.
     * @return The result of the Hermite spline interpolation.
     */
    public static float hermite(float value1, float tangent1, float value2,
            float tangent2, float amount) {
        // All transformed to double not to lose precission
        // Otherwise, for high numbers of param:amount the result is NaN instead
        // of Infinity
        double v1 = value1, v2 = value2, t1 = tangent1, t2 = tangent2, s = amount, result;
        double sCubed = s * s * s;
        double sSquared = s * s;

        if (amount == 0f)
            result = value1;
        else if (amount == 1f)
            result = value2;
        else
            result = (2 * v1 - 2 * v2 + t2 + t1) * sCubed
                    + (3 * v2 - 3 * v1 - 2 * t1 - t2) * sSquared + t1 * s
                    + v1;
        return (float) result;
    }
}

Related Tutorials