将任意长度转换为 -1.0 到 1.0 之间的值? [英] Convert arbitrary length to a value between -1.0 a 1.0?

查看:29
本文介绍了将任意长度转换为 -1.0 到 1.0 之间的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将长度转换为 -1.0 到 1.0 范围内的值?

How can I convert a length into a value in the range -1.0 to 1.0?

示例:我的舞台长度为 440 像素,并接受鼠标事件.我想在舞台中间单击,而不是 X = 220 的输出,我希望它是 X = 0.同样,我希望真正的 X = 0 变成 X = -1.0 而真正的 X = 440 变成 X= 1.0.

Example: my stage is 440px in length and accepts mouse events. I would like to click in the middle of the stage, and rather than an output of X = 220, I'd like it to be X = 0. Similarly, I'd like the real X = 0 to become X = -1.0 and the real X = 440 to become X = 1.0.

我无法访问舞台,所以我不能简单地在中心注册它,这会使这个过程更容易.此外,动态更改舞台的实际大小是不可能的,所以我正在寻找一个公式,它将鼠标在舞台的真实 X 坐标转换为均匀地适应从 -1 到 1 的范围内.

I don't have access to the stage, so i can't simply center-register it, which would make this process a lot easier. Also, it's not possible to dynamically change the actual size of my stage, so I'm looking for a formula that will translate the mouse's real X coordinate of the stage to evenly fit within a range from -1 to 1.

推荐答案

-1 + (2/440)*x

其中x是距离

所以,为了概括它,如果最小归一化值是 a 并且最大归一化值是 b (在你的例子中 a = -1.0, b= 1.0 并且最大可能值为 k (在您的示例中为 k = 440):

So, to generalize it, if the minimum normalized value is a and the maximum normalized value is b (in your example a = -1.0, b = 1.0 and the maximum possible value is k (in your example k = 440):

a + x*(b-a)/k

其中 x>= 0<= k

这篇关于将任意长度转换为 -1.0 到 1.0 之间的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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