我正在编程一个旋钮,它有一个箭头,其位置由弧数定义.
我正在寻找一种方法将这个弧数转换为一个代表温度的数字.
弧数的最小值为1.3,最大值为1.7.
1.3需要等于40和1.7需要等于99.
这可能吗?
如果它是线性的,您可以使用以下公式来允许任何最小值和最大值:
from_min = -1.3
from_max = 1.7
to_min = 40
to_max = 99
from = <whatever value you want to convert>
to = (from - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
Run Code Online (Sandbox Code Playgroud)
该* (to_max - to_min) / (from_max - from_min)位缩放范围从from范围到to范围.from_min之前减去并在添加to_min之后定位to范围内的正确点.
例子,首先是原件:
(1.3..1.7) -> (40..99)
to = (from - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
= (from - 1.3) * 59 / 0.4 + 40
= (from - 1.3) * 147.5 + 40 (same as Ignacio)
= from * 147.5 - 151.75 (same as Zebediah using expansion)
Run Code Online (Sandbox Code Playgroud)
然后使用-1.3作为你的一条评论中提到的下限:
(-1.3..1.7) -> (40..99)
to = (from - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
= (from - -1.3) * 59 / 3 + 40
= (from + 1.3) * 19.67 + 40
Run Code Online (Sandbox Code Playgroud)
这个答案(以及迄今为止的所有其他答案)都假定它是一个线性函数.根据你在问题中使用"arc"和"knob"这样的词语,这绝不是明确的.如果事实证明线性不够,你可能需要一些三角函数(正弦,余弦等).
| 归档时间: |
|
| 查看次数: |
349 次 |
| 最近记录: |