IMPLEMENTATION OF NOISE
From [Watt & Watt, p.208]
#include <math.h>
#define SIZE 575
/* noise is randomly assigned values in [0,1] */
extern float noise[SIZE][SIZE];
extern float scale, pixel_size;
/* Perlin's turbulence function */
float turbulence(float u, float v)
{
float t = 0.;
float linear_noise( );
while (scale > pixel_size) {
t += abs(linear_noise(u/scale, v/scale) * scale);
scale /= 2.;
}
return( t );
}
/* linear_noise interpolates the values of noise[][] to
non-integer coordinates */
float linear_noise( float u, float v)
{
int iu, iv, ip, iq;
float du, dv, bot, top;
iu = u;
iv = v;
du = u - iu;
dv = v - iv;
iu = iu % SIZE ;
iv = iv % SIZE ;
ip = (iu+1) % SIZE ;
iq = (iv+1) % SIZE ;
bot = noise[iu][iv] + du * (noise[ip][iv] - noise[iu][iv]) ;
top = noise[iu][iq] + du * (noise[ip][iq] - noise[iu][iq]) ;
return( bot + dv * (top - bot) );
}