algorithm - 从 "chopped"输入范围返回值的函数

标签 algorithm interpolation

我正在寻找一个函数或算法,对于指定范围内的值,将返回相同范围内的值,但基于值所在的印章/除法。很难解释 - 一些基于预期的输出在这个空壳上

function choppedRange(value, min, max, chops) {
    // value - a value in range min to max
    // chops - integer defining how many "subranges" or "chops" to return values from
    ...
}

// Map (linear conversion) input value in range oldMin -> oldMax
// to a value in range newMin -> newMax
function remap(oldValue, oldMin, oldMax, newMin, newMax) {
    return (((oldValue - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;
}

Illustration 插图试图显示的内容如下:

  1. 确定输入值(蓝点)在哪个印章中。 (假设它的值为 0.35...)
  2. 0,25 范围内的值(蓝色)0.35 重新映射(上面的函数)到 0.5minmax 参数范围内,例如01:remap(0.35, 0.25, 0.5, 0, 1)
  3. 带有示例值的输出(绿点)应为 0.3999...

希望你能明白我的意思。

如您所见,我已完成重新映射 - 但我无法确定 remap 输入的“截断”值 - 如下所示:

remap(input_value, chop_min, chop_max, min, max)

我想根据 choppedRange 中的 chops 参数找到 chop_minchop_max

function choppedRange(value, min, max, chops) {
    // Figure out chop_min and chop_max
    ...
    return remap(value, chop_min, chop_max, min, max)
}

最佳答案

我完成了这个实现

function choppedRange(value, min, max, chops) {
  // value - a value in range min to max
  // chops - integer defining how many "subranges" or "chops" to return values from
  var chopMin = min, chopMax = max, chopValue = max / chops, i, c
  for (i = 1; i <= chops; i++) {
    c = chopValue * i
    if (c < value)
      chopMin = c
    if (c >= value) {
      chopMax = c
      break;
    }
  }
  return (((value - chopMin) * (max - min)) / (chopMax - chopMin)) + min
}

var r = choppedRange(0, 0, 1, 4)
console.log('Result',r)
r = choppedRange(0.35, 0, 1, 4)
console.log('Result',r)
r = choppedRange(0.5, 0, 1, 4)
console.log('Result',r)
r = choppedRange(0.6, 0, 1, 4)
console.log('Result',r)
r = choppedRange(1, 0, 1, 4)
console.log('Result',r)

关于algorithm - 从 "chopped"输入范围返回值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160694/

相关文章:

reactjs - 如何在 Next-i18next/React i18next 中插入一个 Link 组件来改变文本中的位置

r - 当列在 NA 结束时插入 NA 值

algorithm - 关于 HyperOperation 的问题

algorithm - 检测一组非严格不等式中的不一致

python - 随机创建一个列和行中没有重复元素的矩阵

c# - 当前日期公式中特定日期的最后日期

python - python中的有界圆插值

c++ - 分散数据的二维值插值

algorithm - 在网格上插值的最佳算法

algorithm - 给定大量街道名称,测试文本是否包含该组街道名称中的一个街道名称的最有效方法是什么?