Java examples for java.lang:Math Curve
Interpolates between two values using a cubic equation.
public class Main{ /**/*from www . j av a 2 s .co m*/ * Interpolates between two values using a cubic equation. * * @param value1 * Source value. * @param value2 * Source value. * @param amount * Weighting value. * @return The interpolated value. */ public static float smoothStep(float value1, float value2, float amount) { // It is expected that 0 < amount < 1 // If amount < 0, return value1 // If amount > 1, return value2 float result = MathHelper.clamp(amount, 0f, 1f); result = MathHelper.hermite(value1, 0f, value2, 0f, result); return result; } /** * Restricts a value to be within a specified range. * * @param value * The value to clamp. * @param min * The minimum value. If {@code value} is less than {@code min}, {@code min} will be * returned. * @param max * The maximum value. If {@code value} is greater than {@code max}, {@code max} will be * returned. * @return The clamped value. */ public static float clamp(float value, float min, float max) { // First we check to see if we're greater than the max value = (value > max) ? max : value; // Then we check to see if we're less than the min. value = (value < min) ? min : value; // There's no check to see if min > max. return value; } /** * Restricts a value to be within a specified range. * * @param value * The value to clamp. * @param min * The minimum value. If {@code value} is less than {@code min}, {@code min} will be * returned. * @param max * The maximum value. If {@code value} is greater than {@code max}, {@code max} will be * returned. * @return The clamped value. */ public static int clamp(int value, int min, int max) { value = (value > max) ? max : value; value = (value < min) ? min : value; return value; } /** * 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; } }