How to Draw Rectangles and Squares Using Python Turtle
Python's turtle package can be used to draw various shapes on a canvas. Turtle allows the programmer to move a pen in a specified direction by specifying its heading and distance. Turtle has no built-in primitive for drawing a rectangle or square. However it is easy to create a function to draw a rectangle(or square) in turtle.
The following python program defines a custom function for drawing rectangles using turtle. Note that the centre of the turtle canvas is at 0,0. Hence if you draw just a dot at (0,0) it will be at the centre of the drawing canvas. The function draw_rectangle() requires the co-ordinates of the top left of the rectangle. You also need to specify width, height, thickness and color of the rectangle.
The following example draws a blue rectangle which is 200 pixels in width and 100 pixels in height. We have specified the top left of the rectangle as (-100,50) to position the rectangle at the centre of the canvas. The rectangle drawn has a thickness of 5 pixels.
import turtle turtle.setup( 500 , 500 ) board = turtle.Turtle() # draws a rectangle given top left position of a rectangle def draw_rectangle(board,x,y,width,height,size,color): board.pencolor(color) board.pensize(size) board.setheading( 0 ) board.up() board.goto(x,y) board.down() # draw top board.forward(width) # draw right board.right( 90 ) board.forward(height) # draw bottom board.right( 90 ) board.forward(width) # draw left board.right( 90 ) board.forward(height) board.end_fill() # in turtle, the centre of the canvas is 0,0 # hence we position the rectangle in the center # note that we need to pass the top left co-ordinates of rectangle # draws rectangle with 200 pixels in width and 100 pixels in height draw_rectangle(board, - 100 , 50 , 200 , 100 , 5 , "blue" ) turtle.done() |
The following the sample output from the program,
Following python program is a different version which also allows us to specify a fill color for the rectangle. The example is identical to the previous one except that we use a different color to fill the rectangle.
import turtle turtle.setup( 500 , 500 ) board = turtle.Turtle() # draws a rectangle given top left position of a rectangle def draw_filled_rectangle(board,x,y,width,height,size,color,fill): board.fillcolor(fill) board.pencolor(color) board.pensize(size) board.setheading( 0 ) board.begin_fill() board.up() board.goto(x,y) board.down() # draw top board.forward(width) # draw right board.right( 90 ) board.forward(height) # draw bottom board.right( 90 ) board.forward(width) # draw left board.right( 90 ) board.forward(height) board.end_fill() # in turtle, the centre of the canvas is 0,0 # hence we position the rectangle in the center # note that we need to pass the top left co-ordinates of rectangle # draws rectangle with 200 pixels in width and 100 pixels in height # also specifies the rectangle color and the fill color draw_filled_rectangle(board, - 100 , 50 , 200 , 100 , 5 , "blue" , "green" ) turtle.done() |
The following the sample output from the program showing a filled rectangle,
The following program uses looping to draw a two dimensional matrix of identical black squares. Note that we are using black as fill color and white as the stroke color for the rectangles.
import turtle turtle.setup( 500 , 500 ) board = turtle.Turtle() # draws a rectangle given top left position of a rectangle def draw_filled_rectangle(board,x,y,width,height,size,color,fill): board.fillcolor(fill) board.pencolor(color) board.pensize(size) board.setheading( 0 ) board.begin_fill() board.up() board.goto(x,y) board.down() # draw top board.forward(width) # draw right board.right( 90 ) board.forward(height) # draw bottom board.right( 90 ) board.forward(width) # draw left board.right( 90 ) board.forward(height) board.end_fill() # draw a 7x7 matrix of black squares for x in range ( 1 , 8 ): for y in range ( 1 , 8 ): draw_filled_rectangle(board, - 200 + x * 20 ,y * 20 , 20 , 20 , 2 , "white" , "black" ) turtle.done() |
The following the sample output of 7x7 matrix of black squares from the python turtle program,