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:
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!
Answer
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);
}
}
No comments:
Post a Comment