Advertisement
here2share

# Tk_quasicrystals.py

Apr 7th, 2021
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. # Tk_quasicrystals.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. import math
  6. import random
  7.  
  8. # image size
  9. ww = 640
  10. hh = 640
  11.  
  12. root = Tk()
  13. root.title("Tk Quasicrystals")
  14. root.geometry("%dx%d+0+0"%(ww,hh))
  15. canvas = Canvas(root, width=ww, height=hh)
  16. canvas.grid()
  17.  
  18. img = Image.new("RGB", (ww, hh))
  19. pixels = img.load()
  20.  
  21. while 1:
  22.     f = random.random() * 40.0 + 10 # frequency
  23.     p = random.random() * math.pi # phase
  24.     n = random.randint(10, 20) # of rotations
  25.     print(f,p,n)
  26.     for ky in range(hh):
  27.         y = float(ky) / (hh - 1) * 4 * math.pi - 2 * math.pi
  28.         for kx in range(ww):
  29.             x = float(kx) / (ww - 1) * 4 * math.pi - 2 * math.pi
  30.             z = 0.0
  31.             for i in range(n):
  32.                 r = math.hypot(x, y)
  33.                 a = math.atan2(y, x) + i * math.pi * 2.0 / n
  34.                 z += math.cos(r * math.sin(a) * f + p)
  35.             c = int(round(255 * z / n))
  36.             pixels[kx, ky] = (c, c, c) # grayscale
  37.             canvas.update()
  38.     imgTk = ImageTk.PhotoImage(img)
  39.     canvas.create_image(0, 0, image=imgTk, anchor='nw')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement