zhong (钟鹏群) 3 minggu lalu
induk
melakukan
31f78be947
3 mengubah file dengan 291 tambahan dan 26 penghapusan
  1. 0 0
      src/print.log
  2. 149 26
      src/yyy.py
  3. 142 0
      src/yyy2.py

File diff ditekan karena terlalu besar
+ 0 - 0
src/print.log


+ 149 - 26
src/yyy.py

@@ -15,9 +15,65 @@ 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 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}")
+        # Property to store square information: {number: (x, y, size, parent_number)}
+        self.square_positions = {}
+        
+        # Draw the main square (numbered as 0) and 79 inner squares
+        main_squares = self.draw_nested_squares(outer_square_num=0, inner_square_count=11)
+        
+        # Draw 7 smaller squares inside square #2
+        # Note: We need to calculate the coordinates for square #2
+        # This requires getting the position of square #2 from the main grid
+        square_size = int(self.window_size * 0.9)  # 90% of 1000 = 900
+        margin = (self.window_size - square_size) // 2  # (1000 - 900) / 2 = 50
+        
+        # Find the position of square #2 (which is the second square drawn)
+        n = math.ceil(math.sqrt(11))  # For 79 squares, we need ceil(sqrt(79)) = 9
+        small_square_size = (square_size // n) - 2  # Size of each small square minus padding
+        
+        # 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
+        
+        # Square #2 corresponds to index 1 (0-based), so it's at row = 1//9 = 0, col = 1%9 = 1
+        # Actually, square #2 is the second square, so it's at index 1 (0-indexed)
+        square_index = 1  # For square #2 (0-indexed)
+        row = square_index // n
+        col = square_index % n
+        
+        # Calculate position for square #2
+        small_x = margin + gap_per_row + col * (small_square_size + gap_per_row)
+        small_y = margin + gap_per_row + row * (small_square_size + gap_per_row)
+        
+        # Draw 7 smaller squares inside square #2
+        # The inner squares should continue numbering from after the last square
+        # Since we have 79 squares in the main grid (numbered 1 to 79), 
+        # the inner squares should be numbered 80 to 86
+        # We need to draw these inside square #2, so we pass square #2's coordinates
+        # But the numbering should start from 80 onwards
+        # Note: We exclude the outer square number (2) from the result since it's already counted
+        inner_squares = self.draw_nested_squares_for_inner(
+            outer_square_num=2,  # The number of the outer square we're drawing in
+            inner_square_start_num=80,  # Start numbering the inner squares from 80
+            inner_square_count=7,  # Number of inner squares to draw
+            x1=small_x,
+            y1=small_y,
+            x2=small_x + small_square_size,
+            y2=small_y + small_square_size
+        )
+        
+        # Combine all numbers, but remove the duplicate outer square number (2) from inner_squares
+        # since it's already included in main_squares
+        all_numbers = main_squares + [num for num in inner_squares if num != 2]
+        
+        # Print the square positions dictionary
+        output_content = f"All square numbers: {all_numbers}\nSquare positions: {self.square_positions}"
+        print(output_content)
+        
+        # Save the output to print.log file
+        with open("print.log", "w", encoding="utf-8") as f:
+            f.write(output_content)
     
     def draw_nested_squares(self, outer_square_num, inner_square_count, x1=None, y1=None, x2=None, y2=None, parent_square_info=None):
         """
@@ -41,20 +97,13 @@ class SquareWindow:
             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))
+            self.draw_square_at_location(x=x1, y=y1, size=square_size, 
+                                        number=outer_square_num, color="black", text_color="black", font_size=12, parent_number=None)
         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))
+            square_size = x2 - x1  # Calculate the size of the square
+            self.draw_square_at_location(x=x1, y=y1, size=square_size, 
+                                        number=outer_square_num, color="gray", text_color="black", font_size=10, parent_number=None)
 
         # Find the closest perfect square that is >= inner_square_count
         n = math.ceil(math.sqrt(inner_square_count))
@@ -89,17 +138,9 @@ class SquareWindow:
                 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(
-                    small_x, small_y, 
-                    small_x + small_square_size, small_y + small_square_size,
-                    outline="blue", width=1
-                )
-                
-                # 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(current_inner_num), fill="red", font=("Arial", 10))
+                # Draw the square
+                self.draw_square_at_location(x=small_x, y=small_y, size=small_square_size, 
+                                            number=current_inner_num, color="blue", text_color="red", font_size=10, parent_number=outer_square_num)
                 
                 # 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:
@@ -122,6 +163,88 @@ class SquareWindow:
         
         return all_numbers
     
+    def draw_square_at_location(self, x, y, size, number, color, text_color, font_size, parent_number=None):
+        """
+        Draw a square at a specific location with a number in the center.
+        
+        Args:
+            x: X coordinate of the top-left corner
+            y: Y coordinate of the top-left corner
+            size: Size of the square (width and height)
+            number: Number to display in the center
+            color: Color of the square border
+            text_color: Color of the number text
+            font_size: Font size of the number text
+            parent_number: The number of the parent square (None if no parent)
+        """
+        # Draw the square
+        self.canvas.create_rectangle(x, y, x + size, y + size, outline=color, width=1)
+        
+        # Add number in the center of the square
+        center_x = x + size // 2
+        center_y = y + size // 2
+        self.canvas.create_text(center_x, center_y, text=str(number), fill=text_color, font=("Arial", font_size))
+        
+        # Record the square's position information
+        self.square_positions[number] = (x, y, size, parent_number)
+    
+    def draw_nested_squares_for_inner(self, outer_square_num, inner_square_start_num, inner_square_count, x1, y1, x2, y2):
+        """
+        Draw nested squares inside a given square with specific numbering.
+        
+        Args:
+            outer_square_num: The number of the outer square (the container) - this is just for reference, not drawn again
+            inner_square_start_num: The starting number for inner squares
+            inner_square_count: How many inner squares to draw inside the outer square
+            x1, y1, x2, y2: Coordinates of the outer square
+        
+        Returns:
+            A list of all inner square numbers that were drawn (excluding the outer square)
+        """
+        # 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 {inner_square_count} squares, but we're drawing {actual_number} squares in a {n}x{n} grid inside square #{outer_square_num}.")
+        
+        # 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 the required number of squares (up to inner_square_count) in an n x n grid
+        all_numbers = []  # Don't include the outer square number since it's already counted
+        
+        # Calculate spacing to distribute squares evenly with gaps
+        total_used_space = n * small_square_size  # Total space occupied by squares
+        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
+                current_inner_num = inner_square_start_num + (i * n + j)
+                
+                if current_inner_num >= inner_square_start_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_x + j * (small_square_size + gap_x)
+                small_y = y1 + gap_y + i * (small_square_size + gap_y)
+                
+                # Draw the square
+                self.draw_square_at_location(x=small_x, y=small_y, size=small_square_size, 
+                                            number=current_inner_num, color="green", text_color="orange", font_size=8, parent_number=outer_square_num)
+                
+                # Add the current square number to the list
+                all_numbers.append(current_inner_num)
+                
+            if current_inner_num >= inner_square_start_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()
 

+ 142 - 0
src/yyy2.py

@@ -0,0 +1,142 @@
+import tkinter as tk
+
+class SquareWindow:
+    def __init__(self):
+        # Configuration: [(parent_number, count), ...] 
+        # (None, 11) means draw 11 squares inside square #0 (the main square)
+        # (2, 7) means draw 7 squares inside square #2
+        self.config = [(None, 3), (2, 187), (1, 66), (177, 7)]
+        
+        # Set window size to 1000x1000 pixels (needed for calculate_squares)
+        self.window_size = 1000
+        
+        # Calculate squares based on config
+        self.squares = self.calculate_squares()
+        
+        # Create the main window
+        self.root = tk.Tk()
+        self.root.title("Square Window")
+        
+        self.root.geometry(f"{self.window_size}x{self.window_size}")
+        
+        # Create a canvas to draw on
+        self.canvas = tk.Canvas(self.root, width=self.window_size, height=self.window_size, bg="white")
+        self.canvas.pack(fill=tk.BOTH, expand=True)
+        
+        # Property to store square information: {number: (x, y, size, parent_number)}
+        self.square_positions = {}
+        
+        # Draw all squares based on the calculated squares dictionary
+        for number, (x, y, size, parent_number) in self.squares.items():
+            self.draw_square_at_location(x, y, size, number, parent_number)
+        
+        # Print the square positions dictionary
+        output_content = f"Square positions: {self.square_positions}"
+        print(output_content)
+        
+        # Save the output to print.log file
+        with open("print.log", "w", encoding="utf-8") as f:
+            f.write(output_content)
+    
+    def calculate_squares(self):
+        """
+        Calculate all squares based on the config.
+        Config format: [(parent_number, count), ...]
+        (None, count) means draw 'count' squares inside the main square (number 0)
+        (number, count) means draw 'count' squares inside square with 'number'
+        """
+        import math
+        
+        # Initialize the main square (number 0)
+        main_square_size = int(self.window_size * 0.9)  # 90% of 1000 = 900
+        margin = (self.window_size - main_square_size) // 2  # (1000 - 900) / 2 = 50
+        
+        # Dictionary to store all squares: {number: (x, y, size, parent_number)}
+        squares = {}
+        
+        # Add the main square (number 0)
+        squares[0] = (margin, margin, main_square_size, None)
+        
+        # Track the next available number for new squares
+        next_number = 1
+        
+        # Process each configuration entry
+        for parent_number, count in self.config:
+            if parent_number is None:
+                # Draw 'count' squares inside the main square (number 0)
+                parent_num = 0
+            else:
+                parent_num = parent_number
+            
+            # Get the parent square's info
+            if parent_num in squares:
+                parent_x, parent_y, parent_size, _ = squares[parent_num]
+                
+                # Calculate grid size for the child squares
+                n = math.ceil(math.sqrt(count))  # Number of rows/columns needed
+                child_size = (parent_size // n) - 2  # Size of each child square minus padding
+                
+                # Calculate spacing to distribute squares evenly with gaps
+                total_used_space = n * child_size  # Total space occupied by squares
+                remaining_space = parent_size - total_used_space  # Remaining space for gaps
+                gap = remaining_space // (n + 1)  # Gap between squares and margins
+                
+                # Draw 'count' child squares inside the parent square
+                squares_drawn = 0
+                for i in range(n):  # Rows
+                    for j in range(n):  # Columns
+                        if squares_drawn >= count:
+                            break
+                        
+                        # Calculate position for each child square with proper spacing
+                        child_x = parent_x + gap + j * (child_size + gap)
+                        child_y = parent_y + gap + i * (child_size + gap)
+                        
+                        # Add the child square to the dictionary
+                        squares[next_number] = (child_x, child_y, child_size, parent_num)
+                        
+                        next_number += 1
+                        squares_drawn += 1
+                    if squares_drawn >= count:
+                        break
+        
+        return squares
+    
+    def draw_square_at_location(self, x, y, size, number, parent_number):
+        """
+        Draw a square at a specific location with a number in the center.
+        
+        Args:
+            x: X coordinate of the top-left corner
+            y: Y coordinate of the top-left corner
+            size: Size of the square (width and height)
+            number: Number to display in the center
+            parent_number: The number of the parent square (None if no parent)
+        """
+        # Determine color based on whether it has children (is a parent)
+        has_children = any(parent_num == number for _, _, _, parent_num in self.squares.values() if parent_num is not None)
+        if parent_number is None:
+            color = "black"  # Main outer square
+        elif has_children:
+            color = "gray"   # Parent square with children
+        else:
+            color = "blue"   # Child square without children
+        
+        # Draw the square
+        self.canvas.create_rectangle(x, y, x + size, y + size, outline=color, width=1)
+        
+        # Add number in the center of the square
+        center_x = x + size // 2
+        center_y = y + size // 2
+        text_color = "red" if has_children else "black"
+        self.canvas.create_text(center_x, center_y, text=str(number), fill=text_color, font=("Arial", 10))
+        
+        # Record the square's position information
+        self.square_positions[number] = (x, y, size, parent_number)
+
+    def run(self):
+        self.root.mainloop()
+
+if __name__ == "__main__":
+    app = SquareWindow()
+    app.run()

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini