Circle of Squares using Python Turtle


The Circle of Squares is a fascinating geometric pattern that can be created using Python's turtle graphics library. This pattern consists of a circle of squares that are evenly spaced around its circumference, with each square rotated at an angle relative to the previous square. This creates a mesmerizing visual effect that can be customized to suit any color scheme or size.

In this tutorial, we will explore how to create the Circle of Squares pattern using Python's turtle library, step by step. We will also discuss different customization options that can be applied to create unique variations of the pattern.

Whether you're a beginner or an experienced Python programmer, this tutorial will provide you with a fun and creative way to learn about turtle graphics and geometric patterns.

Creating the Circle of Squares Pattern using Python Turtle

Here's an approach to create the Circle of Squares pattern using Python's turtle graphics library −

Import the turtle graphics library −

import turtle

Create a turtle object and set its speed to "fastest" to ensure that the pattern is drawn quickly −

t = turtle.Turtle()
t.speed('fastest')

Set the initial position and orientation of the turtle −

t.penup()
t.goto(0, 250)
t.pendown()

Set the variables for the radius of the circle and the angle between the squares −

radius = 45
angle = 10

Use a loop to draw the squares around the circle −

for i in range(36):
   # draw a square
   for j in range(4):
      t.forward(50)
      t.right(90)
        
   # move the turtle to the next square position
   t.penup()
   t.right(angle)
   t.forward(radius)
   t.pendown()

Once the loop is complete, hide the turtle and exit the window −

t.hideturtle()
turtle.done()

Circle of Squares: The Complete Code

Putting it all together, the complete code for creating the Circle of Squares pattern using Python's turtle graphics library looks like this −

Example

import turtle

t = turtle.Turtle()
t.speed('fastest')

t.penup()
t.goto(0, 250)
t.pendown()

radius = 45
angle = 10

for i in range(36):
   for j in range(4):
      t.forward(50)
      t.right(90)
   t.penup()
   t.right(angle)
   t.forward(radius)
   t.pendown()

t.hideturtle()
turtle.done()

How Does This Code Work?

The code uses the turtle graphics library to create a circle of squares pattern. The turtle starts at the centre of the circle and moves up by 250 pixels before drawing the squares.

  • Once the turtle is in position, it begins drawing the squares using a nested loop.

  • The outer loop iterates 36 times, which corresponds to the number of squares to be drawn.

  • The inner loop draws each side of the square by moving forward 50 pixels and turning right by 90 degrees.

  • After drawing the square, the turtle lifts the pen up using the penup() method and moves to the next position on the circle by turning right by the angle specified in the angle variable and moving forward by the radius specified in the radius variable. Then, the turtle puts the pen down using the pendown() method to start drawing the next square.

  • After drawing all the squares, the turtle hides using the hideturtle() method, and the window remains open until the user closes it using the mouse or keyboard.

To run the above code, you first need to install the turtle library using the following pip command −

pip install PythonTurtle

Output

Now let's run the code and check the actual output −

Conclusion

In conclusion, the Circle of Squares pattern is an interesting and fun project that can be created using the turtle graphics library in Python. By following the code provided, users can easily create a circular pattern of squares with evenly spaced angles and radii.

This project is a great introduction to Python graphics programming and can be used as a starting point for more complex graphic designs. With the flexibility of the turtle library, the possibilities for creating unique designs are endless, and users can easily modify the code to create their own custom patterns.

By experimenting with different angles, radii, and colours, users can create beautiful and complex designs that are limited only by their creativity.

Updated on: 28-Sep-2023

638 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements