Advertisement
here2share

# tk_Grid_of_Triangles_2.py

Dec 23rd, 2023 (edited)
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. # tk_Grid_of_Triangles_2.py
  2.  
  3. import tkinter as tk
  4. import math
  5. import random
  6.  
  7. ww = 500
  8. hh = 500
  9.  
  10. colors = ['#%02X%02X%02X'%(r,g,b) for r in range(0, 256, 15) for g in range(0, 256, 15) for b in range(0, 256, 15)]
  11. random.shuffle(colors)
  12. Lc = len(colors)
  13. print(Lc)
  14.  
  15. def rotate_polygon(polygon, angle_degrees):
  16.     angle_radians = math.radians(angle_degrees)
  17.     coords = canvas.coords(polygon)
  18.    
  19.     # Find the centroid of the polygon
  20.     x_coords = coords[::2]  # Extract x-coordinates
  21.     y_coords = coords[1::2]  # Extract y-coordinates
  22.    
  23.     # Calculate centroid
  24.     centroid_x = sum(x_coords) / len(x_coords)
  25.     centroid_y = sum(y_coords) / len(y_coords)
  26.    
  27.     # Translate centroid to the origin
  28.     translated_coords = [(x - centroid_x, y - centroid_y) for x, y in zip(x_coords, y_coords)]
  29.    
  30.     # Rotate each point around the origin
  31.     rotated_coords = []
  32.     for x, y in translated_coords:
  33.         new_x = x * math.cos(angle_radians) - y * math.sin(angle_radians)
  34.         new_y = x * math.sin(angle_radians) + y * math.cos(angle_radians)
  35.         rotated_coords.extend([new_x, new_y])
  36.    
  37.     # Translate back to original position by adding centroid coordinates
  38.     final_coords = [coord + centroid_x if i % 2 == 0 else coord + centroid_y for i, coord in enumerate(rotated_coords)]
  39.    
  40.     # Update the polygon coordinates on the canvas
  41.     canvas.coords(polygon, *final_coords)
  42.  
  43. def draw_triangles(size):
  44.     height = size * math.sqrt(3) / 2
  45.     row = 0
  46.     while row * height < ww:
  47.         half_offset_x = -2 if row % 2 else size // 2 - 2
  48.         col = -1
  49.         while col * size < hh + size * 2:
  50.             x0 = (col - 1) * size - 2 - half_offset_x
  51.             y0 = row * height
  52.             x1 = x0 + size
  53.             y1 = y0 + height
  54.             triangle = canvas.create_polygon(x0, y0, x1, y0, x1 - size / 2, y1, tags=("triangle"), outline='black', fill=colors[(row+col*250)])
  55.             triangle = canvas.create_polygon(x0, y0, x1, y0, x1 - size / 2, y1, tags=("triangle"), outline='black', fill=colors[(row*500+col)])
  56.             rotate_polygon(triangle, 180)
  57.             canvas.move(triangle, size // 2, size * 0.3)
  58.             col += 1
  59.         row += 1
  60.  
  61. def change_triangle_colors(event):
  62.     color_index = 0
  63.     random.shuffle(colors)
  64.     triangles = canvas.find_withtag("triangle")
  65.     for triangle in triangles:
  66.         canvas.itemconfig(triangle, fill=colors[color_index])
  67.         color_index = (color_index + 1) % Lc
  68.  
  69. root = tk.Tk()
  70. root.title("Grid of Triangles")
  71.  
  72. canvas = tk.Canvas(root, width=ww, height=hh, bg='black')
  73. canvas.pack()
  74.  
  75. root.bind("<space>", change_triangle_colors)
  76.  
  77. triangle_size = 50
  78. draw_triangles(triangle_size)
  79.  
  80. root.mainloop()
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement