Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 11 - Multidimensional Lists - Programming Exercises - Page 394: 11.38

Answer

code

Work Step by Step

# 11.38 (Turtle: draw a polygon/polyline) Write the following functions that draw a # polygon/polyline to connect all points in the list. Each element in the list is a list of # two coordinates. # # Draw a polyline to connect all the points in the list # def drawPolyline(points): # # Draw a polygon to connect all the points in the list and # # close the polygon by connecting the first point with the last point # def drawPolygon(points): # # Fill a polygon by connecting all the points in the list # def fillPolygon(points): import turtle # Draw a polyline to connect all the points in the list def drawPolyline(points): for i in range(len(points) - 1): drawLine(points[i], points[i + 1]) # Draw a polygon to connect all the points in the list and # close the polygon by connecting the first point with the last point def drawPolygon(points): drawPolyline(points) drawLine(points[len(points) - 1], points[0]) # Close the polygon # Fill a polygon by connecting all the points in the list def fillPolygon(points): turtle.begin_fill() drawPolygon(points) turtle.end_fill() # Draw a line from (x1, y1) to (x2, y2) def drawLine(x1, y1, x2, y2): turtle.penup() turtle.goto(x1, y1) turtle.pendown() turtle.goto(x2, y2) points = input("Enter points: ").split() points = [eval(p) for p in points] drawPolygon(points)
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.