Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_torus_ani.py
- from tkinter import *
- from math import sqrt, sin, cos, pi
- from random import randint as ri
- rgb = [i for i in range(0, 256, 28)]
- colors = ['#{:02x}{:02x}{:02x}'.format(r,g,b) for r in rgb for g in rgb for b in rgb][::-1]
- # Define the torus parameters
- R = 250 # Major radius
- r = 50 # Minor radius
- poly_count = 30 # Number of polygons to approximate the torus surface
- theta_step = 2 * pi / poly_count # Angle increment for the azimuthal direction
- phi_step = 2 * pi / poly_count # Angle increment for the polar direction
- xA, yA = 450, 320
- seg = poly_count // 4 # Only update twice for the visible parts of the torus
- # Define the initial rotation angles
- x_rotate, y_rotate, z_rotate = 0, 0, 0
- ww = 900
- hh = 640
- # Create the window and canvas
- root = Tk()
- root.title("TORUS")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, bg="grey", width=ww, height=hh)
- canvas.grid(row=1, column=1)
- btn = Button(root, text='Quit', width=8, command=root.destroy).place(x=ww-80, y=hh-40)
- def deg(i):
- return round(i * 180 / pi, 2)
- def draw():
- global c
- for j in range(poly_count):
- v1 = vertices[i*poly_count+j]
- v2 = vertices[((i+1)%poly_count)*poly_count+j]
- v3 = vertices[((i+1)%poly_count)*poly_count+(j+1)%poly_count]
- v4 = vertices[i*poly_count+(j+1)%poly_count]
- color = colors[ri(c, c+2)]
- if v1[2] < 60:
- canvas.create_polygon(v1[:2], v2[:2], v3[:2], v4[:2], fill=color, outline=color, tags=('lowest'))
- else:
- canvas.create_polygon(v1[:2], v2[:2], v3[:2], v4[:2], fill=color, outline=color)
- c += 1
- canvas.tag_lower('lowest')
- def o(a, b):
- return list(range(a, b))
- # Define the function for updating the torus animation
- while 1:
- canvas.delete("all")
- c = 0
- # Update the rotation angles
- x_rotate = (x_rotate + 0.11) % (pi*2)
- y_rotate = (y_rotate + 0.05) % (pi*2)
- # Calculate the torus vertices using its parametric equations
- vertices = []
- for i in range(poly_count):
- theta = i * theta_step
- for j in range(poly_count):
- phi = j * phi_step
- x = (R + r * cos(phi)) * cos(theta)
- y = (R + r * cos(phi)) * sin(theta)
- z = r * sin(phi)
- # Apply the rotation transformations
- x1 = x * cos(y_rotate) - z * sin(y_rotate)
- z1 = z * cos(y_rotate) + x * sin(y_rotate)
- y1 = y * cos(x_rotate) - z1 * sin(x_rotate)
- z2 = z1 * cos(x_rotate) + y * sin(x_rotate)
- x2 = x1 * cos(z_rotate) + y1 * sin(z_rotate)
- y2 = y1 * cos(z_rotate) - x1 * sin(z_rotate)
- vertices.append((x2+xA, y2+yA, z2))
- # Draw the torus using the vertices
- for i in range(poly_count):
- draw()
- canvas.create_text(200, 100, text='TORUS', fill='white', font='Arial 60 bold')
- canvas.create_text(50, hh-70, text=f'x: {deg(x_rotate)} deg', fill='black', font='Arial 18', anchor="w")
- canvas.create_text(50, hh-50, text=f'y: {deg(y_rotate)} deg', fill='black', font='Arial 18', anchor="w")
- root.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement