How to draw a triangle in python with turtle

Python has a simple pen drawing library called turtle. Using simple movement commands, we can draw shapes using the python turtle library. When teaching python to children, turtle is a good library to introduce to get children excited about the language and its features.

The basic actions used in the following examples are,

  • Draw a line with pen - forward[] command
  • Move without drawing - penup[], pendown[] commands
  • Turn the pen to an angle - left[], right[] commands

The following python program draws a simple equilateral triangle,

import turtle

board = turtle.Turtle[]

board.forward[100] # draw base

board.left[120]
board.forward[100]

board.left[120]
board.forward[100]

turtle.done[]

The following python program draws a right angled triangle,

import turtle

board = turtle.Turtle[]

board.forward[100] # draw base

board.left[90]
board.forward[100]

board.left[135]
board.forward[142]

turtle.done[]

The following python program draws a star shape by drawing two identical isosceles triangles,

import turtle

board = turtle.Turtle[]

# first triangle for star
board.forward[100] # draw base

board.left[120]
board.forward[100]

board.left[120]
board.forward[100]

board.penup[]
board.right[150]
board.forward[50]

# second triangle for star
board.pendown[]
board.right[90]
board.forward[100]

board.right[120]
board.forward[100]

board.right[120]
board.forward[100]

turtle.done[]

Drawing with Turtle in Python is really fun. In the past handful of tutorials, we learned how to import the Turtle module for use in our programs saw how to make the turtle [pen] move on the canvas, made the turtle change directions on the canvas, saw how to use loops in turtle, and made drawings of shapes using variables. Now we will take a look at drawing another type of Polygon, the triangle using the Turtle library in Python.

Define A Triangle Function

To draw a triangle, we want to use a function, and it should make use of variables. We do this so that we can call the function many times if we like to draw many triangles of different sizes.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


draw_triangle[]

done[]

Nice! The triangle function works. Notice that the loop uses 3 iterations shown by range[3] whereas when we drew a square it used 4 iterations in the loop. Another thing we notice is that we are passing in 120 as the degrees to turn for the left[] function. Why is that? This is because when drawing a triangle, you need to use the outside angle rather than the inside angle. What we are drawing here is an equilateral triangle since all three angles of an equilateral triangle add up to 180 degrees.

Drawing More Triangles

Now we can use the draw_triangle[] function a couple of times in combination with moving the turtle to a different spot on the canvas for a nice effect.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


draw_triangle[]
right[180]
forward[100]
right[180]
draw_triangle[200]

done[]

The following iteration takes that idea a step further to draw three triangles in different spots on the canvas.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


draw_triangle[]
right[180]
forward[100]
right[180]
draw_triangle[200]
right[180]
forward[100]
right[180]
draw_triangle[250]

done[]

Drawing Triangles In A Loop

Calling the draw_triangle[] inside of a loop makes for some really cool effects.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


for i in range[20]:
    draw_triangle[]
    right[1]

done[]

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


for i in range[40]:
    draw_triangle[]
    right[10]

done[]

How do you draw a turtle Python triangle?

How to Draw a Triangle in Python Turtle.
Draw a line with pen - forward[] command..
Move without drawing - penup[], pendown[] commands..
Turn the pen to an angle - left[], right[] commands..

What shapes can turtle draw in Python?

Changing the Turtle Shape.
Square..
Arrow..
Circle..
Turtle..
Triangle..
Classic..

How do you make a spiral triangle turtle in Python?

To draw something on the screen[cardboard] just move the turtle[pen]..
Import turtle and create a turtle instance..
Using for loop[i = 0 to i< n * 3] and repeat below step. turtle. forward[i * 10]. turtle. right[120]..
Close the turtle instance..

Chủ Đề