I'm doing terrain generation and I have a perlin library that is giving me random numbers between -1 and +1. I want to convert this to the scale of 0-255. What is the proper way to do this?
6 Answers
Base formula is:
Result := ((Input - InputLow) / (InputHigh - InputLow))
* (OutputHigh - OutputLow) + OutputLow;
Your case:
Result := ((Input - -1) / (1 - -1) * (255 - 0) + 0;
From here you can optimize the conversion if your coefficients are static, but compiler will probably do it by itself as well.
Result := ((Input - -1) / 2) * 255 + 0;
Result := Input * 127.5 + 127.5;
-
4\$\begingroup\$ This answer made the most sense to my programmer brain. :) A "thank you" to everyone else for their great answers. \$\endgroup\$– ThrakaCommented Aug 2, 2012 at 15:44
-
1\$\begingroup\$ This is brilliant. Incredibly helpful and concise \$\endgroup\$ Commented Feb 16, 2021 at 16:23
-
\$\begingroup\$ Is there any specific name for this kind of normalization? It is brilliant but does this have a name? \$\endgroup\$ Commented Aug 11, 2021 at 0:54
The range -1
to 1
is a total range of 2
. So you need to scale it appropriately to get to 255
. Something like this:
(randomPerlin + 1.0) * 127.5
So you first add 1
, to get to 0..2
then multiply with 127.5
to get a range of 0..255
oldRange = oldMax - oldMin
newRange = newMax - newMin
newValue = ((oldValue - oldMin) * newRange / oldRange) + newMin
Hope this helps.
Through the power of scaling and bias.
From your value, subtract the minimum value of your range. That will give you a value in the range [0..2].
Divide that by the width of the source range, giving you a value in the range [0..1].
Multiply that by the width of the target range, giving you a value in the range [0..255].
Add the base of the target range to get a value in the target range, which for this case is the same as the previous step.
In summary:
(v - (-1.0))
v' = 0 + ---------------- * (255 - 0)
(1.0 - (-1.0))
Translate the input range so we get the min to zero by adding 1 (the negative value of the min input) -1 .. 1 -> 0 .. 2
As the output range starts with zero, do nothing for that.
Scale the new input range so it fits the output range, this is easy as they now both starts at zero: multiply the value by 255/2 0..2 * 2/255 -> 0..255
Done!
Example:
0.5 will go: (0.5 + 1) * 127.5 = 191.25 -0.5 will go: (-0.5 + 1) * 127.5 = 63.75
Lets say x
is original input and y
is expected output.
We have at least two points
(x,y)
(-1, 0)
(1, 255)
Since we want to do a linear interpolation we can look into the equation of form
y = m*x + c
With above two points, we can solve for m
and c
Once that is done, you get the transform equation.
Note this method works for other types of equations too. e.g If you want a exponential or quadratic interpolation.
-
\$\begingroup\$ In our field, the common meaning of linear interpolation tends to be that you mix linearly between two quantities with a blending factor like this:
v = (1-a)*x + (a)*y
. I don't see how this answer has much relevance to the question asked as all he wants is a range mapping. \$\endgroup\$ Commented Aug 4, 2012 at 11:08 -
\$\begingroup\$ the thing is two points in a plane uniquely define a line. we can treat range mapping as straight line. and we know two points on that line. solving it will give line equation. may be linear interpolation is not the right word here, but I wanted to mean set of inner points between those two points on this line. \$\endgroup\$– AnkushCommented Aug 4, 2012 at 13:23