Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def gen_noise(x,y):
- matrix = np.empty([x,y])
- for i in range(x):
- for j in range(y):
- matrix[i,j] = random.random()
- return matrix
- def Interpolate(x0,x1,alpha):
- return x0 * (1 - alpha) + alpha * x1;
- def GenerateSmoothNoise(noise, octave):
- width,height = noise.shape
- samplePeriod = 1<<octave
- sampleFreq = 1.0/samplePeriod
- smoothnoise = np.empty([10,10])
- for i in range(width):
- sample_i0 = (i / samplePeriod) * samplePeriod
- sample_i1 = (sample_i0 + samplePeriod) % width
- horizontal_blend = (i - sample_i0) * sampleFreq
- for j in range(height):
- sample_j0 = (j / samplePeriod) * samplePeriod;
- sample_j1 = (sample_j0 + samplePeriod) % height;
- vertical_blend = (j - sample_j0) * sampleFreq;
- top = Interpolate(noise[sample_i0][sample_j0], noise[sample_i1][sample_j0], horizontal_blend)
- bottom = Interpolate(noise[sample_i0][sample_j1],noise[sample_i1][sample_j1], horizontal_blend)
- smoothnoise[i,j] = Interpolate(top, bottom, vertical_blend)
- return smoothnoise
- def GeneratePerlinNoise(baseNoise, octaveCount,threshold):
- width,height = baseNoise.shape
- smoothNoise = []
- persistance = 0.5;
- for i in range(octaveCount):
- smoothNoise.append(GenerateSmoothNoise(baseNoise, i))
- perlinNoise = np.empty([width,height])
- amplitude = 1.0
- totalAmplitude = 0.0
- for octave in range(octaveCount):
- amplitude *= persistance
- totalAmplitude += amplitude
- for i in range(width):
- for j in range(height):
- perlinNoise[i,j] += smoothNoise[octave][i,j] * amplitude
- flatp = perlinNoise.flatten()
- flatp.sort()
- for i in range(width):
- for j in range(height):
- if perlinNoise[i,j] >= flatp[-threshold]:
- perlinNoise[i,j]=1
- else: perlinNoise[i,j]=0
- return perlinNoise;
- def genPath(width,height):
- kfs = []
- for i in range(width):
- kfs.append(int(0.9*random.random()*10))
- kfs.append(kfs[-1])
- matrix = []
- for i in range(width):
- row = np.array([1]*height)
- s = min(kfs[i],kfs[i+1])
- e = max(kfs[i],kfs[i+1])+1
- row[s:e] = 0
- matrix.append(row)
- matrix = np.flipud(np.array(matrix))
- return matrix
- fig = plt.figure()
- import copy
- for k in range(9):
- perlin = GeneratePerlinNoise(gen_noise(10,10),2,random.choice(range(15,25)))
- path = genPath(10,10)
- w,h = perlin.shape
- for i in range(w):
- for j in range(h):
- if path[i,j] == 0: perlin[i,j] = 0
- fig.add_subplot(3,3,k+1)
- plt.imshow(copy.deepcopy(perlin), cmap='Greys', interpolation='nearest')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement