Java interpolate interpolate(float t, float a, float b)

Here you can find the source of interpolate(float t, float a, float b)

Description

Does a linear interpolation.

License

Open Source License

Parameter

Parameter Description
t when 0, the result is a, when 1, the result is b.
a value at start of range
b value at end of range

Return

an interpolated value between a and b (or beyond), with the relative position t.

Declaration

public static float interpolate(float t, float a, float b) 

Method Source Code

//package com.java2s;
/*/*from w  ww. j a v a  2 s .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 {
    /**
     * Does a linear interpolation.
     *
     * @param t when 0, the result is a, when 1, the result is b.
     * @param a value at start of range
     * @param b value at end of range
     *
     * @return an interpolated value between a and b (or beyond), with the relative position t.
     */
    public static float interpolate(float t, float a, float b) {
        return a + t * (b - a);
    }

    /**
     * Does a linear interpolation using doubles
     *
     * @param t when 0, the result is a, when 1, the result is b.
     * @param a value at start of range
     * @param b value at end of range
     *
     * @return an interpolated value between a and b (or beyond), with the relative position t.
     */
    public static double interpolate(double t, double a, double b) {
        return a + t * (b - a);
    }

    /**
     * Calculates a linearily interpolated value, given a start value and position, an end value and position,
     * and the position to get the value at.
     * <p/>
     * First calculates the relative position, then does a normal linear interpolation between the start and end value,
     * using the relative position as the interpolation factor.
     *
     * @param position
     * @param startPosition
     * @param endPosition
     * @param startValue
     * @param endValue
     *
     * @return the interpolated value
     */
    public static double interpolate(final double position, final double startPosition, final double endPosition,
            final double startValue, final double endValue) {
        final double relativePosition = (position - startPosition) / (endPosition - startPosition);
        return startValue + relativePosition * (endValue - startValue);
    }
}

Related

  1. interpolate(final int x1, final double y1, final int x2, final double y2, final double... f)
  2. interpolate(float a, float b, float d)
  3. interpolate(float bottom, float top, float ratio)
  4. interpolate(float firstColor, float secondColor, float stage, float maxStages)
  5. interpolate(float s, float e, float t)
  6. interpolate(float var1, float var2, float var3)
  7. interpolate(int a, int b, int theta, int reciprocal)
  8. interpolate(int i, int n, float u1, float u2, float du)
  9. interpolate(int start, int end, int n, int i)