Value Remapping

天大地大妈咪最大 提交于 2019-12-02 19:11:48

From your description, it ought to be doing this, right?

low2 + (value - low1) * (high2 - low2) / (high1 - low1)

Find how far you are into the first range, scale that distance by the ratio of sizes of the ranges, and that's how far you should be into the second range.

Processing is open-source. You can view the map() function here.

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;
  }

Specifically, you're looking for this line of code:

float outgoing =
      start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));

I would like to add that is sometimes useful to find the factor between the low1 and high1 so that you can modulate it with a curve before using the factor as a LERP's t.

So, t = (value-low1)/(high1-low1) to get the relative position of value in the line low1 to high1.

Then you can modulate t with some curve filter for example, gamma, bias, gain, etc As also clamp the t between 0 and 1 if you to restrict values that go over the set lows and highs.

And then use the t for the LERP between low2 and high2 like: finalvalue = low2*(1-t) + high2*t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!