如何将范围[a,b]标准化到[c,d],其中a映射到d, b映射到c ?

时间:2022-05-30 04:16:39

For example, i have a range of distances from [0,15] and i need to map them to the opacity of elements from [0,1] .

例如,我的距离为[0,15],我需要将它们映射到[0,1]元素的不透明度。

The element with distance 0 should have opacity 1 and element with distance 15 should have opacity 0.

距离为0的元素应该有不透明度1,而距离为15的元素应该有不透明度0。

I tried normal min-max normalization which maps 0 to 0 and 15 to 1 , but i need the opposite.

我尝试了正常的min-max标准化,将0映射到0和15到1,但是我需要相反的。

Formula which i use for regular normalization :

我经常使用的公式:

( (currentValue - min)/ (max - min) ) * (newMax - newMin) + newMin

(currentValue - min)/ (max - min)) * (newMax - newMin) + newMin。

3 个解决方案

#1


2  

The formula should be:

的公式应该是:

newMax - ((currentValue - min)/ (max - min)) * (newMax - newMin) 

((currentValue - min)/ (max - min)) * (newMax - newMin) calculate the new distance from the root to the current value, and as the root of the new range is put at newMax, so, we get our formula.

(currentValue - min)/ (max - min)) * (newMax - newMin)计算从根到当前值的新距离,作为新范围的根,我们得到了我们的公式。

#2


0  

(( (currentValue - min)/ (max - min) ) * (newMin - newMax) + newMax )

((currentValue - min)/ (max - min)) * (newMin - newMax) + newMax)

Should do the trick!

应该做的!

#3


0  

You should .reverse() your range array first. And from there it's just matching the boundaries of the two arrays. Possibly like so:

你应该先将你的range数组反转。从那里它只是匹配两个数组的边界。可能像这样:

var distances =  [0,1,2,3,4,5,6];
var opacity = {min: 0, max: 1};

var result = [];

distances.reverse().forEach(function(distance){
  var step = opacity.min+((opacity.max-opacity.min)/distances.length);
  var item = {
    distance: distance,
    opacity: opacity.min+distance*step
  }
  result.push(item);
});

console.log(result);

#1


2  

The formula should be:

的公式应该是:

newMax - ((currentValue - min)/ (max - min)) * (newMax - newMin) 

((currentValue - min)/ (max - min)) * (newMax - newMin) calculate the new distance from the root to the current value, and as the root of the new range is put at newMax, so, we get our formula.

(currentValue - min)/ (max - min)) * (newMax - newMin)计算从根到当前值的新距离,作为新范围的根,我们得到了我们的公式。

#2


0  

(( (currentValue - min)/ (max - min) ) * (newMin - newMax) + newMax )

((currentValue - min)/ (max - min)) * (newMin - newMax) + newMax)

Should do the trick!

应该做的!

#3


0  

You should .reverse() your range array first. And from there it's just matching the boundaries of the two arrays. Possibly like so:

你应该先将你的range数组反转。从那里它只是匹配两个数组的边界。可能像这样:

var distances =  [0,1,2,3,4,5,6];
var opacity = {min: 0, max: 1};

var result = [];

distances.reverse().forEach(function(distance){
  var step = opacity.min+((opacity.max-opacity.min)/distances.length);
  var item = {
    distance: distance,
    opacity: opacity.min+distance*step
  }
  result.push(item);
});

console.log(result);