缩放两个数字范围

时间:2013-01-08 21:41:59

标签: javascript valueconverter

我记得在某些时候使用方程来做到这一点 - 你如何在Javascript中做到这一点?

插入两个数字范围:

rangeX = 1 (through) 10;
rangeY = 300.77 (through) 559.22;

输入范围Y比例中的值:

inputY = 328.17;

转换为rangeX scale中的比例值:

outputX = 1.43;

6 个答案:

答案 0 :(得分:25)

使用百分比:

xMax = 10;
xMin = 1;

yMax = 559.22;
yMin = 300.77;

percent = (inputY - yMin) / (yMax - yMin);
outputX = percent * (xMax - xMin) + xMin;

答案 1 :(得分:23)

function convertRange( value, r1, r2 ) { 
    return ( value - r1[ 0 ] ) * ( r2[ 1 ] - r2[ 0 ] ) / ( r1[ 1 ] - r1[ 0 ] ) + r2[ 0 ];
}

convertRange( 328.17, [ 300.77, 559.22 ], [ 1, 10 ] );

>>> 1.9541497388276272

答案 2 :(得分:1)

这是线性插值,与@oleq 的答案相同,但明确使用 min 和 max 变量而不是范围数组

function getScaledValue(value, sourceRangeMin, sourceRangeMax, targetRangeMin, targetRangeMax) {
    var targetRange = targetRangeMax - targetRangeMin;
    var sourceRange = sourceRangeMax - sourceRangeMin;
    return (value - sourceRangeMin) * targetRange / sourceRange + targetRangeMin;
}

答案 3 :(得分:0)

不保证数学,但我认为是这样的:

var xLow = 1;
var xHigh = 10:
var yLow = 300.77;
var yHigh = 559.22;

var inputY = 328.17;
var ouputX = xLow;

var scaleYOverX = (yHigh - yLow)/(xHigh - xLow);

if(inputY >= yLow && inputY <= yHigh) {
  outputX = (inputY - yLow)/scaleYOverX + xLow;
  alert(outputX);
} else {
  alert("Invalid input for Y scale");
}

答案 4 :(得分:0)

Swift 3 Bool扩展范围与否?

func translate(input : Float, inputMin: Float, inputMax: Float, outputMin: Float, outputMax: Float, extendRange: Bool? = false, log: Bool? = false) -> Float {

    //The actual translation function
    func translationResult(_ inputMinA: Float, _ inputMaxA: Float) -> Float {
        let myResult = outputMin + (outputMax - outputMin) * (input - inputMinA) / (inputMaxA - inputMinA)
        return myResult
    }

    // extendRange true means it'll return a value outside the range of inputMin and inputMax but still follow the ratio
    if extendRange! {
        return result = translationResult(inputMin, inputMax)

        if log! == true && input > inputMax || input < inputMin{
            print("outside range!")
        }
    } else {
        //Doesn't let value go outside range
        let inputMinA = min(inputMin, input)
        let inputMaxA = max(inputMax, input)

        return result = translationResult(inputMinA, inputMaxA)
    }
    }


    translate(input: 50, inputMin: 100, inputMax: 1000.0, outputMin: 1, outputMax: 10, extendRange: false) => 1
    translate(input: 50, inputMin: 100, inputMax: 1000.0, outputMin: 1, outputMax: 10, extendRange: true) => 0.5

答案 5 :(得分:0)

我将@Foggzie的答案变成了TypeScript函数和ES2016函数。

TypeScript:

const scale = (inputY: number, yRange: Array<number>, xRange: Array<number>): number => {
  const [xMin, xMax] = xRange;
  const [yMin, yMax] = yRange;

  const percent = (inputY - yMin) / (yMax - yMin);
  const outputX = percent * (xMax - xMin) + xMin;

  return outputX;
};

ES2016:

const scale = (inputY, yRange, xRange) => {
  const [xMin, xMax] = xRange;
  const [yMin, yMax] = yRange;

  const percent = (inputY - yMin) / (yMax - yMin);
  const outputX = percent * (xMax - xMin) + xMin;

  return outputX;
};