将任意长度转换为-1.0到1.0之间的值? [英] Convert arbitrary length to a value between -1.0 a 1.0?
问题描述
如何将长度转换为-1.0到1.0范围内的值?
How can I convert a length into a value in the range -1.0 to 1.0?
示例:我的舞台长度为440px,并接受鼠标事件.我想在舞台中间单击,而不是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屋!