Advertisement
here2share

# Tk_0-3-4-7_animation.py

Jan 1st, 2022 (edited)
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. # Tk_0-3-4-7_animation.py
  2.  
  3. import math
  4. from time import time
  5. from tkinter import *
  6.  
  7. ww=hh=500
  8. ctr=ww/2
  9.  
  10. t=1.0/2
  11. d_fade=[round(i*0.01,2) for i in range(1000)]
  12. z=[0.0 for i in range(200)]
  13. z += [round(t*i*0.01,3) for i in range(200)]
  14. z += [1.0 for i in range(400)]
  15. z += [round(t*i*0.01,3) for i in range(200,0,-1)]
  16. d_fade=dict(zip(d_fade,z))
  17. d_fade[10.0] = 0.0
  18. L_fade=len(d_fade)
  19.  
  20. root=Tk()
  21. root.title("# 0-3-4-7 Animation")
  22.  
  23. root.geometry("%dx%d+0+0"%(ww,hh))
  24.  
  25. root2=Tk()
  26.  
  27. root2.geometry("%dx%d+0+0"%(ww,hh))
  28.  
  29. canvas=Canvas(root, width=ww, height=hh)
  30. canvas.pack()
  31.  
  32. cv2=Canvas(root2, width=ww, height=hh)
  33. cv2.pack()
  34.  
  35. root.overrideredirect(True)
  36. root.resizable(False, False)
  37. root.update_idletasks()
  38. root.wm_attributes("-topmost", True)
  39.  
  40. root2.overrideredirect(True)
  41. root2.resizable(False, False)
  42. root2.update_idletasks()
  43. root2.wm_attributes("-topmost", True)
  44.    
  45. omega=1.0 # kiloradians per second
  46. ratio=0.8
  47. m=7
  48. n=3
  49.  
  50. side=min(ww, hh)
  51. theta=omega*time()
  52. phi=theta*(1.0-m/n)
  53. cx,cy=ww/2,hh/2
  54. r0=0.4*side
  55. r1=r0*n/m
  56.  
  57. # 7-pointed star
  58. steps=200
  59. star7pt=[]
  60. for k in range(-10,steps):
  61.     theta=k*n*2.0*math.pi/steps
  62.     phi=theta*(1.0-m/n)
  63.     x,y=(cx+(r0-r1)*math.cos(theta), cy+(r0-r1)*math.sin(theta))
  64.     x,y=(x+ratio*r1*math.cos(phi), y+ratio*r1*math.sin(phi))
  65.     star7pt += [(x,y)]
  66.  
  67. def dot(x, y):
  68.     cv2.create_oval(x+rdot, y+rdot, x-rdot, y-rdot, fill=color, outline='')
  69.     canvas.create_oval(x+rdot, y+rdot, x-rdot, y-rdot, fill=color, outline='')
  70.  
  71. def polygon(obj):
  72.     cv2.create_line(obj, fill=color, width=5)
  73.  
  74. def txt(x, y):
  75.     cv2.create_text(x, y, text=c, font=('verdana','30'), fill='blue')
  76.     canvas.create_text(x, y, text=c, font=('verdana','30'), fill='blue')
  77.  
  78. def fade():
  79.     root2.wm_attributes('-alpha', alpha) # 0.0 to 1.0
  80.    
  81. def quit(e=None):
  82.     root2.destroy()
  83.     root.destroy()
  84.  
  85. btnA = Button(cv2, text="CLOSE", font=("Ariel, 10"), command=quit)
  86. btnB = Button(canvas, text="CLOSE", font=("Ariel, 10"), command=quit)
  87. btnA.place(x=ww-100,y=hh-50)
  88. btnB.place(x=ww-100,y=hh-50)
  89.  
  90. zzz=time()
  91. while 1:
  92.     xy=[]
  93.     circles=[]
  94.     triangles=[]
  95.     ttt=time()-zzz
  96.     show=ttt%70
  97.     t=round((ttt)%10,2)
  98.     alpha=d_fade[t]
  99.     for i in range(m-n):
  100.         theta=omega*ttt+2.0*math.pi*i/(m-n)
  101.         px,py=(cx+(r0-r1)*math.cos(theta), cy+(r0-r1)*math.sin(theta))
  102.         triangle=[]
  103.         for j in range(n):
  104.             phi=(theta-2.0*math.pi*i/(m-n))*(1.0-m/n)
  105.             psi=phi+2.0*math.pi*j/n
  106.             qx,qy=(px+ratio*r1*math.cos(psi), py+ratio*r1*math.sin(psi))
  107.             xy += [(qx,qy)]
  108.             triangle += [(qx,qy)]
  109.         triangles += [triangle]
  110.  
  111.            
  112.         phi=(theta-2.0*math.pi*i/(m-n))*(1.0-m/n)
  113.         qx,qy=px+math.cos(phi), py+math.sin(phi)
  114.         circles += [(qx, qy)]
  115.    
  116.     # draw
  117.     canvas.delete('all')
  118.     cv2.delete('all')
  119.    
  120.     # 7-Pointed Star (always drawn first)
  121.     color='grey90'
  122.     cv2.create_line(star7pt, fill=color, width=5)
  123.     canvas.create_line(star7pt, fill=color, width=5)
  124.    
  125.     # Circles
  126.     if show > 60:
  127.         color='lightgreen'
  128.         for x,y in circles:
  129.             rdot=70
  130.             cv2.create_oval(x+rdot, y+rdot, x-rdot, y-rdot, fill=color, outline='')
  131.        
  132.         # Full Circle
  133.         r=188
  134.         cv2.create_oval(cx+r, cy+r, cx-r, cy-r, fill='', outline='cyan', width=5)
  135.  
  136.     # 2 Prisms
  137.     elif show > 50:
  138.         color='pink'
  139.         polygon([xy[i-1] for i in (1,2,5,4,1,3,6,4)])
  140.         polygon([xy[i-1] for i in (7,8,11,10,7,9,12,10)])
  141.         polygon([xy[i-1] for i in (8,9,12,11)])
  142.         polygon([xy[i-1] for i in (2,3,6,5)])
  143.        
  144.     # Hyper Spiral 2
  145.     elif show > 40:
  146.         color='green'
  147.         polygon([xy[i-1] for i in (1,5,7,10,2,6,8,11,3,4,9,12,1)])
  148.        
  149.     # Hyper Spiral
  150.     elif show > 30:
  151.         color='purple'
  152.         polygon([xy[i-1] for i in (1,5,9,10,2,6,7,11,3,4,8,12,1)])
  153.        
  154.     # Bouncy Squares
  155.     elif show > 20:
  156.         color='lightgreen'
  157.         polygon([xy[i-1] for i in (1,5,9,12,1)])
  158.         color='lightblue'
  159.         polygon([xy[i-1] for i in (2,6,7,10,2)])
  160.         color='pink'
  161.         polygon([xy[i-1] for i in (3,4,8,11,3)])
  162.    
  163.     # Squares
  164.     elif show > 10:
  165.         color='lightblue'
  166.         for i in range(3):
  167.             polygon([xy[i+j] for j in range(0,12,3)]+[xy[i]])
  168.  
  169.     # Triangles
  170.     else:
  171.         color='lightgreen'
  172.         for triangle in triangles:
  173.             polygon(triangle+[triangle[0]])
  174.        
  175.     # Dots (always drawn last)
  176.     c=1
  177.     for x,y in xy:
  178.         color='orange'
  179.         rdot=7
  180.         dot(x,y)
  181.         # txt(x,y)
  182.         c += 1
  183.    
  184.     fade()
  185.     canvas.update()
  186.     cv2.update()
  187.  
  188. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement