Java interpolate interpolateSmoothly(final float position, final float startPosition, final float endPosition, final float startValue, final float endValue)

Here you can find the source of interpolateSmoothly(final float position, final float startPosition, final float endPosition, final float startValue, final float endValue)

Description

Calculates a smoothly (cosine) interpolated value, given a start value, an end value, and the position to get the value at.

License

Open Source License

Parameter

Parameter Description
position a parameter
startPosition a parameter
endPosition a parameter
startValue a parameter
endValue a parameter

Declaration

public static float interpolateSmoothly(final float position, final float startPosition,
        final float endPosition, final float startValue, final float endValue) 

Method Source Code

//package com.java2s;
/*//  w w w .  jav  a 2s .c o m
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2007-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

public class Main {
    /**
     * Calculates a smoothly (cosine) interpolated value, given a start value, an end value,
     * and the position to get the value at.
     *
     * @param position
     * @param startPosition
     * @param endPosition
     * @param startValue
     * @param endValue
     */
    public static float interpolateSmoothly(final float position, final float startPosition,
            final float endPosition, final float startValue, final float endValue) {
        final float relativePosition = (position - startPosition) / (endPosition - startPosition);

        // Clamp values at edges
        float result;
        if (relativePosition <= 0) {
            result = startValue;
        } else if (relativePosition >= 1) {
            result = endValue;
        } else {
            // Cosine interpolation
            final double relativeSmoothPosition = (1.0 - Math.cos(relativePosition * Math.PI)) / 2.0;
            result = (float) (startValue * (1.0 - relativeSmoothPosition) + endValue * relativeSmoothPosition);
        }

        return result;
    }
}

Related

  1. interpolateLinear(float lastState, float currentState, float ip)
  2. interpolateProp(final String source, final String propName, final String defValue)
  3. interpolateRotation(float par1, float par2, float par3)
  4. interpolateRotation(float prevRotation, float nextRotation, float partialTick)
  5. interpolateRound(double a, double b, double t)
  6. interpolateValue(double current, double prev, float partialTickTime)
  7. interpolateValues(float prevVal, float nextVal, float partialTick)
  8. interpolateYawDegrees(float angle1, float ratio1, float angle2, float ratio2)
  9. interpolateYFromX(double x, double x0, double x1, double y0, double y1)