Algorithm to map an interval to a smaller interval

前端 未结 6 2059
生来不讨喜
生来不讨喜 2021-02-02 11:50

I tried searching, but due to the nature of my question, I was unable to find something satisfactory.

My problem is the following: I am trying to map numbers ranging fro

6条回答
  •  走了就别回头了
    2021-02-02 12:47

    Here could be an optimized way to map your x data, This pseudo code shows you the main idea for a map function that:

    • Avoid problems with x values out of b1 - b2's range.
    • Deals with array mapping

      function map(var x, var b1, var b2, var s1, var s2)
      {
          var i;
          var result;
      
          i = 0;
          while(i < sizeof(s2))
              if(x < b1)
                  result[i++] = s1;
              else if (x > b2)
                  result[i++] = s2;
              else
                  result[i] = (x - b1) / (b2 - b1 ) * (s2[i] - s1[i]) + s1[i++];
          return (result);
      }
      

提交回复
热议问题