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.

Related to my previous question (2D Terra(ria)in generation - Accidental noise) I'm stuck on understanding how the gradient function itself works (in the Accidental library).

After help on chat (thanks Jon!) I understand that upon defining the gradient, for example, Gradient(0,0,0,1), it returns a function that upon its turn would return correct values between 0 & 1 for the arguments you provide.

So calling the this function with (0,40) would give you a value back between (0,1). However, I don't understand how it would do this without knowing the outer "range".

E.g. If you'd render the gradient image that's shown on his site: Accidental's Noise Gradient image

Would you do that by defining the gradient(0,0,0,1) and then calling it for each pixel? (0,1) (0,2), (50,50),...? In that case, pixel (0,200) would have to return 1, but if you'd have an image of 400 pixels high, (0,200) would have to return 0.5

And calling it with values between (0,0) & (0,1) with predefined steps (1/200 or 1/400 for example), seems to make the gradient noise function itself rather useless.

If possible, pseudo or python code would be very welcome to illustrate how the "internals" on that gradient function would work, perhaps I'm thinking of it too complex.

Thanks in advance!

share|improve this question
1  
Sorry I missed your chat, I made an answer for you. Hope that clears things up? –  Byte56 Jun 19 '13 at 14:50

2 Answers 2

up vote 5 down vote accepted

The values specified when you create your gradient, are your endpoints. So for example, when you create a gradient with the values (x1=0, x2=0, y1=0, y2=1), you are creating a gradient line on the y-axis with its endpoints being 0 and 1, on the y-axis.

Okay, so you have a gradient on your y-axis, starting at point 0, all the way to point 1. What does this mean?

If it lies on or beyond P1, the value is assigned as 0. If it lies on or beyond P2 the value is assigned as 1. Anything in between is assigned as a linear interpolation between 0 and 1.

This doesn't mean your image is 1 pixel high, it just means your gradient is 1 unit high in your function.

When you retrieve values from this function, you can scale according to your image.

For example:

for (int i = 0; i < imageWidth; i++)
{
    for (int j = 0; j < imageHeight; j++)
    {
        double x = i / imageWidth;
        double y = j / imageHeight;
        double val = gradient.Get(x,y);
    }
}
share|improve this answer
    
I guess we were writing our answers at the same time. Looks like we're thinking the same thing! –  Byte56 Jun 19 '13 at 17:39
    
@Byte56 Yeah. Upvote for you my good man. –  Jon Jun 19 '13 at 18:33

Everything you pass into the function should be scaled by the size of your world. Since the gradient lies between 0 and 1 of the function, you essentially just normalize everything based on your maximum world height. To illustrate, here's a little function that would do something like that:

float gadientAt(worldX, worldY)
   return gradientFunction(worldX/WORLD_SIZE_X, worldY/WORLD_SIZE_Y)

While you don't really have to scale the x axis (since the gradient is along the y axis), we're just ensuring the function works for all types of gradients.

If you were to pass in values outside of the 0 to 1 range, you'd just get 0 or 1 for return values.

You can always create the gradient function with the size of your world Gradient(0,0,worldMinY,WorldMaxY), but people usually like to think in normalized values.

share|improve this answer
    
wouldn't a simple 0,0 & 0,1 gradient function (the function itself), just return the same normalized values? (assuming it goes along the Y axis?) –  Busata Jun 19 '13 at 16:48
    
You have to have a maximum value to normalize. –  Byte56 Jun 19 '13 at 17:35
    
Thanks! Accepted Jon's answer for his help on chat as well & your answers are the same :-) Perhaps I'll switch it every week or so ;) –  Busata Jun 19 '13 at 18:40
    
@Busata Ha, no worries Busata. His answer is certainly worth accepting. –  Byte56 Jun 19 '13 at 19:06

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.