zhong (钟鹏群) 3 týždňov pred
rodič
commit
884c1d07ee
1 zmenil súbory, kde vykonal 80 pridanie a 31 odobranie
  1. 80 31
      src/yyy.py

+ 80 - 31
src/yyy.py

@@ -1,4 +1,5 @@
 import tkinter as tk
+import math
 
 class SquareWindow:
     def __init__(self):
@@ -14,48 +15,79 @@ class SquareWindow:
         self.canvas = tk.Canvas(self.root, width=self.window_size, height=self.window_size, bg="white")
         self.canvas.pack(fill=tk.BOTH, expand=True)
         
-        # Draw a square that is 90% of the window size
-        square_size = int(self.window_size * 0.9)  # 90% of 1000 = 900
+        # Draw the main square (numbered as 0) and all nested squares
+        all_numbers = self.draw_nested_squares(outer_square_num=0, inner_square_count=79)
+        print(f"All square numbers: {all_numbers}")
+    
+    def draw_nested_squares(self, outer_square_num, inner_square_count, x1=None, y1=None, x2=None, y2=None, parent_square_info=None):
+        """
+        Draw nested squares recursively.
         
-        # Calculate the position to center the square
-        margin = (self.window_size - square_size) // 2  # (1000 - 900) / 2 = 50
+        Args:
+            outer_square_num: The number of the outer square
+            inner_square_count: How many inner squares to draw inside the outer square
+            x1, y1, x2, y2: Coordinates of the outer square (if None, assumes it's the main square)
+            parent_square_info: Information about the parent square for positioning
         
-        # Coordinates for the square (centered in the window)
-        x1, y1 = margin, margin
-        x2, y2 = margin + square_size, margin + square_size
-        
-        # Draw the square
-        self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", width=2)
-        
-        # Determine how many squares to draw based on the desired number
-        desired_number = 79  # You can change this to any number you want to draw
-        
-        # Find the closest perfect square that is >= desired_number
-        import math
-        n = math.ceil(math.sqrt(desired_number))  # For 19, sqrt(19) ≈ 4.36, ceil(4.36) = 5, so n=5
-        actual_number = n * n  # For n=5, actual_number = 25
+        Returns:
+            A list of all square numbers that were drawn
+        """
+        # Calculate the size and position of the outer square
+        if x1 is None or y1 is None or x2 is None or y2 is None:
+            # This is the main outer square (number 0)
+            square_size = int(self.window_size * 0.9)  # 90% of 1000 = 900
+            margin = (self.window_size - square_size) // 2  # (1000 - 900) / 2 = 50
+            x1, y1 = margin, margin
+            x2, y2 = margin + square_size, margin + square_size
+            
+            # Draw the main square and label it
+            self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", width=2)
+            
+            # Add number 0 in the center of the main square
+            center_x = x1 + square_size // 2
+            center_y = y1 + square_size // 2
+            self.canvas.create_text(center_x, center_y, text=str(outer_square_num), fill="black", font=("Arial", 12))
+        else:
+            # This is a nested square, draw it with a different color
+            self.canvas.create_rectangle(x1, y1, x2, y2, outline="gray", width=1)
+            
+            # Add number in the center of the square
+            center_x = x1 + (x2 - x1) // 2
+            center_y = y1 + (y2 - y1) // 2
+            self.canvas.create_text(center_x, center_y, text=str(outer_square_num), fill="black", font=("Arial", 10))
+
+        # Find the closest perfect square that is >= inner_square_count
+        n = math.ceil(math.sqrt(inner_square_count))
+        actual_number = n * n
         
-        print(f"You wanted {desired_number} squares, but we're drawing {actual_number} squares in a {n}x{n} grid.")
+        print(f"You wanted {inner_square_count} squares, but we're drawing {actual_number} squares in a {n}x{n} grid for square #{outer_square_num}.")
         
-        # Calculate the size of each small square (1/n of the main square's side minus 5 pixels)
-        small_square_size = (square_size // n) - 5  # (900 / 5) - 5 = 175 pixels
+        # Calculate the size of each small square (1/n of the main square's side minus 2 pixels)
+        square_width = x2 - x1
+        square_height = y2 - y1
+        small_square_size = min((square_width // n) - 2, (square_height // n) - 2)
         
-        # Draw only the required number of squares (up to desired_number) in an n x n grid
-        square_number = 1  # Start numbering from 1
+        # Draw the required number of squares (up to inner_square_count) in an n x n grid
+        square_number_base = outer_square_num + 1  # Start numbering after the outer square
+        all_numbers = [outer_square_num]  # Include the outer square number
         
         # Calculate spacing to distribute squares evenly with gaps
         total_used_space = n * small_square_size  # Total space occupied by squares
-        remaining_space = square_size - total_used_space  # Remaining space for gaps
-        gap_per_row = remaining_space // (n + 1)  # Gap between squares and margins
+        remaining_space_x = square_width - total_used_space  # Remaining space for gaps in x direction
+        remaining_space_y = square_height - total_used_space  # Remaining space for gaps in y direction
+        gap_x = remaining_space_x // (n + 1)  # Gap between squares and margins in x
+        gap_y = remaining_space_y // (n + 1)  # Gap between squares and margins in y
         
         for i in range(n):  # Rows
             for j in range(n):  # Columns
-                if square_number > desired_number:
+                current_inner_num = square_number_base + (i * n + j)
+                
+                if current_inner_num > outer_square_num + inner_square_count:
                     break  # Stop drawing if we've reached the desired number
                 
                 # Calculate position for each small square with proper spacing
-                small_x = x1 + gap_per_row + j * (small_square_size + gap_per_row)
-                small_y = y1 + gap_per_row + i * (small_square_size + gap_per_row)
+                small_x = x1 + gap_x + j * (small_square_size + gap_x)
+                small_y = y1 + gap_y + i * (small_square_size + gap_y)
                 
                 # Draw the small square
                 self.canvas.create_rectangle(
@@ -67,12 +99,29 @@ class SquareWindow:
                 # Add number in the center of each small square
                 center_x = small_x + small_square_size // 2
                 center_y = small_y + small_square_size // 2
-                self.canvas.create_text(center_x, center_y, text=str(square_number), fill="red", font=("Arial", 10))
+                self.canvas.create_text(center_x, center_y, text=str(current_inner_num), fill="red", font=("Arial", 10))
+                
+                # If this is the 2nd square in the main grid, draw 7 smaller squares inside it
+                if outer_square_num == 0 and current_inner_num == 2:
+                    # Recursively call to draw 7 squares inside this square
+                    inner_squares = self.draw_nested_squares(
+                        outer_square_num=current_inner_num,
+                        inner_square_count=7,
+                        x1=small_x,
+                        y1=small_y,
+                        x2=small_x + small_square_size,
+                        y2=small_y + small_square_size
+                    )
+                    all_numbers.extend(inner_squares)
+                
+                # Add the current square number to the list
+                all_numbers.append(current_inner_num)
                 
-                square_number += 1
-            if square_number > desired_number:
+            if current_inner_num > outer_square_num + inner_square_count:
                 break  # Break outer loop as well if we've reached the desired number
         
+        return all_numbers
+    
     def run(self):
         self.root.mainloop()