Here you can find the source of interpolateClamp(final double position, final double startPosition, final double endPosition, final double startValue, final double endValue)
Parameter | Description |
---|---|
position | a parameter |
startPosition | a parameter |
endPosition | a parameter |
startValue | a parameter |
endValue | a parameter |
public static double interpolateClamp(final double position, final double startPosition, final double endPosition, final double startValue, final double endValue)
//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); } }