2013-04-13 106 views
0

如果我在x的範圍內的雙 - Y,I如何可以擴展這種雙重到新的範圍內的 - B爪哇雙縮放

例如從-1轉換 - > 1到0 - > 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)

+0

乾杯,將接受時,可以這樣做:D – marscom

+0

+ 1向我介紹'lerp'。你已經度過了我的一天。 :)請將第三個參數名稱更改爲'scale'。 – OldCurmudgeon

+0

對於lerp和答案+1,lerp很酷 – marscom