In this Python Turtle tutorial, we will learn How to create triangles in Python Turtle and we will also cover different examples related to the Turtle triangle. And, we will cover these topics.
- Python turtle triangle
- Python turtle triangle Spiral code
- Python turtle Sierpinski triangle
- Python turtle Nested triangle
Python turtle triangle
In this section, we will learn how to draw a triangle in a Python turtle.
A triangle has three edges and three vertices. It is a closed, two-dimensional shape.
Code:
In the following code, we import the turtle module. This turtle() method is generally used to make objects.
- tur.forward(100) is used to move the turtle in the forwarding direction.
- tur.left(120) is used to move the turtle in the left direction after moving to forward.
from turtle import *
import turtle
tur = turtle.Turtle()
tur.forward(100)
tur.left(120)
tur.forward(100)
tur.left(120)
tur.forward(100)
turtle.done()
Output:
After running the above code, we get the following output in which we can see a triangle is drawn with the help of a turtle.
Read: Replit Python Turtle
Python turtle triangle Spiral code
In this section, we will learn about how to draw triangle spiral code in Python turtle.
A Spiral is defined as a long curved line that moves round and round from a central point. Similarly triangle spiral is a long curved line that moves around and round away from its central point and a spiral triangle is formed.
Code:
In the following code, we import the turtle module from turtle import *, import turtle. This turtle() method is mainly used to make objects.
tur.right(120) is used to change the direction of a pen by 120 degrees clockwise.
from turtle import *
import turtle
n = 8
tur = turtle.Turtle()
for i in range(n * 4):
tur.forward(i * 8)
tur.right(120)
turtle.done()
Output:
After running the code, we get the following output in which we can see a spiral triangle is drawn on the screen.
Read: Python Turtle Size
Python turtle Sierpinski triangle
In this section, we will learn about how to draw turtle Sierpinski triangle in Python turtle.
The Sierpinski is defined as subdividing shapes into smaller copies. Sierpinski triangle is a is drawn with a three-way recursive algorithm. We can draw the Sierpinski triangle simply by hand.
Code:
In the following code, we will import the turtle module for drawing a Sierpinski triangle. Sierpinski creates a beautiful pattern inside the triangle.
- turtle.Screen() is used to create a screen.
- Sierpinski(mypoints,3,tur) is used to draw some points to create a pattern.
- turtle.goto(points[0][0],points[0][1]) is used to move the turtle to an absolute position.
- turtle.begin_fill() is used just call before drawing a shape to be filled.
- turtle.end_fill() is used just call after drawing a shape to be filled.
from turtle import *
import turtle
def drawTriangle(points,color,turtle):
turtle.fillcolor(color)
turtle.up()
turtle.goto(points[0][0],points[0][1])
turtle.down()
turtle.begin_fill()
turtle.goto(points[1][0],points[1][1])
turtle.goto(points[2][0],points[2][1])
turtle.goto(points[0][0],points[0][1])
turtle.end_fill()
def getmid(p1,p2):
return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2)
def Sierpinski(points,degree,myTurtle):
colormap = ['blue','cyan','yellow','white','green',
'purple','yellow']
drawTriangle(points,colormap[degree],myTurtle)
if degree > 0:
Sierpinski([points[0],
getmid(points[0], points[1]),
getmid(points[0], points[2])],
degree-1, myTurtle)
Sierpinski([points[1],
getmid(points[0], points[1]),
getmid(points[1], points[2])],
degree-1, myTurtle)
Sierpinski([points[2],
getmid(points[2], points[1]),
getmid(points[0], points[2])],
degree-1, myTurtle)
def mainwin():
tur = turtle.Turtle()
ws = turtle.Screen()
mypoints = [[-100,-50],[0,100],[100,-50]]
Sierpinski(mypoints,3,tur)
ws.exitonclick()
mainwin()
Output:
After running the above code we get the following output in which we see a beautiful Sierpinski triangle is drawn on the screen.
Read: Python Turtle Font
Python turtle Nested triangle
In this section, we will about how to draw a turtle nested triangle inPython turtle.
Before moving forward we should have a piece of knowledge about nested. Nested is an ordered collection of sets and each set contained the preceding set.
A nested triangle is defined as there is single triangle it contained a number of triangles that are generated by a nested loop.
Code:
In the following code, we import the turtle module from turtle import *, import turtle for drawing a nested triangle.
- right(90) is used to move the turtle in the right direction.
- After the move right forward(8 + shape) function is used for moving the turtle in the forward direction.
- left(120) is used to move the turtle in left direction.
from turtle import *
import turtle
numberoftriangle = 6
for shape in range(1, numberoftriangle + 1):
for sides in range(1, 5):
forward(10 + shape * 10 )
left(120)
right(90)
forward(8 + shape)
turtle.done()
Output:
After running the above code we get the following output in which we see a nested triangle is drawn on the screen.
You may also like to read the following tutorials.
- Python Turtle Square
- Python Turtle Tracer
- Python Turtle Art
- Python Turtle Circle
- Python Turtle Speed
- Python Turtle Write Function
- Python Turtle Race
- How to Draw Flower in Python Turtle
So, in this tutorial, we discussed the Python turtle triangle and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle triangle
- Python turtle triangle Spiral code
- Python turtle Sierpinski triangle
- Python turtle Nested triangle
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.