Advertisement
here2share

# Tk_6_sided_snowflakes.py

Sep 11th, 2021 (edited)
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # Tk_6_sided_snowflakes.py
  2.  
  3. from math import cos,sin,pi,radians,degrees,atan2
  4. import random
  5.  
  6. ri=random.randint
  7.  
  8. try:
  9.     import tkinter as tk
  10. except:
  11.     import Tkinter as tk
  12.  
  13. root=tk.Tk()  
  14. root.title("Tk 6 Sided Snowflakes")
  15.  
  16. ww=640
  17. hh=640
  18.  
  19. wc=ww/2
  20. hc=hh/2
  21.  
  22. class Cv(): 0
  23. cv=Cv()
  24. xy={}
  25. span=ww/2-20
  26. curve=pi/12
  27.  
  28. mouse=[ww/2,hh/2]
  29.  
  30. def find_xy(d, theta, x0=0, y0=0):
  31.     theta_rad = pi/2 - radians(theta)
  32.     return x0 + d*cos(theta_rad), y0 + d*sin(theta_rad)
  33.    
  34. def dist__(a,b):
  35.     return (abs(a)**2+abs(b)**2)**0.5
  36.  
  37. def degrees__(x1,y1,x2,y2):
  38.     return (degrees(atan2((x2-x1),(y2-y1)*-1))+90)%360 if (y2-y1) else 0.0
  39.  
  40. for y in range(span):
  41.     for ym in (-y,y):
  42.         for x in range(span):
  43.             for xm in (-x,x):
  44.                 deg=int(degrees__(0, 0, xm, ym))
  45.                 distance=int(dist__(xm, ym))
  46.                 if distance > span:
  47.                     break
  48.                 if deg <= 30 and xm < 1:
  49.                     xy[xm,ym]=distance,deg
  50.  
  51. arm=[]
  52. ttt=range(20,100,5)
  53. for y in ttt:
  54.     for ym in (-y,y):
  55.         for x in ttt:
  56.             for xm in (-x,x):
  57.                 arm+=[(xm,ym)]
  58.  
  59. canvas=tk.Canvas(root,width=ww,height=hh,bg="dark blue")
  60. canvas.pack(fill="both",expand=True)
  61.  
  62. def mmove(event):
  63.     mouse[:]=[z*1.0 for z in (event.x,event.y)]
  64. canvas.bind('<Motion>',mmove)
  65.  
  66. def rgb2hex(r,g,b):
  67.     return '#{:02x}{:02x}{:02x}'.format(r,g,b)
  68.  
  69. def rndrgb():
  70.     r,g,b=([ri(0, 255) for _ in 'rgb'])
  71.     return rgb2hex(r,g,b)
  72.  
  73. def plot():
  74.     canvas.delete(tk.ALL)
  75.     snowflake=[(ri(150,290), 320)]
  76.     px,py=random.choice(xylist[-20:])
  77.    
  78.     count = ri(9,18)
  79.     random.shuffle(arm)
  80.     for x,y in arm:
  81.         t=x+px,y+py
  82.         if t in xylist:
  83.             snowflake+=[t]
  84.             px,py=t
  85.             if not count:
  86.                 break
  87.             count -= 1
  88.     snowflake+=[(60,320),(ri(90,240)-span,-ri(5,200))]
  89.        
  90.     for t in range(0,360,60):
  91.         aaa=[]
  92.         bbb=[]
  93.         for x,y in snowflake:
  94.             try:
  95.                 dist,angle=xy[x-wc,y-hc]
  96.             except:
  97.                 angle=degrees__(0, 0, x-wc, y-hc)
  98.                 dist=dist__(x, y)
  99.             t%=360
  100.             xxx,yyy=find_xy(dist,t+angle)
  101.             aaa+=[(xxx+wc,yyy+hc)]
  102.             xxx,yyy=find_xy(dist,t-angle)
  103.             bbb+=[(xxx+wc,yyy+hc)]
  104.         canvas.create_polygon(aaa,width=3,outline=cv.color,fill='')
  105.         canvas.create_polygon(bbb,width=3,outline=cv.color,fill='')
  106.     '''
  107.     '''
  108.     canvas.update()
  109.  
  110. xylist=list(xy)
  111. xylist.sort(key=sum)
  112. xylist=[(x2+wc,y2+hc) for x2,y2 in xylist]
  113. xyL=len(xylist)
  114.  
  115. cv.color="white"
  116.  
  117. def tick():
  118.     plot()
  119.     canvas.after(1000,tick)
  120.  
  121. tick()
  122. '''
  123. '''
  124.  
  125. tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement