Advertisement
here2share

# Tk_torus_ani_3.py

Jun 7th, 2023
1,266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. # Tk_torus_ani_3.py
  2.  
  3. from tkinter import *
  4. from math import sqrt, sin, cos, pi
  5. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  6.  
  7. rgb = [i for i in range(0, 256, 28)]
  8. colors = ['#{:02x}{:02x}{:02x}'.format(r,g,b) for r in rgb for g in rgb for b in rgb][::-1]
  9. # Define the initial rotation angles
  10. x_rotate, y_rotate, z_rotate = 0, 0, 0
  11.  
  12. ww = 900
  13. hh = 640
  14. cx, cy = ww//2, hh//2
  15.  
  16. # Create the window and canvas
  17. root = Tk()
  18. root.title("TORUS")
  19. root.geometry("%dx%d+0+0"%(ww, hh))
  20. canvas = Canvas(root, bg="grey", width=ww, height=hh)
  21. canvas.grid(row=1, column=1)
  22. btn = Button(root, text='Quit', width=8, command=root.destroy).place(x=ww-80, y=hh-40)
  23.  
  24. # Define the torus parameters
  25. R = 250  # Major radius
  26. r = 50  # Minor radius
  27. poly_count = 30  # Number of polygons to approximate the torus surface
  28. theta_step = 2 * pi / poly_count  # Angle increment for the azimuthal direction
  29. phi_step = 2 * pi / poly_count  # Angle increment for the polar direction
  30.  
  31. def deg(i):
  32.     return round(i * 180 / pi, 2)
  33.  
  34. def plot():
  35.     global c
  36.     for j in range(poly_count):
  37.         v1 = vertices[i*poly_count+j]
  38.         v2 = vertices[((i+1)%poly_count)*poly_count+j]
  39.         v3 = vertices[((i+1)%poly_count)*poly_count+(j+1)%poly_count]
  40.         v4 = vertices[i*poly_count+(j+1)%poly_count]
  41.         color = colors[int(v1[0])]
  42.  
  43.         draw.polygon([v1, v2, v3, v4], fill=color, outline=color)
  44.         c += 1
  45.        
  46. def o(a, b):
  47.     return list(range(a, b))
  48.  
  49. # Define the function for updating the torus animation
  50. while 1:
  51.     img = Image.new("RGBA", (ww, hh), (128, 128, 128, 255))
  52.     draw = ImageDraw.Draw(img)
  53.     blur_radius = 0.001 * min(img.size)
  54.  
  55.     c = 0
  56.    
  57.     # Update the rotation angles
  58.     x_rotate = (x_rotate + 0.11) % (pi*2)
  59.     y_rotate = (y_rotate + 0.07) % (pi*2)
  60.    
  61.     # Calculate the torus vertices using its parametric equations
  62.     vertices = []
  63.     poly_z = []
  64.     for i in range(poly_count):
  65.         theta = i * theta_step
  66.         zzz = []
  67.         for j in range(poly_count):
  68.             phi = j * phi_step
  69.             x = (R + r * cos(phi)) * cos(theta)
  70.             y = (R + r * cos(phi)) * sin(theta)
  71.             z = r * sin(phi)
  72.            
  73.             # Apply the rotation transformations
  74.             x1 = x * cos(y_rotate) - z * sin(y_rotate)
  75.             z1 = z * cos(y_rotate) + x * sin(y_rotate)
  76.             y1 = y * cos(x_rotate) - z1 * sin(x_rotate)
  77.             z2 = z1 * cos(x_rotate) + y * sin(x_rotate)
  78.             x2 = x1 * cos(z_rotate) + y1 * sin(z_rotate)
  79.             y2 = y1 * cos(z_rotate) - x1 * sin(z_rotate)
  80.            
  81.             vertices.append((x2 + ww // 2, y2 + hh // 2))
  82.             zzz.append(z2)
  83.         poly_z.append((i, max(zzz)))
  84.  
  85.     sorted_poly_count = [i for i, j in sorted(poly_z, key=lambda x: x[1], reverse=True)]
  86.  
  87.     # Draw the torus using the sorted vertices
  88.     for i in sorted_poly_count:
  89.         plot()
  90.  
  91.     tkimg = ImageTk.PhotoImage(img)
  92.     canvas.create_image((cx, cy), image=tkimg)
  93.  
  94.     canvas.create_text(200, 100, text='TORUS', fill='white', font='Arial 60 bold')
  95.     canvas.create_text(50, hh-70, text=f'x: {deg(x_rotate)} deg', fill='black', font='Arial 18', anchor="w")
  96.     canvas.create_text(50, hh-50, text=f'y: {deg(y_rotate)} deg', fill='black', font='Arial 18', anchor="w")
  97.     root.update()
  98.  
  99. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement