Algorithm to map an interval to a smaller interval

前端 未结 6 2050
生来不讨喜
生来不讨喜 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:48

    To map
    [A, B] --> [a, b]
    
    use this formula
    (val - A)*(b-a)/(B-A) + a
    

    as correctly mentioned in the other answer it's linear mapping.

    Basically

    y = m*x + c
    
    c = intersection at y-axis
    m = slope determined by two known point (A, a), (B, b) = (b-a)/(B-A)
    

提交回复
热议问题