Java interpolate interpolateClamp(final double position, final double startPosition, final double endPosition, final double startValue, final double endValue)

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

Description

Calculates a linearily interpolated value, given a start value and position, an end value and position, 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 double interpolateClamp(final double position, final double startPosition,
        final double endPosition, final double startValue, final double endValue) 

Method Source Code

//package com.java2s;
/*/*w w  w  .j a  va2 s. c om*/
 *    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 linearily interpolated value, given a start value and position, an end value and position,
     * and the position to get the value at.
     * <p/>
     * If the position is outside start or end position, it is treated as if it was at the start or end position respectively.
     * <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
     */
    public static double interpolateClamp(final double position, final double startPosition,
            final double endPosition, final double startValue, final double endValue) {
        // Clamp
        double p = position;
        if (p < startPosition) {
            p = startPosition;
        } else if (p > endPosition) {
            p = endPosition;
        }

        return interpolate(p, startPosition, endPosition, startValue, endValue);
    }

    /**
     * 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(String value)
  2. interpolate2D(final float wi, final float wj, final float x00, final float x10, final float x01, final float x11)
  3. interpolateAngle(double oldAngle, double newAngle, double scale)
  4. interpolateAO(float a, float b, float c, float d)
  5. interpolateBrightness(int a, int b, int c, int d)
  6. interpolateColor(double x, double y, int c0, int c1, int c2, int c3)
  7. interpolateColor(int a, int b, float w)
  8. interpolateColor(int c1, int c2, int st, int sts)
  9. interpolateColor(int rgba1, int rgba2, float percent)