其他语言中 map() (来自处理)的等价物是什么?

What is the equivalent of map() (from processing) in other languages?

详情见processing (a java based language) there's a very handy function called map() which takes 5 arguments. It re-maps a number from one range to another. Take a look at the official documentation here

我找不到它在其他语言中的等效项,尤其是在 java 中。或者是制作我自己的功能的最佳解决方案?

这是一个简单的计算:

static double map(double value, double start1, double stop1, double start2, double stop2) {
    return (value - start1) / (stop1 - start1) * (stop2 - start2) + start2;
}

这只是插入一个值,相当于(在处理参数名称之后):

map(value, start1, stop1, start2, stop2) == 
   ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2

这是相当微不足道的,实际上你把你的值变成了 [0.0, 1.0] 中的一个值,它告诉你离开始或停止还有多远(0.0 在 start1,1.0在 stop1) 然后你把它变成一个与另一个间隔成比例的值。

处理是开源的,所以像这样的问题可以通过查看 GitHub here.

上的源代码来回答。

具体来说,here是一个直接指向map()函数的link,看起来像这样:

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