|
@@ -15,9 +15,65 @@ class SquareWindow:
|
|
|
self.canvas = tk.Canvas(self.root, width=self.window_size, height=self.window_size, bg="white")
|
|
self.canvas = tk.Canvas(self.root, width=self.window_size, height=self.window_size, bg="white")
|
|
|
self.canvas.pack(fill=tk.BOTH, expand=True)
|
|
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):
|
|
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
|
|
x2, y2 = margin + square_size, margin + square_size
|
|
|
|
|
|
|
|
# Draw the main square and label it
|
|
# 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:
|
|
else:
|
|
|
# This is a nested square, draw it with a different color
|
|
# 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
|
|
# Find the closest perfect square that is >= inner_square_count
|
|
|
n = math.ceil(math.sqrt(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_x = x1 + gap_x + j * (small_square_size + gap_x)
|
|
|
small_y = y1 + gap_y + i * (small_square_size + gap_y)
|
|
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 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:
|
|
if outer_square_num == 0 and current_inner_num == 2:
|
|
@@ -122,6 +163,88 @@ class SquareWindow:
|
|
|
|
|
|
|
|
return all_numbers
|
|
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):
|
|
def run(self):
|
|
|
self.root.mainloop()
|
|
self.root.mainloop()
|
|
|
|
|
|