如何获得与角度对应的值

How to get values with respact to angle

本文关键字:何获得      更新时间:2024-03-26

角度0到180度我想要介于它们之间的值,比如:

Angle 0deg then value = -10
Angle 90deg then value = 0
Angle 180deg then value = 10

数值在-10到10之间变化。

function getValue(angle) {
    return Math.round(angle / 180 * 20 - 10);
}

这是一个简单的线性插值。你要从[0:180]到[-10:10]。以下是步骤:

  • 将角度除以180,使其在[0:1]范围内(规格化)
  • 乘以20,这是你的目标范围的宽度。你得到[0:20]
  • 减去10,将您的范围移动10,并使其居中于0。你得到[-10:10]

如果需要十进制值,可以去掉Math.round