将间隔映射到较小间隔的算法 [英] Algorithm to map an interval to a smaller interval

查看:68
本文介绍了将间隔映射到较小间隔的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜索,但是由于问题的性质,我无法找到满意的东西.

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

我的问题如下:我试图将范围从0到2000的数字映射(虽然理想情况下上限是可调的)到较小的间隔(范围从10到100).上限将映射(2000-> 100)以及下限.除此之外,在[0; 2000]间隔内大于另一个条目的条目将比[0; 100]中的映射条目更大.

My problem is the following: I am trying to map numbers ranging from 0 to 2000 (though ideally the upper limit would be adjustable) to the much smaller interval ranging from 10 to 100. The upper limits would map (2000->100) and the lower limits as well. Other than that, an entry that is bigger than another entry in the interval [0;2000] would ideally be bigger than that mapped entry in [0;100]

我想这个问题不是特定于语言的,但是如果您想知道,我今天正在使用Javascript.

I'm thinking that this question is not language specific, but in case you are wondering, I'm working with Javascript today.

推荐答案

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.

基本上

y = m*x + c

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

这篇关于将间隔映射到较小间隔的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆