I'm trying to generate 2D Perlin noise using pnoise2()
from Python's noise module. I have tried to follow examples.
The function merely requires an x
and y
input:
noise2(x, y, octaves=1, persistence=0.5, repeatx=1024, repeaty=1024, base=0.0)
Yet no matter what I pass in, the function always returns 0.0
. How come? Did I miss something?
[edit: 05/10/12]
Some other example works, the code is simple:
import sys
from noise import pnoise2
import random
random.seed()
octaves = random.random()
freq = 16.0 * octaves
for y in range(30):
for x in range(40):
n = int(pnoise2(x/freq, y / freq, 1)*10+3)
if n>=1:
n=1
else:
n=0
print n,
print
So, since I don't know what the * 0.5
or + 0.5
or * 10+3
etc. values are for, I tried myself, whilst excluding some of these values. In the example above random.random()
returns a float e.g. 0.7806
etc. which is then multiplied by 16.0
to get the frequency. NB: That already puzzles me because octaves, I thought ought to be an integer since it tells the number of successive noise functions (note also it is an integer 1
that is passed in as 3rd value in the pnoise2()
function). Anyways, in the function, some integers from the range()
are then divided by the frequency
, now about 13.88
:
>>> pnoise2(1/13.88, 1/13.88, 1)
0.06868855153353493
>>> pnoise2(0.07, 0.06, 1)
0.06691436186932441
>>> pnoise2(3.07, 3.04, 1)
0.06642476158741428
>>> pnoise2(3.00, 2.03, 1)
0.02999223154495647
But then again:
>>> pnoise2(3.0, 2.0, 1)
0.0
>>> pnoise2(1.0, 2.0, 1)
0.0
>>> pnoise2(4.0, 5.0, 1)
0.0
So I conclude that x
or y
must have decimals in order for the function to return a value other than 0.0
.
Now after this, my problem is I don't understand why. Can someone enlighten me please?