Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_torus_ani_3.py
- from tkinter import *
- from math import sqrt, sin, cos, pi
- from PIL import Image, ImageTk, ImageDraw, ImageFilter
- 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 initial rotation angles
- x_rotate, y_rotate, z_rotate = 0, 0, 0
- ww = 900
- hh = 640
- cx, cy = ww//2, hh//2
- # 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)
- # 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
- def deg(i):
- return round(i * 180 / pi, 2)
- def plot():
- 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[int(v1[0])]
- draw.polygon([v1, v2, v3, v4], fill=color, outline=color)
- c += 1
- def o(a, b):
- return list(range(a, b))
- # Define the function for updating the torus animation
- while 1:
- img = Image.new("RGBA", (ww, hh), (128, 128, 128, 255))
- draw = ImageDraw.Draw(img)
- blur_radius = 0.001 * min(img.size)
- c = 0
- # Update the rotation angles
- x_rotate = (x_rotate + 0.11) % (pi*2)
- y_rotate = (y_rotate + 0.07) % (pi*2)
- # Calculate the torus vertices using its parametric equations
- vertices = []
- poly_z = []
- for i in range(poly_count):
- theta = i * theta_step
- zzz = []
- 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 + ww // 2, y2 + hh // 2))
- zzz.append(z2)
- poly_z.append((i, max(zzz)))
- sorted_poly_count = [i for i, j in sorted(poly_z, key=lambda x: x[1], reverse=True)]
- # Draw the torus using the sorted vertices
- for i in sorted_poly_count:
- plot()
- tkimg = ImageTk.PhotoImage(img)
- canvas.create_image((cx, cy), image=tkimg)
- 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