Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

5 Answers

up vote 6 down vote accepted

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;

share|improve this answer
    
This answer made the most sense to my programmer brain. :) A "thank you" to everyone else for their great answers. –  Thraka Aug 2 '12 at 15:44
add comment

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

share|improve this answer
add comment

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))
share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
    
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. –  Lars Viklund Aug 4 '12 at 11:08
    
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. –  Ankush Aug 4 '12 at 13:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.