Advertisement
here2share

# simplex_noise.py

May 5th, 2023
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # simplex_noise.py
  2.  
  3. def simplex_noise(x, y):
  4.     f2, g2 = (math.sqrt(3.0) - 1) / 2, (3 - math.sqrt(3.0)) / 6
  5.    
  6.     i, j = math.floor(x + (x + y) * f2), math.floor(y + (x + y) * f2)
  7.     ii, jj = i + math.floor((i + j) * g2), j + math.floor((i + j) * g2)
  8.    
  9.     X, Y, x, y = ii - (ii + jj) * f2, jj - (ii + jj) * f2, x - ii + (ii + jj) * g2, y - jj + (ii + jj) * g2
  10.     i1, j1 = (1, 0) if x > y else (0, 1)
  11.     x1, y1, x2, y2 = x - i1 + g2, y - j1 + g2, x - 1 + 2*g2, y - 1 + 2*g2
  12.    
  13.     h = lambda X, Y: math.floor(hash(X * 127 + Y) * (2147483648 - 1)) / (2147483648 - 1)
  14.     grad = lambda i, j: (h(X+i, Y+j) * 2 - 1, h(Y+i, X+j) * 2 - 1)
  15.     dot = lambda g, dx, dy: g[0] * dx + g[1] * dy
  16.     weight = lambda t: max(0, (1 - t**2)**4)
  17.    
  18.     n0, n1, n2 = dot(grad(0, 0), x, y), dot(grad(i1, j1), x1, y1), dot(grad(1, 1), x2, y2)
  19.    
  20.     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