Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. def gen_noise(x,y):
  2. matrix = np.empty([x,y])
  3. for i in range(x):
  4. for j in range(y):
  5. matrix[i,j] = random.random()
  6. return matrix
  7.  
  8. def Interpolate(x0,x1,alpha):
  9. return x0 * (1 - alpha) + alpha * x1;
  10.  
  11. def GenerateSmoothNoise(noise, octave):
  12. width,height = noise.shape
  13.  
  14. samplePeriod = 1<<octave
  15. sampleFreq = 1.0/samplePeriod
  16.  
  17. smoothnoise = np.empty([10,10])
  18.  
  19. for i in range(width):
  20.  
  21. sample_i0 = (i / samplePeriod) * samplePeriod
  22. sample_i1 = (sample_i0 + samplePeriod) % width
  23. horizontal_blend = (i - sample_i0) * sampleFreq
  24. for j in range(height):
  25. sample_j0 = (j / samplePeriod) * samplePeriod;
  26. sample_j1 = (sample_j0 + samplePeriod) % height;
  27. vertical_blend = (j - sample_j0) * sampleFreq;
  28. top = Interpolate(noise[sample_i0][sample_j0], noise[sample_i1][sample_j0], horizontal_blend)
  29. bottom = Interpolate(noise[sample_i0][sample_j1],noise[sample_i1][sample_j1], horizontal_blend)
  30. smoothnoise[i,j] = Interpolate(top, bottom, vertical_blend)
  31.  
  32. return smoothnoise
  33.  
  34. def GeneratePerlinNoise(baseNoise, octaveCount,threshold):
  35. width,height = baseNoise.shape
  36.  
  37. smoothNoise = []
  38.  
  39. persistance = 0.5;
  40.  
  41. for i in range(octaveCount):
  42. smoothNoise.append(GenerateSmoothNoise(baseNoise, i))
  43.  
  44. perlinNoise = np.empty([width,height])
  45. amplitude = 1.0
  46. totalAmplitude = 0.0
  47.  
  48. for octave in range(octaveCount):
  49. amplitude *= persistance
  50. totalAmplitude += amplitude
  51.  
  52. for i in range(width):
  53. for j in range(height):
  54. perlinNoise[i,j] += smoothNoise[octave][i,j] * amplitude
  55. flatp = perlinNoise.flatten()
  56. flatp.sort()
  57. for i in range(width):
  58. for j in range(height):
  59.  
  60. if perlinNoise[i,j] >= flatp[-threshold]:
  61.  
  62. perlinNoise[i,j]=1
  63. else: perlinNoise[i,j]=0
  64.  
  65. return perlinNoise;
  66.  
  67.  
  68.  
  69. def genPath(width,height):
  70. kfs = []
  71. for i in range(width):
  72. kfs.append(int(0.9*random.random()*10))
  73. kfs.append(kfs[-1])
  74. matrix = []
  75. for i in range(width):
  76. row = np.array([1]*height)
  77. s = min(kfs[i],kfs[i+1])
  78. e = max(kfs[i],kfs[i+1])+1
  79. row[s:e] = 0
  80. matrix.append(row)
  81.  
  82.  
  83. matrix = np.flipud(np.array(matrix))
  84. return matrix
  85.  
  86. fig = plt.figure()
  87. import copy
  88. for k in range(9):
  89. perlin = GeneratePerlinNoise(gen_noise(10,10),2,random.choice(range(15,25)))
  90. path = genPath(10,10)
  91.  
  92. w,h = perlin.shape
  93. for i in range(w):
  94. for j in range(h):
  95. if path[i,j] == 0: perlin[i,j] = 0
  96. fig.add_subplot(3,3,k+1)
  97. plt.imshow(copy.deepcopy(perlin), cmap='Greys', interpolation='nearest')
  98.  
  99.  
  100.  
  101. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement