价值重新映射

ack*_*ack 18 math processing

处理具有我一直使用的强大功能:

map(value, low1, high1, low2, high2)
Run Code Online (Sandbox Code Playgroud)

http://processing.org/reference/map_.html

它将value(具有预期范围的low1to high1)重新映射到目标范围low2to high2).

我想了解它背后的数学因此我可以在其他语言中使用它.有人想给我一块骨头并帮我逆向工程吗?我知道这是一个重新缩放并重新抵消的因素......今天早上感觉脑死了.

Cas*_*bel 33

根据你的描述,它应该是这样做的,对吗?

low2 + (value - low1) * (high2 - low2) / (high1 - low1)
Run Code Online (Sandbox Code Playgroud)

找出你进入第一个范围的距离,用距离的大小比例来衡量该距离,以及你应该到第二个范围的距离.


Kev*_*man 6

处理是开源的。您可以在此处查看该map()功能。

static public final float map(float value,
                                float start1, float stop1,
                                float start2, float stop2) {
    float outgoing =
      start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
    String badness = null;
    if (outgoing != outgoing) {
      badness = "NaN (not a number)";

    } else if (outgoing == Float.NEGATIVE_INFINITY ||
               outgoing == Float.POSITIVE_INFINITY) {
      badness = "infinity";
    }
    if (badness != null) {
      final String msg =
        String.format("map(%s, %s, %s, %s, %s) called, which returns %s",
                      nf(value), nf(start1), nf(stop1),
                      nf(start2), nf(stop2), badness);
      PGraphics.showWarning(msg);
    }
    return outgoing;
  }
Run Code Online (Sandbox Code Playgroud)

具体来说,您正在寻找这行代码:

float outgoing =
      start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
Run Code Online (Sandbox Code Playgroud)