Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to draw a polygon given a number of points with Turtle()?

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()?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading