Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # simplex_noise.py
- def simplex_noise(x, y):
- f2, g2 = (math.sqrt(3.0) - 1) / 2, (3 - math.sqrt(3.0)) / 6
- i, j = math.floor(x + (x + y) * f2), math.floor(y + (x + y) * f2)
- ii, jj = i + math.floor((i + j) * g2), j + math.floor((i + j) * g2)
- X, Y, x, y = ii - (ii + jj) * f2, jj - (ii + jj) * f2, x - ii + (ii + jj) * g2, y - jj + (ii + jj) * g2
- i1, j1 = (1, 0) if x > y else (0, 1)
- x1, y1, x2, y2 = x - i1 + g2, y - j1 + g2, x - 1 + 2*g2, y - 1 + 2*g2
- h = lambda X, Y: math.floor(hash(X * 127 + Y) * (2147483648 - 1)) / (2147483648 - 1)
- grad = lambda i, j: (h(X+i, Y+j) * 2 - 1, h(Y+i, X+j) * 2 - 1)
- dot = lambda g, dx, dy: g[0] * dx + g[1] * dy
- weight = lambda t: max(0, (1 - t**2)**4)
- n0, n1, n2 = dot(grad(0, 0), x, y), dot(grad(i1, j1), x1, y1), dot(grad(1, 1), x2, y2)
- return 50 * (weight(x**2 + y**2) * n0 + weight(x1**2 + y1**2) * n1 + weight(x2**2 + y2**2) * n2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement