javascript - 在 Javascript 中重新创建 Processing map 函数

标签 javascript

<分区>

我正在尝试重新创建 map function在 Javascript 的处理中使用。 它永远不会崩溃,我只会得到错误的输出。

您将在下面找到我当前正在使用的功能。我完全被困在这里,所以任何帮助将不胜感激。

前两行专门用于防止任何数字超出函数范围。

function map(number, loworig, highorig, lowconversion, highconversion)
{
    if (number < loworig) number = loworig;
    else if (number > highorig) number = highorig;

    var num = highorig-loworig;
    var newnum = number-num;

    var ratio = newnum/num;

    var convnum = highconversion - lowconversion;
    var newconv = convnum*ratio;

    var result = newconv + convnum;

    return result;
}

编辑:我只是回顾了我的代码,我做了一些硬编码的数字转换,让它在它工作的时候工作。基本上,我的猜测与此功能无关,但我似乎无法弄清楚为什么它不起作用。

最佳答案

来自source code (对评论进行了轻微的重新格式化):

  /**
   * Re-maps a number from one range to another. In the example above,
   * the number '25' is converted from a value in the range 0..100 into
   * a value that ranges from the left edge (0) to the right edge (width)
   * of the screen.
   *
   * Numbers outside the range are not clamped to 0 and 1, because
   * out-of-range values are often intentional and useful.
   */
  static public final float map(float value,
                                float start1, float stop1,
                                float start2, float stop2) {
    return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
  }

这样转换成JavaScript:

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

关于javascript - 在 Javascript 中重新创建 Processing map 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20910091/

相关文章:

javascript - 如何在 V3 API 中访问 Google Map Maker map ?

javascript - 单击/选择时 Spotfire 按钮会改变颜色

javascript - 在 vuejs 中使用全局函数。 Lint (?)

javascript - 调用 JS 函数时从数据库填充下拉菜单的值?

JavaScript XML 解析

java - javascript 函数中的表单操作不起作用

javascript - 我的数据库文件有什么问题?

javascript - 如何在没有 DIV 的情况下隐藏 <body>?

从未使用文字表示法调用的 Javascript 对象构造函数?

javascript - 为什么我发布的延迟 Observable 工厂被多次调用?