Advertisement
here2share

# Tk_torus_ani.py

May 29th, 2023 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. # Tk_torus_ani.py
  2.  
  3. from tkinter import *
  4. from math import sqrt, sin, cos, pi
  5. from random import randint as ri
  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.  
  10. # Define the torus parameters
  11. R = 250  # Major radius
  12. r = 50  # Minor radius
  13. poly_count = 30  # Number of polygons to approximate the torus surface
  14. theta_step = 2 * pi / poly_count  # Angle increment for the azimuthal direction
  15. phi_step = 2 * pi / poly_count  # Angle increment for the polar direction
  16. xA, yA = 450, 320
  17. seg = poly_count // 4  # Only update twice for the visible parts of the torus
  18.  
  19. # Define the initial rotation angles
  20. x_rotate, y_rotate, z_rotate = 0, 0, 0
  21.  
  22. ww = 900
  23. hh = 640
  24.  
  25. # Create the window and canvas
  26. root = Tk()
  27. root.title("TORUS")
  28. root.geometry("%dx%d+0+0"%(ww,hh))
  29. canvas = Canvas(root, bg="grey", width=ww, height=hh)
  30. canvas.grid(row=1, column=1)
  31. btn = Button(root, text='Quit', width=8, command=root.destroy).place(x=ww-80, y=hh-40)
  32.  
  33. def deg(i):
  34.     return round(i * 180 / pi, 2)
  35.  
  36. def draw():
  37.     global c
  38.     for j in range(poly_count):
  39.         v1 = vertices[i*poly_count+j]
  40.         v2 = vertices[((i+1)%poly_count)*poly_count+j]
  41.         v3 = vertices[((i+1)%poly_count)*poly_count+(j+1)%poly_count]
  42.         v4 = vertices[i*poly_count+(j+1)%poly_count]
  43.         color = colors[ri(c, c+2)]
  44.  
  45.         if v1[2] < 60:
  46.             canvas.create_polygon(v1[:2], v2[:2], v3[:2], v4[:2], fill=color, outline=color, tags=('lowest'))
  47.         else:
  48.             canvas.create_polygon(v1[:2], v2[:2], v3[:2], v4[:2], fill=color, outline=color)
  49.         c += 1
  50.     canvas.tag_lower('lowest')
  51.        
  52. def o(a, b):
  53.     return list(range(a, b))
  54.  
  55. # Define the function for updating the torus animation
  56. while 1:
  57.     canvas.delete("all")
  58.     c = 0
  59.     # Update the rotation angles
  60.     x_rotate = (x_rotate + 0.11) % (pi*2)
  61.     y_rotate = (y_rotate + 0.05) % (pi*2)
  62.     # Calculate the torus vertices using its parametric equations
  63.     vertices = []
  64.     for i in range(poly_count):
  65.         theta = i * theta_step
  66.                
  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.             # Apply the rotation transformations
  73.             x1 = x * cos(y_rotate) - z * sin(y_rotate)
  74.             z1 = z * cos(y_rotate) + x * sin(y_rotate)
  75.             y1 = y * cos(x_rotate) - z1 * sin(x_rotate)
  76.             z2 = z1 * cos(x_rotate) + y * sin(x_rotate)
  77.             x2 = x1 * cos(z_rotate) + y1 * sin(z_rotate)
  78.             y2 = y1 * cos(z_rotate) - x1 * sin(z_rotate)
  79.             vertices.append((x2+xA, y2+yA, z2))
  80.    
  81.     # Draw the torus using the vertices
  82.     for i in range(poly_count):
  83.         draw()
  84.     canvas.create_text(200, 100, text='TORUS', fill='white', font='Arial 60 bold')
  85.     canvas.create_text(50, hh-70, text=f'x: {deg(x_rotate)} deg', fill='black', font='Arial 18', anchor="w")
  86.     canvas.create_text(50, hh-50, text=f'y: {deg(y_rotate)} deg', fill='black', font='Arial 18', anchor="w")
  87.     root.update()
  88.  
  89. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement