Advertisement
here2share

# Tk_blobs.py

Feb 19th, 2021 (edited)
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. # Tk_blob.py
  2.  
  3. from tkinter import *
  4. import math
  5. from random import shuffle, randint as rnd
  6.  
  7. root = Tk()
  8.  
  9. ww = 520
  10. hh = 520
  11. r = (min(ww,hh)-5)/2
  12.  
  13. canvas = Canvas(root,width=ww,height=hh)
  14. canvas.pack()
  15.  
  16. rainbow = []
  17. def rgb():
  18.     rainbow.append('#{:02x}{:02x}{:02x}'.format(r,g,b))
  19.    
  20. def wave(z,amp):
  21.     return abs(amp-math.cos(math.radians(z))*amp)/2
  22.    
  23. def wave_animate(amplitude):
  24.     waves = {}
  25.     for amp in range(3,amplitude+1):
  26.         waves[amp] = []
  27.         for z in range(0,3600,int(3600/(amp+30))):
  28.             z *= 0.1
  29.             zzz = int(wave(z,amp+1))
  30.             if zzz == amp:
  31.                 waves[amp] += waves[amp][::-1][1:]
  32.                 break
  33.             else:
  34.                 waves[amp] += [zzz+1]
  35.     return waves
  36.  
  37. WAVES = wave_animate(100)
  38.  
  39. r, g, b = 255, 0, 0
  40. for g in range(256):
  41.     rgb()
  42. for r in range(255, -1, -1):
  43.     rgb()
  44. for b in range(256):
  45.     rgb()
  46. for g in range(255, -1, -1):
  47.     rgb()
  48. for r in range(256):
  49.     rgb()
  50. for b in range(255, -1, -1):
  51.     rgb()
  52.  
  53. p = len(rainbow)/360.0
  54. r360 = [rainbow[int(z*p)] for z in range(360)]
  55.  
  56. points = []
  57. circ = int((math.pi * r**2) / 500)
  58. p = 360.0/circ
  59.  
  60. for z in range(0,ww,2):
  61.     canvas.create_line((0,z,z+ww,z),fill='black')
  62.     canvas.create_line((z,0,z,z+hh),fill='black')
  63.  
  64. ANGLES = list(range(0,circ,3))
  65.  
  66. def find_xy(t):
  67.     t *= p
  68.     wave = of_waves[int(t)]
  69.     x = math.sin(math.radians(t))
  70.     y = math.cos(math.radians(t))
  71.     x,y = [z*(150+wave)+r-5 for z in (x,y)]
  72.     return x,y
  73.    
  74. while 1:
  75.     of_circ = []
  76.     of_waves = dict((z,[]) for z in range(circ+1))
  77.     L = rnd(9,30)
  78.     of_angles = ANGLES[:]
  79.     shuffle(of_angles)
  80.     while len(of_circ) < L+1:
  81.         period = of_angles.pop()
  82.         amp = rnd(10,30)
  83.         of_circ.append((period,amp))
  84.     for i in range(L):
  85.         period,amp = of_circ[i]
  86.         for j in WAVES[amp]:
  87.             of_waves[period%(360+1)].append(j)
  88.             period += 1
  89.        
  90.     of_waves = [sum(of_waves[z]) for z in range(circ)]
  91.     canvas.delete('blob')
  92.     px,py = find_xy(0)
  93.     x0,y0 = px,py
  94.     for t in range(1,circ):
  95.         x,y = find_xy(t)
  96.         # angle = math.degrees(math.atan2(x,y))
  97.         color = r360[int(t*p)]
  98.         canvas.create_line((px,py,x,y),fill=color,width=5,tag='blob')
  99.         px,py = x,y
  100.     color = r360[int(t*p)]
  101.     canvas.create_line((x,y,x0,y0),fill=color,width=5,tag='blob')
  102.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement