here2share

# tk_Basic_Optimizations_Demo.py

Nov 13th, 2025
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. # tk_Basic_Optimizations_Demo.py
  2.  
  3. import tkinter as tk
  4.  
  5. canvas_width = 600
  6. canvas_height = 600
  7. pan_offset_x = 0
  8.  
  9. scene_objects = [
  10.     [50, 50, 50, 0, 0],
  11.     [150, 70, 40, 1, 0],
  12.     [250, 60, 60, 0, 1],
  13.     [350, 80, 30, 1, 1],
  14.     [450, 90, 55, 0, 2],
  15.     [550, 100, 45, 1, 2],
  16.     [100, 250, 70, 0, 0],
  17.     [200, 270, 50, 1, 0],
  18.     [300, 260, 80, 0, 1],
  19.     [400, 280, 35, 1, 1],
  20.     [500, 290, 65, 0, 2],
  21.     [50, 450, 60, 0, 0],
  22.     [150, 470, 45, 1, 0],
  23.     [250, 460, 75, 0, 1],
  24.     [350, 480, 38, 1, 1],
  25.     [450, 490, 60, 0, 2],
  26.     [550, 500, 50, 1, 2]
  27. ]
  28.  
  29. zsort_triangles = [
  30.     [60, 50, 50, 2, 0],
  31.     [80, 150, 50, 2, 0],
  32.     [220, 120, 60, 2, 1],
  33.     [400, 70, 70, 2, 2],
  34.     [520, 150, 80, 2, 3],
  35.     [100, 350, 90, 2, 4],
  36.     [300, 450, 100, 2, 5],
  37.     [580, 300, 70, 2, 6],
  38.     [180, 270, 60, 2, 7],
  39.     [480, 370, 80, 2, 8],
  40.     [360, 240, 90, 2, 9],
  41.     [60, 50, 15, 2, 0],
  42.     [100, 70, 10, 2, 1],
  43.     [140, 90, 12, 2, 2],
  44.     [180, 110, 8, 2, 3],
  45.     [220, 130, 14, 2, 4],
  46.     [260, 150, 9, 2, 5],
  47.     [300, 170, 13, 2, 6],
  48.     [340, 190, 11, 2, 7],
  49.     [380, 210, 10, 2, 8],
  50.     [420, 230, 12, 2, 9],
  51.     [460, 250, 14, 2, 10],
  52.     [500, 270, 9, 2, 11],
  53.     [540, 290, 13, 2, 12],
  54.     [580, 310, 11, 2, 13],
  55.     [100, 350, 10, 2, 14],
  56.     [140, 370, 12, 2, 15],
  57.     [180, 390, 8, 2, 16],
  58.     [220, 410, 14, 2, 17],
  59.     [260, 430, 9, 2, 18],
  60.     [300, 450, 13, 2, 19]
  61. ]
  62.  
  63. occluder = [200, 200, 200, "gray"]
  64. frustum = [120, 120, 480, 480]
  65.  
  66. def get_wrapped_x(x):
  67.     return (x + pan_offset_x) % canvas_width
  68.  
  69. def get_bounding_box(x, y, size):
  70.     return (x, y, x + size, y + size)
  71.  
  72. def is_inside_frustum(x, y, size):
  73.     left, top, right, bottom = get_bounding_box(get_wrapped_x(x), y, size)
  74.     return not (right < frustum[0] or left > frustum[2] or bottom < frustum[1] or top > frustum[3])
  75.  
  76. def is_occluded(x, y, size):
  77.     left, top, right, bottom = get_bounding_box(get_wrapped_x(x), y, size)
  78.     return (left >= occluder[0] and top >= occluder[1] and right <= occluder[0] + occluder[2] and bottom <= occluder[1] + occluder[2])
  79.  
  80. def draw_object(x, y, size, shape_type, dashed=False, stationary=False):
  81.     x = x if stationary else get_wrapped_x(x)
  82.     dash = (2, 2) if dashed else None
  83.     outline = "black"
  84.  
  85.     if shape_type == 0:  # Rectangle
  86.         fill = "blue"
  87.         canvas.create_rectangle(x, y, x + size, y + size, fill=fill if not dashed else "", outline=outline, dash=dash)
  88.     elif shape_type == 1:  # Oval
  89.         fill = "red"
  90.         canvas.create_oval(x, y, x + size, y + size, fill=fill if not dashed else "", outline=outline, dash=dash)
  91.     elif shape_type == 2:  # Triangle
  92.         fill = "green"
  93.         points = [
  94.             x + size / 2, y,
  95.             x, y + size,
  96.             x + size, y + size
  97.         ]
  98.         canvas.create_polygon(points, fill=fill, outline=outline,)
  99.  
  100. def draw_square(x, y, size, fill_color="blue"):
  101.     canvas.create_rectangle(x, y, x + size, y + size, fill=fill_color, outline="black")
  102.  
  103. def render_scene():
  104.     canvas.delete("all")
  105.     canvas.create_text(300, 20, text="Basic Optimizations Demo", fill="black", font=("Arial", 12))
  106.     draw_square(occluder[0], occluder[1], occluder[2], occluder[3])
  107.     canvas.create_text(occluder[0], occluder[1], text="Occluder", fill="black", anchor="s")
  108.     canvas.create_rectangle(frustum[0], frustum[1], frustum[2], frustum[3], outline="cyan", width=2)
  109.     canvas.create_text(frustum[0], frustum[1], text="Frustum", fill="cyan", anchor="s")
  110.  
  111.     all_objects = scene_objects + zsort_triangles
  112.  
  113.     if enable_zsort.get():
  114.         all_objects = sorted(all_objects, key=lambda o: o[2])  # ascending
  115.     else:
  116.         all_objects = sorted(all_objects, key=lambda o: -o[2])  # descending
  117.  
  118.     for obj in all_objects:
  119.         x, y, size, shape_type, _ = obj
  120.         stationary = (shape_type == 2)
  121.         frustum_culled = enable_frustum.get() and not is_inside_frustum(x, y, size)
  122.         occlusion_culled = enable_occlusion.get() and is_occluded(x, y, size)
  123.         dashed = frustum_culled or occlusion_culled
  124.         draw_object(x, y, size, shape_type, dashed=dashed, stationary=stationary)
  125.  
  126. def auto_pan():
  127.     global pan_offset_x
  128.     pan_offset_x -= 2
  129.     render_scene()
  130.     root.after(20, auto_pan)
  131.  
  132. root = tk.Tk()
  133. root.title("Basic Optimizations Demo")
  134. root.geometry("800x600+0+0")
  135.  
  136. canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg="white")
  137. canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  138.  
  139. control_frame = tk.Frame(root)
  140. control_frame.pack(side=tk.RIGHT, fill=tk.Y)
  141.  
  142. enable_frustum = tk.BooleanVar()
  143. enable_occlusion = tk.BooleanVar()
  144. enable_zsort = tk.BooleanVar()
  145.  
  146. tk.Checkbutton(control_frame, text="Frustum Culling", variable=enable_frustum, command=render_scene).pack(anchor="w", padx=10, pady=5)
  147. tk.Checkbutton(control_frame, text="Occlusion Culling", variable=enable_occlusion, command=render_scene).pack(anchor="w", padx=10, pady=5)
  148. tk.Checkbutton(control_frame, text="Z-Index Sorting", variable=enable_zsort, command=render_scene).pack(anchor="w", padx=10, pady=5)
  149.  
  150. render_scene()
  151. auto_pan()
  152. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment