Skip to content

Instantly share code, notes, and snippets.

@daniel-woods
Created March 22, 2017 00:53
Show Gist options
  • Save daniel-woods/c64e66eb15b59cfa16ba41ab5f388e2c to your computer and use it in GitHub Desktop.
Save daniel-woods/c64e66eb15b59cfa16ba41ab5f388e2c to your computer and use it in GitHub Desktop.
import random
import math
def perlinNoise(hex_x, hex_y, seed):
random.seed(seed)
pers_min = 0.45
pers_max = 0.75
octaves = int(math.log(max(hex_x, hex_y), 2.0))
p = random.uniform(pers_min, pers_max)
perlin_arr = [[0.0 for i in range(hex_x)] for j in range(hex_y)] # image array
pixels = [[0.0 for i in range(hex_x)] for j in range(hex_y)] # image array
totAmp = 0.0
# Octave # k
# Octaves = threads
# threads may go out of sync, must start and end in order
threads = []
for k in range(octaves):
print(str(k) + "/" + str(octaves) + " octaves completed")
freq = 2 ** k
amp = p ** k
totAmp += amp
# create an image from n by m grid of random numbers (w/ amplitude) / Bilinear Interpolation
n = freq + 1
m = freq + 1 # grid size
ar = [[random.random() * amp for i in range(n)] for j in range(m)]
nx = hex_x / (n - 1.0)
ny = hex_y / (m - 1.0)
for ky in range(hex_y):
for kx in range(hex_x):
i = int(kx / nx)
j = int(ky / ny)
dx0 = kx - i * nx
dx1 = nx - dx0
dy0 = ky - j * ny
dy1 = ny - dy0
z = ar[j][i] * dx1 * dy1
z += ar[j][i + 1] * dx0 * dy1
z += ar[j + 1][i] * dx1 * dy0
z += ar[j + 1][i + 1] * dx0 * dy0
z /= nx * ny
perlin_arr[ky][kx] += z # add image layers together
for ky in range(hex_y):
for kx in range(hex_x):
c = int(perlin_arr[ky][kx] / totAmp * 255)
pixels[kx][ky] = c
return pixels
def octave():
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment