Python Turtle Polygon

In this Python tutorial, we will learn how to draw a polygon in Python Turtle and we will also cover different examples related to Turtle Polygon. And, we will cover these topics.

  • Python turtle polygon
  • Python turtle fill polygon

Python turtle polygon

In this section, we will learn about how to draw a polygon with the help of a turtle in a python turtle.

Before moving forward we should have some piece of knowledge about polygon. A polygon is defined as a two-dimension geometrical figure. It has a finite number of sides that are connected to each other and make the shape of a polygon.

We called the triangle a polygon because it is a plane figure. Similarly, the circle also has a plane figure but we don’t call it polygon because it is curved in shape and does not have sides.

Code:

In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.

  • sides = 10 this is the input for the number of the sides of the polygon.
  • length = 100 the input for the length of the side of the polygon.
  • turt.forward(length) is used to move the turtle in the forward direction.
  • turt.right(360 / sides) is used to move the turtle in the right direction.

from turtle import * 
import turtle as turt

tur = turt.Turtle()
 
sides = 10

length = 100
  
  
for _ in range(sides):
    turt.forward(length)
    turt.right(360 / sides)
turt.done()

Output:

After running the above code we get the following output in which we can see that a polygon is drawn on the screen with the help of a turtle.

Python turtle polygon
Python turtle polygon

Read Python Turtle Random

READ:  Python Scipy Chi-Square Test [7 Amazing Examples]

Python turtle fill polygon

In this section, we will learn about how to fill the color inside the polygon with the help of a turtle in a python turtle.

As we know polygon is a plane figure it has at least three straight and finite sides. The straight lines are connected to each other from end to end then the polygon is formed.

The line segment where the two-point connected is called the corner or also called edges of the polygon. From this, we can say that polygons have two-dimension(2d) shapes but not all the two-dimension shapes are called polygons.

As we learn above that polygon has two dimension shapes. The two-dimension shape is defined as which has three vertices and three edges. Triangle is a three-sided polygon it has three vertices and three edges.

We can draw different types of polygon with the help of a turtle after drawing that we can also fill color inside the polygon of our choice.

Code:

In the following code, we will import the turtle library from turtle import *, import turtle.

  • tur = turtle.Turtle() method is used to make objects.
  • tur.fillcolor(“cyan”) is used to fill the color inside the shape from which our shape looks beautiful and attractive.
  • tur.begin_fill() is used to start filling the color inside the shape.
  • tur.forward(100) is used to move the turtle in the forward direction.
  • tur.right(36) is used to move the turtle in the right direction.
  • tur.end_fill() is used end filling color.
from turtle import *
import turtle
 
tur = turtle.Turtle()
tur.fillcolor("cyan")
tur.begin_fill()
for _ in range(10):
  tur.forward(100)
  tur.right(36)
tur.end_fill() 
turtle.done()

Output:

READ:  PyTorch Load Model + Examples

After running the above code we get the following output in which we can see that a polygon is drawn with a beautiful color that looks attractive and attract the user’s eye.

Python turtle fill polygon
Python turtle fill polygon

Related Python turtle tutorials:

So, in this tutorial, we will discuss Python Turtle Polygon and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle polygon
  • Python turtle fill polygon