Advertisement
here2share

# tk_Spectral_Harmonograph.py

Dec 30th, 2020
1,446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. # tk_Spectral_Harmonograph.py
  2.  
  3. from math import sin,pi
  4. import random as r
  5. from colorsys import hsv_to_rgb
  6.  
  7. try:
  8.     import tkinter as tk
  9. except:
  10.     import Tkinter as tk
  11.  
  12. root=tk.Tk()
  13. root.title("Tk Spectral Harmonograph")
  14. ww,hh=1280,720
  15.  
  16. canvas=tk.Canvas(root,width=ww,height=hh)
  17. canvas.pack(fill="both",expand=True)
  18.  
  19. gap=100
  20. ww-=gap
  21. hh-=gap
  22. depth=ww
  23. hui=.159
  24. dec=0.999999
  25. dt=0.01
  26. mx=10
  27. sd=0.002
  28.  
  29. def scale(length):
  30.     while True:
  31.         a1,a2=r.randint(-mx,mx),r.randint(-mx,mx)
  32.         max=abs(a1)+abs(a2)
  33.         if max>0: break
  34.     return a1,a2,length/(2*max)
  35.  
  36. def hsv2rgb(*hsv):
  37.     rgb = hsv_to_rgb(*hsv)
  38.     r = hex(int(rgb[0]*255))[2:]
  39.     if len(r) < 2:
  40.         r = "0"+r
  41.     g = hex(int(rgb[1]*255))[2:]
  42.     if len(g) < 2:
  43.         g = "0"+g
  44.     b = hex(int(rgb[2]*255))[2:]
  45.     if len(b) < 2:
  46.         b = "0"+b
  47.     hexCode = "#" + r + g + b
  48.     # hsv[0]+=0.01
  49.     return hexCode
  50.  
  51. while True:
  52.     trail=[]
  53.  
  54.     ax1,ax2,xscale=scale(ww)
  55.     ay1,ay2,yscale=scale(hh)
  56.     # az1,az2,zscale=scale(depth)
  57.  
  58.     fx1, fx2 = r.randint(1,mx) + r.gauss(0,sd), r.randint(1,mx) + r.gauss(0,sd)
  59.     fy1, fy2 = r.randint(1,mx) + r.gauss(0,sd), r.randint(1,mx) + r.gauss(0,sd)
  60.     # fz1, fz2 = r.randint(1,mx) + r.gauss(0,sd), r.randint(1,mx) + r.gauss(0,sd)
  61.  
  62.     px1, px2 = r.uniform(0,2*pi), r.uniform(0,2*pi)
  63.     py1, py2 = r.uniform(0,2*pi), r.uniform(0,2*pi)
  64.     # pz1, pz2 = r.uniform(0,2*pi), r.uniform(0,2*pi)
  65.  
  66.     x=y=z=0.0
  67.     k=1
  68.     hue=0
  69.     t=0
  70.     trail=[(x,y,z,hsv2rgb(hue,1,1))]
  71.     for j in range (50):
  72.         for i in range (2000):
  73.             x = xscale * k * (ax1*sin(t * fx1 + px1) + ax2*sin(t * fx2 + px2))
  74.             y = yscale * k * (ay1*sin(t * fy1 + py1) + ay2*sin(t * fy2 + py2))
  75.             # z = zscale * k * (az1*sin(t * fz1 + pz1) + az2*sin(t * fz2 + pz2))
  76.             x+=ww/2
  77.             y+=hh/2
  78.             trail.append((x,y,z,hsv2rgb(hue,1,1)))
  79.             hue = (hue + dt*hui) % 360
  80.             t+=dt
  81.             k*=dec
  82.  
  83.     canvas.delete('all')
  84.     for x,y,z,color in trail:
  85.         canvas.create_oval((x,y,x+1,y+1),fill=color)
  86.     root.update()
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement