Geometric Precision

Objective: Geometric Precision

Welcome to Geometric Precision: Looping a Hexagon!

Our objective is to create a Python script using Turtle graphics that draws a regular hexagon using a for loop. The script should correctly calculate and use the external turn angle (360 / number_of_sides) for a hexagon.

This micro-quest focuses on employing for loops to construct regular polygons by iteratively drawing sides and making calculated turns.

Check the Specification

Before we start coding, let's check the engineering specification by running the tests.

Open your terminal, navigate to the project directory, and run pytest.

This will show us the failing tests that define what we need to build.

Test Results:

  • test_draw_hexagon_logic_and_loop_structure

Implement: main.py

Now, let's implement the solution in main.py by following the TODO comments.

Remember, the goal is to draw a hexagon using a for loop and the correct external angle calculation.

Step by step checklist:

  1. Define a variable for the number of sides a hexagon has (6).
  2. Define a variable for the length of each side (the tests expect this to be 100 units).
  3. Calculate the external angle needed for the turtle to turn at each vertex (360 degrees divided by the number of sides).
  4. Use a loop that repeats for each side of the hexagon.
  5. Inside the loop, move the 'pen' forward by the defined side length.
  6. Inside the loop, turn the 'pen' left by the calculated external angle.

The following documentation sections are going to be helpful:

  • The for Loop with range()
  • Loops in Turtle Graphics
  • Basic Turtle Setup
  • Basic Movement and Drawing Commands
  • Repeating Drawing Actions
  • Drawing Regular Polygons

Validate the Solution

With the code implemented, let's run pytest again to validate our work.

Open your terminal, navigate to the project directory, and run pytest.

If all tests pass, you've successfully drawn a hexagon using a loop and calculated angle!

Test Results:

  • test_draw_hexagon_logic_and_loop_structure All tests passed!

Documentation

Repeating with Loops: The Power of Patterns

Loops are fundamental programming constructs that allow a block of code to be executed repeatedly. This is essential for automating tasks, processing sequences of data, and creating repetitive patterns in graphics.

The for Loop with range()

The for loop in Python is used to iterate over a sequence (like a list, tuple, string, or range). The range() function is commonly used with for loops to repeat an action a specific number of times.

  • Basic Syntax:

    for variable in sequence:
        # Code block to be repeated
        # This code is indented
    # Code outside the loop (not repeated)
    
  • Using range(stop): range(stop) generates a sequence of numbers starting from 0, up to (but not including) the stop number.

    # This loop will run 5 times, with 'i' taking values 0, 1, 2, 3, 4
    for i in range(5):
        print(i)
    

    Console output showing numbers 0, 1, 2, 3, 4 on separate lines

  • Using range(start, stop): range(start, stop) generates a sequence of numbers starting from start, up to (but not including) the stop number.

    # This loop will run 3 times, with 'j' taking values 2, 3, 4
    for j in range(2, 5):
        print(j)
    
  • Repeating an Action a Fixed Number of Times: If the loop variable itself is not needed within the loop, it's common practice to use an underscore _ as the variable name.

    # This loop will run 3 times, printing the message each time
    for _ in range(3):
        print("Hello!")
    

    Console output showing "Hello!" printed 3 times

Loops in Turtle Graphics

Loops are particularly effective with Turtle graphics for drawing repetitive shapes and patterns efficiently. Instead of writing the same drawing commands multiple times, a loop can execute them repeatedly.

  • Basic Turtle Setup: To use Turtle graphics, you need to import the module and create a Turtle object.

    import turtle
    
    # Create a turtle object (often called 'pen' or 'artist')
    my_turtle = turtle.Turtle()
    
    # Optional: Adjust speed (0 is fastest, 1-10 are slow to fast)
    # my_turtle.speed(0)
    
    # Optional: Hide the turtle icon
    # my_turtle.hideturtle()
    
    # Optional: Keep the window open (usually at the end of the script)
    # turtle.done()
    

    Basic turtle setup code

  • Basic Movement and Drawing Commands:

    • forward(distance): Moves the turtle forward by distance.
    • backward(distance): Moves the turtle backward by distance.
    • left(angle): Turns the turtle left by angle degrees.
    • right(angle): Turns the turtle right by angle degrees.
    • penup(): Lifts the pen up, so movement does not draw.
    • pendown(): Puts the pen down, so movement draws.
    • goto(x, y): Moves the turtle to the absolute position (x, y).
    • circle(radius): Draws a circle with the given radius.
  • Repeating Drawing Actions: A loop can repeat the commands needed to draw a simple shape.

    import turtle
    
    pen = turtle.Turtle()
    
    # Draw a square using a loop
    side_length = 100
    for _ in range(4): # A square has 4 sides
        pen.forward(side_length)
        pen.left(90) # Turn 90 degrees for a square
    
    # turtle.done()
    

    Turtle drawing a single square

  • Drawing Multiple Identical Shapes: A for loop can repeat the drawing commands for a single shape multiple times, with movement commands in between to position each new shape.

    import turtle
    
    pen = turtle.Turtle()
    pen.speed(0) # Fastest speed
    
    shape_size = 40
    spacing = 10
    
    # Draw two triangles side-by-side
    for _ in range(2): # Repeat 2 times for 2 triangles
        # Draw one triangle (3 sides, 120 degree turns)
        for _ in range(3):
            pen.forward(shape_size)
            pen.left(120)
        
        # Move to the next position without drawing
        pen.penup()
        pen.forward(shape_size + spacing) # Move horizontally
        pen.pendown()
    
    # turtle.done()
    

    Turtle drawing two triangles side-by-side

  • Drawing Regular Polygons: Loops simplify drawing regular polygons by repeating the actions of drawing a side and turning. The external angle for any regular polygon is 360 / number_of_sides.

    import turtle
    
    pen = turtle.Turtle()
    
    num_sides = 5 # For a pentagon
    side_length = 80
    angle = 360 / num_sides # Calculate the external angle
    
    # Draw the polygon
    for _ in range(num_sides):
        pen.forward(side_length)
        pen.left(angle) # Turn by the calculated external angle
    
    # turtle.done()
    

    Turtle drawing a pentagon

  • Drawing Shapes with Changing Properties: The loop variable can be used within the loop to dynamically alter properties of the shapes being drawn, such as size or position.

    import turtle
    
    pen = turtle.Turtle()
    pen.speed(0)
    pen.hideturtle()
    
    # Draw circles with increasing radii
    base_radius = 10
    num_circles = 4
    
    for i in range(num_circles):
        current_radius = base_radius * (i + 1) # Radius increases: 10, 20, 30, 40
    
        # Position for concentric circles (move to bottom edge)
        pen.penup()
        pen.goto(0, -current_radius)
        pen.pendown()
    
        pen.circle(current_radius)
    
    # turtle.done()
    

    Turtle drawing concentric circles

  • Creating Growing Patterns (Spirals): By systematically modifying a movement or drawing parameter (like line length) in each iteration, growing patterns like spirals can be created.

    import turtle
    
    pen = turtle.Turtle()
    # pen.speed(0) # Optional: faster drawing
    
    initial_step = 5
    num_segments = 15
    turn_angle = 90 # For a square spiral
    
    for i in range(num_segments):
        # Calculate distance: 5 * (0+1)=5, 5*(1+1)=10, ..., 5*(14+1)=75
        distance = initial_step * (i + 1)
        
        pen.forward(distance)
        pen.right(turn_angle)
    
    # turtle.done()
    

    Turtle drawing a square spiral

  • Drawing Complex Shapes (Stars): Leveraging for loops with specific angle calculations allows for drawing complex, non-convex geometric shapes like stars through repeated line-and-turn sequences. For a 5-pointed star, the external turn angle is 144 degrees.

    import turtle
    
    pen = turtle.Turtle()
    
    num_points = 5
    side_length = 100
    star_angle = 144 # Specific angle for a 5-pointed star
    
    # Draw the star
    for _ in range(num_points):
        pen.forward(side_length)
        pen.right(star_angle)
    
    # turtle.done()
    

    Turtle drawing a 5-pointed star

  • Nested Loops: One loop can be placed inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop. This is useful for drawing patterns like grids.

    # Example of nested loops printing coordinates
    for row in range(2): # Outer loop runs 2 times (row 0, row 1)
        for col in range(3): # Inner loop runs 3 times for *each* row iteration
            print(f"Row {row}, Col {col}")
    # Output:
    # Row 0, Col 0
    # Row 0, Col 1
    # Row 0, Col 2
    # Row 1, Col 0
    # Row 1, Col 1
    # Row 1, Col 2
    

    In Turtle graphics, nested loops can be used to repeat a shape-drawing process (inner loop) multiple times while moving the turtle between shapes (outer loop).

    # Sketch: Drawing a row of shapes using nested loops
    # Outer loop: Repeat for each shape in the row
    #   Inner loop: Draw one complete shape (e.g., a square)
    #   Move turtle to the start position of the next shape
    

    Turtle drawing a pattern using nested loops, e.g., a grid of squares or circles