C++ 从 [0.5 - 1] 归一化到 [0 - 1]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1471370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:06:20  来源:igfitidea点击:

Normalizing from [0.5 - 1] to [0 - 1]

c++mathhlslnormalize

提问by tweetypi

I'm kind of stuck here, I guess it's a bit of a brain teaser. If I have numbers in the range between 0.5 to 1 how can I normalize it to be between 0 to 1?

我有点卡在这里,我想这有点脑筋急转弯。如果我有介于 0.5 到 1 之间的数字,我如何将其标准化为介于 0 到 1 之间?

Thanks for any help, maybe I'm just a bit slow since I've been working for the past 24 hours straight O_O

感谢您的帮助,也许我只是有点慢,因为我过去 24 小时一直在工作 O_O

回答by jason

Others have provided you the formula, but not the work. Here's how you approach a problem like this. You might find this far more valuable than just knowning the answer.

其他人为您提供了公式,但没有提供工作。以下是您处理此类问题的方法。您可能会发现这比仅仅知道答案更有价值。

To map [0.5, 1]to [0, 1]we will seek a linear map of the form x -> ax + b. We will require that endpoints are mapped to endpoints and that order is preserved.

为了映射[0.5, 1]到,[0, 1]我们将寻找形式为 的线性映射x -> ax + b。我们将要求端点映射到端点并保留该顺序。

Method one:The requirement that endpoints are mapped to endpoints and that order is preserved implies that 0.5is mapped to 0and 1is mapped to 1

方法一:端点映射到端点并保留顺序的要求意味着0.5映射到01被映射到1

a * (0.5) + b = 0 (1)
a * 1 + b = 1     (2)

This is a simultaneous system of linear equations and can be solved by multiplying equation (1)by -2and adding equation (1)to equation (2). Upon doing this we obtain b = -1and substituting this back into equation (2)we obtain that a = 2. Thus the map x -> 2x - 1will do the trick.

这是线性方程的同时系统,并且可以通过乘以等式来解决(1)-2和添加方程(1)以方程(2)。这样做后,我们得到b = -1并将其代入方程,(2)我们得到a = 2。因此,地图x -> 2x - 1将发挥作用。

Method two:The slope of a line passing through two points (x1, y1)and (x2, y2)is

方法二:一个线通过的通过两个点的斜率(x1, y1)(x2, y2)

(y2 - y1) / (x2 - x1).

Here we will use the points (0.5, 0)and (1, 1)to meet the requirement that endpoints are mapped to endpoints and that the map is order-preserving. Therefore the slope is

在这里,我们将使用点(0.5, 0)(1, 1)满足端点映射到端点并且映射是顺序保留的要求。因此斜率为

m = (1 - 0) / (1 - 0.5) = 1 / 0.5 = 2.

We have that (1, 1)is a point on the line and therefore by the point-slope form of an equation of a line we have that

我们有那(1, 1)是线上的一个点,因此通过一条线的方程的点斜形式我们有

y - 1 = 2 * (x - 1) = 2x - 2

so that

以便

y = 2x - 1.

Once again we see that x -> 2x - 1is a map that will do the trick.

我们再一次看到这x -> 2x - 1是一张可以解决问题的地图。

回答by Bill the Lizard

Subtract 0.5 (giving you a new range of 0 - 0.5) then multiply by 2.

减去 0.5(给你一个新的范围 0 - 0.5)然后乘以 2。

double normalize( double x )
{
    // I'll leave range validation up to you
    return (x - 0.5) * 2;
}

回答by Toon Krijthe

To add another generic answer.

添加另一个通用答案。

If you want to map the linear range [A..B] to [C..D], you can apply the following steps:

如果要将线性范围 [A..B] 映射到 [C..D],可以应用以下步骤:

Shift the range so the lower bound is 0. (subract A from both bounds:

移动范围,使下限为 0。(从两个边界中减去 A:

[A..B] -> [0..B-A]

Scale the range so it is [0..1]. (divide by the upper bound):

缩放范围使其为 [0..1]。(除以上限):

[0..B-A] -> [0..1]

Scale the range so it has the length of the new range which is D-C. (multiply with D-C):

缩放范围,使其具有新范围的长度,即 DC。(乘以 DC):

[0..1] ->  [0..D-C]

Shift the range so the lower bound is C. (add C to the bounds):

移动范围,使下限为 C。(将 C 添加到边界):

[0..D-C] -> [C..D]

Combining this to a single formula, we get:

将其组合到一个公式中,我们得到:

       (D-C)*(X-A)
X' =   -----------  + C
          (B-A)

In your case, A=0.5, B=1, C=0, D=1 you get:

在你的情况下,A=0.5, B=1, C=0, D=1 你得到:

       (X-0.5)
X' =   ------- = 2X-1
        (0.5)

Note, if you have to convert a lot of X to X', you can change the formula to:

请注意,如果您必须将很多 X 转换为 X',则可以将公式更改为:

       (D-C)         C*B - A*D
X' =   ----- * X  +  ---------  
       (B-A)           (B-A)

It is also interesting to take a look at non linear ranges. You can take the same steps, but you need an extra step to transform the linear range to a nonlinear range.

看看非线性范围也很有趣。您可以采取相同的步骤,但您需要一个额外的步骤来将线性范围转换为非线性范围。

回答by Glenner003

× 2 ? 1

× 2 ? 1

should do the trick

应该做的伎俩

回答by meetar

Lazyweb answer:To convert a value xfrom [minimum..maximum]to [floor..ceil]:

Lazyweb 答案:要将值x从转换[minimum..maximum][floor..ceil]

General case:

一般情况:

normalized_x = ((ceil - floor) * (x - minimum))/(maximum - minimum) + floor

To normalize to [0..255]:

归一化为 [0..255]:

normalized_x = (255 * (x - minimum))/(maximum - minimum)

To normalize to [0..1]:

归一化为 [0..1]:

normalized_x = (x - minimum)/(maximum - minimum)

回答by Whitney Imura

You could always use clamp or saturate within your math to make sure your final value is between 0-1. Some saturate at the end, but I've seen it done during a computation, too.

你总是可以在你的数学中使用clamp或saturate来确保你的最终值在0-1之间。最后有些饱和,但我也看到它在计算过程中完成。