0

如果我在 x - y 范围内有一个双精度,我如何将这个双精度缩放到新范围 a - b

例如从 -1 -> 1 转换为 0 -> 1

谢谢马丁

4

1 回答 1

3
/**
 * @param value The incoming value to be converted
 * @param low1  Lower bound of the value's current range
 * @param high1 Upper bound of the value's current range
 * @param low2  Lower bound of the value's target range
 * @param high2 Upper bound of the value's target range
 */
public static final double map(double value, double low1, double high1, double low2, double high2) {

    double diff = value - low1;
    double proportion = diff / (high1 - low1);

    return lerp(low2, high2, proportion);
}

// Linearly interpolate between two values
public static final double lerp(double value1, double value2, double amt) {
    return ((value2 - value1) * amt) + value1;
}

http://developmentality.wordpress.com/2009/12/15/useful-utility-functions-0-of-n/

在你的情况下,你会打电话map(x, -1, 1, 0, 1)

于 2013-04-13T00:27:53.867 回答