如何避免"陷阱"使用线性插值?

时间:2017-06-08 14:14:18

标签: c++ linear-interpolation

我有一个范围a(起始范围)和范围b(目标范围),我需要将值aX从范围a缩放到范围b。代码是基本的线性插值:

double LinearInterpolation(double a0, double a1, double b0, double b1, double aX) {
    return b0 + (b1 - b0) * (aX - a0) / (a1 - a0);
}

事实是:范围b可以是1.0到1.0的0长度。但是当我达到那个范围时,我得到的值是"陷阱",并且无法返回。 Here's一个例子:

int main ()
{
    double a0 = 0.4;
    double a1 = 1.0;
    double b0 = 0.6;
    double b1 = 1.0;

    double aX = 0.58;

    aX = LinearInterpolation(a0, a1, b0, b1, aX);
    std::cout << aX << std::endl;

    b0 = 1.0;
    aX = LinearInterpolation(a0, a1, b0, b1, aX);
    std::cout << aX << std::endl;

    b0 = 0.6;
    aX = LinearInterpolation(a0, a1, b0, b1, aX);
    std::cout << aX << std::endl;    
}

b0为0之前,它会正确缩放aX值。当我到达b0 = b1 = 1.0时,我无法返回(aX始终为1.0,因为X = a1,因此始终是b0 + (b1 - b0) => b1

我该如何避免这种情况?

1 个答案:

答案 0 :(得分:1)

  

我该如何避免这种情况?

不要存储实际值,而是存储范围内的相对位置:

struct value_in_range {
  double normalized_offset;
  double in_range (double from, double to) const {
    return from + (to - from) * normalized_offset;
  }
};

value_in_range make_value_in_range (double from, double value, double to) {
  // Add assertions to avoid misuse
  return {(value - from) / (to - from)};
}

用你的例子:

int main ()
{
    double a0 = 0.4;
    double a1 = 1.0;
    double b0 = 0.6;
    double b1 = 1.0;

    double aX = 0.58;

    value_in_range X = make_value_in_range (a0, aX, a1);

    std::cout << X.in_range(b0, b1) << std::endl;

    b0 = 1.0;
    std::cout << X.in_range(b0, b1) << std::endl;

    b0 = 0.6;
    std::cout << X.in_range(b0, b1) << std::endl;
}

您也可以在原始代码中设置aX