Advertisement
here2share

# Tk_2D_Perlin_Noise_Plus.py

Apr 11th, 2021
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # Tk_2D_Perlin_Noise_Plus.py # zzz too slow
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. import random
  6. import math
  7.  
  8. ww = 640
  9. hh = 640
  10.  
  11. def rgb2hex(rgb):
  12.     r,g,b = rgb
  13.     return "#%02x%02x%02x" % (r,g,b)
  14.  
  15. RGBs = []
  16. def z():
  17.     RGBs.append((r,g,b))
  18. r,g,b = 255,0,0
  19. for g in range(256):
  20.     z()
  21. for r in range(254, -1, -1):
  22.     z()
  23. for b in range(256):
  24.     z()
  25. for g in range(254, -1, -1):
  26.     z()
  27. for r in range(256):
  28.     z()
  29. for b in range(254, -1, -1):
  30.     z()
  31. 0
  32. Lc = len(RGBs)
  33.  
  34. root = Tk()
  35. root.title("Tk_2D_Perlin_Noise_Plus")
  36. root.geometry("%dx%d+0+0"%(ww,hh))
  37. canvas = Canvas(root, width=ww, height=hh)
  38. canvas.grid()
  39.  
  40. dirs = [(math.cos(a * 2.0 * math.pi / 256),
  41.          math.sin(a * 2.0 * math.pi / 256))
  42.          for a in range(256)]
  43.  
  44. def surflet(gridX, gridY, x, y, hashfunc):
  45.     distX, distY = abs(x-gridX), abs(y-gridY)
  46.     polyX = 1 - 6*distX**5 + 15*distX**4 - 10*distX**3
  47.     polyY = 1 - 6*distY**5 + 15*distY**4 - 10*distY**3
  48.     hashed = hashfunc(int(gridX), int(gridY))
  49.     grad = (x-gridX)*dirs[hashed%len(dirs)][0] + (y-gridY)*dirs[hashed%len(dirs)][1]
  50.     return polyX * polyY * grad
  51.  
  52. def noise(x, y, hashfunc):
  53.     intX, intY = int(math.floor(x)), int(math.floor(y))
  54.     s1 = surflet(intX+0, intY+0, x, y, hashfunc)
  55.     s2 = surflet(intX+1, intY+0, x, y, hashfunc)
  56.     s3 = surflet(intX+0, intY+1, x, y, hashfunc)
  57.     s4 = surflet(intX+1, intY+1, x, y, hashfunc)
  58.     return (s1 + s2 + s3 + s4)
  59.  
  60. def fBm(x, y, octs, hashfunc):
  61.     val = 0
  62.     for o in range(octs):
  63.         scale = 2**o
  64.         val += 0.5**o * noise(x*scale, y*scale, hashfunc)
  65.     return val
  66.  
  67. class PermHash(object):
  68.     def __init__(self, perm=None):
  69.         if perm is None:
  70.             self._perm = list(range(256))
  71.             random.shuffle(self._perm)
  72.             self._perm += self._perm       
  73.         else:
  74.             self._perm = perm
  75.  
  76.     def __call__(self, *args):
  77.         return self._perm[(self._perm[int(args[0])%len(self._perm)] + int(args[1]))%len(self._perm)]
  78.  
  79.     def GetSaveState(self):
  80.         return self._perm
  81.  
  82. size, freq, octs, data = 200, 1/32.0, 2, []
  83. hashfunc = PermHash()
  84.  
  85. img = Image.new("RGB",(ww, hh))
  86.  
  87. for y in range(hh):
  88.     for x in range(ww):
  89.         tx = x - 100
  90.         ty = y - 100
  91.         tt = fBm(tx*freq, ty*freq, octs, hashfunc)
  92.         tt = int((tt+1)/2*Lc)
  93.         data.append(RGBs[tt])
  94.  
  95. if 1:
  96.     img.putdata(data)
  97.     imgTk = ImageTk.PhotoImage(img)
  98.     canvas.create_image(0, 0, anchor=NW, image=imgTk)
  99.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement