# One Dimensional Perlin noise vs Multi-Dimensional

I am reading through a book called "The Nature of Coding" and I am working on porting the examples from Processing.js to Regular old Java. The example I am on uses the following method call in processing...

``````noise(x)
``````

I noticed this class provided by Perlin himself, however, it is three dimensional. Can I just do something like this to make it one dimensional (seems too easy)...

``````static public double noise(double x) {
return noise(x, 0, 0);
}
``````

Or is there some other changes I have to make?

-
Yes, this is fine. –  Nathan Reed Jan 11 '13 at 17:42

No, you should avoid doing that. 3D Perlin noise is 7 or 8 times as expensive as 1D Perlin noise. You'd be better off reimplementing the 1D function, because it's very simple. If I'm not mistaken:

``````static public double noise(double x) {
int X = (int)Math.floor(x) & 255;
x -= Math.floor(x);
return lerp(u, grad(p[X  ], x  ),
}

static double grad(int hash, double x) {
return ((hash&1) == 0 ? x : -x);
}
``````
-
So I do notice some values in the previous version that look like they merge values, When I move to 2D do I have to create those merged values as well I am refering basically to...int A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z, // HASH COORDINATES // OF B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z; // THE 8 CUBE // CORNERS, –  Jackie Jan 12 '13 at 6:19
The purpose of these `p[...p[...]]` constructs is to shuffle `X`, `Y` and `Z` in order to generate a pseudo-random hash number. In 1D you only have `X` so `p[X]` and `p[X+1]` are enough to generate the hash. –  Sam Hocevar Jan 12 '13 at 13:54

Noise along any of the axes will be consistent noise. Just imagine you're flying through a cloud of noise sampling a single line of data.

So yes, `return noise(x, 0, 0);` is exactly like traveling along a single line of noise inside a cloud of noise. You'll be traveling along the line that represents the x axis. You could even do `return noise(x, x, x);` and travel diagonally through the noise if you wanted to. The point being, as long as you're moving linearly through the noise you'll get consistent noise with consistent values of `x`.

-
Can you expand this a little bit? For example the noise method in Processing does return a constant based on the x value passed to it, but the value does in fact change as x changes. Also you never really said if my implementation is correct with the 0s or not. –  Jackie Jan 11 '13 at 17:37
The word wasn't constant, it was consistent - what Byte56 is saying is that any 1-dimensional 'slice' of a proper 3-dimensional noise function will be equivalent to a 1-dimensional noise function. –  Steven Stadnicki Jan 11 '13 at 23:01
@StevenStadnicki Thanks, I was actually confused by that "constant" comment and your comment makes it clear now. –  Byte56 Jan 11 '13 at 23:03
(Also, I should note that 'noise along any of the axes will be consistent' is a goal, but not always a reality - IIRC there are some bad implementations of Perlin noise out there that show some very distinct axial inhomogeneity.) –  Steven Stadnicki Jan 11 '13 at 23:26
Note that `noise(x, x, x)` will return noise with a frequency `sqrt(3)` larger than expected. And even when accounting for that, you will not get the same noise as `noise(x, 0, 0)` because of Perlin noise’s inherent anisotropy. –  Sam Hocevar Jan 12 '13 at 2:16