价值重新映射
处理有一个很棒的功能,我一直使用:
map(value, low1, high1, low2, high2)
http://processing.org/reference/map_.html< /a>
它将 value
(预期范围为 low1
到 high1
)重新映射到 low2
目标范围> 到high2
)。
我想了解它背后的数学原理,这样我就可以在其他语言中使用它。 有人想给我一点建议并帮我进行逆向工程吗?我知道这是一个被重新调整和重新抵消的麻疯病……今天早上感觉脑死亡。
Processing has a great function I use all the time:
map(value, low1, high1, low2, high2)
http://processing.org/reference/map_.html
It remaps value
(that has an expected range of low1
to high1
) into a target range of low2
to high2
).
I want to understand the math behind it so I can use it in other languages.
Anyone want to throw me a bone and help me reverse engineer it? I understand that it's a lerp that's been re-scaled and re-offset... feeling brain dead this morning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从你的描述来看,应该是这样的吧?
找出您进入第一个范围的距离,按范围大小的比率缩放该距离,这就是您应该进入第二个范围的距离。
From your description, it ought to be doing this, right?
Find how far you are into the first range, scale that distance by the ratio of sizes of the ranges, and that's how far you should be into the second range.
处理是开源的。您可以查看
map()
函数此处。具体来说,您正在寻找这行代码:
Processing is open-source. You can view the
map()
function here.Specifically, you're looking for this line of code:
我想补充一点,有时查找 low1 和 high1 之间的因子很有用,以便您可以在将该因子用作 LERP 的 t 之前用曲线对其进行调制。
因此,t = (value-low1)/(high1-low1) 即可获得 value 在 low1 到 high1 行中的相对位置。
然后,您可以使用一些曲线滤波器(例如伽马、偏置、增益等)来调制 t。如果您要限制超出设定低点和高点的值,也可以将 t 限制在 0 和 1 之间。
然后使用 t 表示 low2 和 high2 之间的 LERP,例如:finalvalue = low2*(1-t) + high2*t
I would like to add that is sometimes useful to find the factor between the low1 and high1 so that you can modulate it with a curve before using the factor as a LERP's t.
So, t = (value-low1)/(high1-low1) to get the relative position of value in the line low1 to high1.
Then you can modulate t with some curve filter for example, gamma, bias, gain, etc As also clamp the t between 0 and 1 if you to restrict values that go over the set lows and highs.
And then use the t for the LERP between low2 and high2 like: finalvalue = low2*(1-t) + high2*t
任何人想知道是否有一个无浮点版本可以保持尽可能高的精度,我做了这个:
似乎对我有用,但没有经过广泛测试(例如 max 小于 min 未测试)。
背后的想法是通过使用更大的类型来放大原始值,以便除法产生更大的数字 - 从而获得更高的精度。
Anyone wondering if there's a float-less version that keeps as much precision as possible, I made this:
Seems to work for me, not extensively tested (e.g. max is smaller than min is not tested).
The idea behind is it to enlarge the original value by using a bigger type so that divisions yield bigger numbers - which results in more precision.
就我而言,我使用了表达式 (var * amount)-shift
其中 var 是值
amount 是混合形状的数量
并且shift是基于blendshape索引的值(如果值为1则移位1并且你得到0,所以下一个blendshape开始)
In my case I used an expression (var * amount)-shift
where var is value
amount is amount of blendshapes
and shift is value based on the index of the blendshape (shift by 1 and you get 0 if the value is 1 so the next blendshape starts of)