# pf-perlin

A Perlin noise generator for any number of dimensions.

## The Algorithm

First, a unit gradient vector is assigned to every lattice point. (The original algorithm accomplishes this using a permutation table). To get a random vector in , each coordinate of the vector is randomly selected from a normal distribution. Thus the probability of any point being selected is equal to

This probability is only dependent on the magnitude of . All that’s left is to normalize the vector. Another option is to uniformly sample each component of from until you get a nonzero vector whose magnitude is less than or equal to , but the probability of success approaches as the number of dimensions increases.

To find the value of a point, find the cell it lies in. For each corner of the cell, let the distance vector be the vector from the corner to the point. Compute the value of the corner by taking the dot product of the distance vector with the gradient vector at that corner. These are called influence values.

In the case of value noise, the influence value at each corner is a random value that is constant for all points inside the cell. Finally, to get the value for a point, interpolate between the influence values.

An interpolation function between two values and takes in a parameter and returns a value in . This can be formulated as

where is any function . Ken Perlin used the function which has both first and second derivative equal to at . With a -dimensional interpolation function chosen, an -dimensional interpolation function can be constructed by repeatedly interpolating along each dimension.

This completes the construction for one octave of Perlin noise. Next, the octave is scaled down by a factor of and added to itself repeatedly. The effect is visualized here with 1D Perlin noise:

The range of the first octave is as is shown here (although this can depend on the interpolation function used). Thus the radius of the final Perlin noise function with octaves is

This allows the noise function to be mapped to the interval or any desired interval.

Perlin noise still contains some directional artifacts—most notably at the origin. This can be remedied by rotating each octave by some angle such as the golden ratio for its high irrationality. For higher dimensions, this is addressed in simplex noise.