SHARE
TWEET

Untitled

a guest Mar 19th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. N = 128
  2. iterations = 50
  3. noiseFct = .01
  4. noiseSeed(1)
  5.  
  6. Kr, Ks, Kc, Ke = .01, .01, .01, .5 # Constants
  7. w, m, wDiff, mDiff = [[[0 for y in xrange(N)] for x in xrange(N)] for n in xrange(4)] # Water and Material 2D arrays + corresponding differences arrays
  8. h = [[noise(x * noiseFct, y * noiseFct) for y in xrange(N)] for x in xrange(N)] # 2D (Perlin) noised terrain
  9.  
  10. mat = ((-1, -1), (0, -1), (1, -1),
  11.        (-1, 0),           (1, 0),
  12.        (-1, 1),  (0, 1),  (1, 1)) # Moore Neighborhood
  13.  
  14. for iter in xrange(iterations):
  15.  
  16.     for i in xrange(1, N-1):
  17.         for j in xrange(1, N-1):
  18.  
  19.             # Add precipitation
  20.             w[i][j] += Kr
  21.  
  22.             # Remove sediment from terrain
  23.             sediment = w[i][j] * Ks
  24.             h[i][j] -= sediment
  25.             m[i][j] += sediment
  26.  
  27.             # Compute differences with lower surrounding neighbors
  28.             a = h[i][j] + w[i][j] # Current altitude = current height + current amount of water
  29.             aTotal = 0
  30.             dTotal = 0
  31.             cells = 0
  32.             for dx, dy in mat:
  33.                x, y = (i + dx), (j + dy) # Coordinates of surrounding cells
  34.                 ai = h[x][y] + w[x][y] # altitude of ith neighbor
  35.                 d = a - ai # difference between current altitude and altitude of ith neighbor
  36.                 if d > 0.0: # if lower altitudes
  37.                     dTotal += d # cumulating differences for later average computation
  38.                     aTotal += ai # cumulating altitudes for later average computation
  39.                     cells += 1
  40.  
  41.  
  42.             if aTotal == 0: continue # Sometimes no cells with lower altitude are found
  43.  
  44.             # Water and Sediment distribution
  45.             avgAlt = aTotal / cells # average of total heights
  46.             deltaA = a - avgAlt
  47.             waterAmount = min(w[i][j], deltaA)
  48.  
  49.  
  50.             ### STEP 3 ###            
  51.             for dx, dy in mat:
  52.                 x, y = (i + dx), (j + dy) # Coordinates of surrounding cells
  53.                 ai = h[x][y] + w[x][y] # altitude of ith neighbor
  54.                 di = a - ai # difference between current altitude and altitude of ith neighbor
  55.  
  56.                 if di > 0: # if lower altitudes
  57.  
  58.                     deltaW = waterAmount * (di / dTotal) # water amount
  59.  
  60.                     wDiff[i][j] += deltaW #storing water amount to be removed from central cell
  61.                     w[x][y] += deltaW #water distribution to selected surrounding cells
  62.  
  63.                     deltaM = m[i][j] * (deltaW / w[i][j]) # material amount
  64.  
  65.                     mDiff[i][j] += deltaM #storing material amount to be removed from central cell
  66.                     m[x][y] += deltaM #material distribution to selected surrounding cells
  67.  
  68.                     w[i][j] -= wDiff[i][j] #removing amount of water that has been distributed from central cell
  69.                     m[i][j] -= mDiff[i][j] #removing amount of material that has been distributed from central cell
  70.  
  71.  
  72.             #Evaporation
  73.             mMax = Kc * w[i][j]
  74.             deltaM = max(0, m[i][j] - mMax)
  75.             m[i][j] -= deltaM
  76.             h[i][j] += deltaM
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top