Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_2D_Perlin_Noise_Plus.py # zzz too slow
- from Tkinter import *
- from PIL import Image, ImageTk
- import random
- import math
- ww = 640
- hh = 640
- def rgb2hex(rgb):
- r,g,b = rgb
- return "#%02x%02x%02x" % (r,g,b)
- RGBs = []
- def z():
- RGBs.append((r,g,b))
- r,g,b = 255,0,0
- for g in range(256):
- z()
- for r in range(254, -1, -1):
- z()
- for b in range(256):
- z()
- for g in range(254, -1, -1):
- z()
- for r in range(256):
- z()
- for b in range(254, -1, -1):
- z()
- 0
- Lc = len(RGBs)
- root = Tk()
- root.title("Tk_2D_Perlin_Noise_Plus")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.grid()
- dirs = [(math.cos(a * 2.0 * math.pi / 256),
- math.sin(a * 2.0 * math.pi / 256))
- for a in range(256)]
- def surflet(gridX, gridY, x, y, hashfunc):
- distX, distY = abs(x-gridX), abs(y-gridY)
- polyX = 1 - 6*distX**5 + 15*distX**4 - 10*distX**3
- polyY = 1 - 6*distY**5 + 15*distY**4 - 10*distY**3
- hashed = hashfunc(int(gridX), int(gridY))
- grad = (x-gridX)*dirs[hashed%len(dirs)][0] + (y-gridY)*dirs[hashed%len(dirs)][1]
- return polyX * polyY * grad
- def noise(x, y, hashfunc):
- intX, intY = int(math.floor(x)), int(math.floor(y))
- s1 = surflet(intX+0, intY+0, x, y, hashfunc)
- s2 = surflet(intX+1, intY+0, x, y, hashfunc)
- s3 = surflet(intX+0, intY+1, x, y, hashfunc)
- s4 = surflet(intX+1, intY+1, x, y, hashfunc)
- return (s1 + s2 + s3 + s4)
- def fBm(x, y, octs, hashfunc):
- val = 0
- for o in range(octs):
- scale = 2**o
- val += 0.5**o * noise(x*scale, y*scale, hashfunc)
- return val
- class PermHash(object):
- def __init__(self, perm=None):
- if perm is None:
- self._perm = list(range(256))
- random.shuffle(self._perm)
- self._perm += self._perm
- else:
- self._perm = perm
- def __call__(self, *args):
- return self._perm[(self._perm[int(args[0])%len(self._perm)] + int(args[1]))%len(self._perm)]
- def GetSaveState(self):
- return self._perm
- size, freq, octs, data = 200, 1/32.0, 2, []
- hashfunc = PermHash()
- img = Image.new("RGB",(ww, hh))
- for y in range(hh):
- for x in range(ww):
- tx = x - 100
- ty = y - 100
- tt = fBm(tx*freq, ty*freq, octs, hashfunc)
- tt = int((tt+1)/2*Lc)
- data.append(RGBs[tt])
- if 1:
- img.putdata(data)
- imgTk = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=NW, image=imgTk)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement