How to draw a polygon given a number of points with Turtle()?
Question:
I have a list of points given by (x, y)
coordinates, and I want to connect them in a polygon shape. For example:
import turtle
tina = turtle.Turtle()
tina.shape('turtle')
points = [
(40, -80),
(30, -80),
(30, -70),
(40, -70)
]
Is there a function for drawing polygons like turtle.circle()?
Answers:
Try the following:
import turtle
tina=turtle.Turtle()
tina.shape("turtle")
points=[[10,-80],[200,-80], [100, 100]]
# Draw a polygon based on the points list in points and #show the result
def draw_polygon(points):
tina.penup()
tina.goto(points[0])
tina.pendown()
for point in points:
tina.goto(point)
tina.goto(points[0]) # Go back to the origin to close the polygon shape
draw_polygon(points)
turtle.done()
However, it draws only in the order the points are given – so that order needs to be correctly specified in order to get a polygon.
I have a list of points given by (x, y)
coordinates, and I want to connect them in a polygon shape. For example:
import turtle
tina = turtle.Turtle()
tina.shape('turtle')
points = [
(40, -80),
(30, -80),
(30, -70),
(40, -70)
]
Is there a function for drawing polygons like turtle.circle()?
Try the following:
import turtle
tina=turtle.Turtle()
tina.shape("turtle")
points=[[10,-80],[200,-80], [100, 100]]
# Draw a polygon based on the points list in points and #show the result
def draw_polygon(points):
tina.penup()
tina.goto(points[0])
tina.pendown()
for point in points:
tina.goto(point)
tina.goto(points[0]) # Go back to the origin to close the polygon shape
draw_polygon(points)
turtle.done()
However, it draws only in the order the points are given – so that order needs to be correctly specified in order to get a polygon.