Turtle Graphics:循环绘制递增尺寸且双边相连的重复正方形
Hey there! Let's figure out how to draw those connected, increasing-size squares you want. I'll use Python's turtle module since it's super straightforward for this kind of geometric drawing—no fancy setup needed.
From what you described, you want a set of squares where each one is bigger than the last, and all share two connected edges. I'll cover two common interpretations that match your "two edges connected" requirement, so you can pick what aligns with your diagram.
方案1:共享公共顶点的嵌套正方形
This version has all squares starting from the same corner, so their two adjacent edges (like right and top) naturally connect as extensions of each other. Perfect for a nested, outward-growing effect.
import turtle # Get valid input from the user while True: try: square_count = int(input("Enter the number of squares to draw: ")) if square_count <= 0: print("Please enter a positive whole number!") continue break except ValueError: print("Oops, that's not an integer. Try again!") # Set up the turtle drawer = turtle.Turtle() drawer.speed(3) # Adjust speed: 1=slow, 10=fast, 0=instant start_point = (0, 0) drawer.penup() drawer.goto(start_point) drawer.pendown() # Configure square dimensions base_side = 40 # Size of the first square size_step = 20 # How much each square grows by # Loop to draw each square for i in range(square_count): current_side = base_side + (i * size_step) # Draw 4 sides of the square for _ in range(4): drawer.forward(current_side) drawer.left(90) # Return to the shared starting point for the next square drawer.penup() drawer.goto(start_point) drawer.pendown() # Keep the window open until user closes it turtle.done()
代码解释:
- Input Handling: The loop ensures the user enters a valid positive integer—no crashes from bad inputs!
- Turtle Setup: We start at the origin (0,0) as our shared vertex, so every square's right and top edges start here, creating the "two edges connected" effect.
- Drawing Loop: For each square, we calculate its size (base size plus increment), draw four sides (each followed by a 90-degree turn), then jump back to the shared vertex to draw the next bigger square.
方案2:依次衔接的楼梯式正方形
If your diagram shows squares linking corner-to-corner (each square's bottom-left corner connects to the previous square's top-right), this version creates a staircase effect where each square connects to the last via two edges indirectly.
import turtle # Get user input while True: try: square_count = int(input("Enter the number of squares to draw: ")) if square_count <= 0: print("Positive numbers only, please!") continue break except ValueError: print("That's not an integer. Let's try again.") # Set up turtle drawer = turtle.Turtle() drawer.speed(2) # Starting position and initial size current_x, current_y = 0, 0 base_side = 30 size_step = 20 drawer.penup() drawer.goto(current_x, current_y) drawer.pendown() # Draw squares in sequence for _ in range(square_count): # Draw one square for _ in range(4): drawer.forward(base_side) drawer.left(90) # Move to the top-right corner of the current square for the next one drawer.penup() current_x += base_side current_y += base_side drawer.goto(current_x, current_y) drawer.pendown() # Increase the size for the next square base_side += size_step turtle.done()
代码解释:
- Sequential Placement: After drawing each square, we move to its top-right corner to start the next one. This creates a staircase where each square connects to the last via two edges (the previous square's right/top edges and the new square's left/bottom edges).
- Size Growth: Each square gets bigger by
size_stepto meet your "incrementing size" requirement.
You can tweak base_side and size_step to adjust how big the squares start and how much they grow each time. The speed() function lets you make the drawing faster or slower too!
内容的提问来源于stack exchange,提问作者ChrisCrad

