The colors are maintained in a list. In this implementation, store just three co
ID: 3717690 • Letter: T
Question
The colors are maintained in a list. In this implementation, store just three colors: red, green, and blue. Cycle through these colors for the successive polygons. The first polygon is colored red, the second is colored green, the third is colored blue, the fourth is colored red, and so on.The program ends when the user presses the enter key without typing any other characters. The program asks for the coordinates as a sequence of integers. The interactions as follows. Enter all x and y coordinates separated by commas 0, 40, 50, 50, 80, 0 Enter all x and y coordinates separated by commas 100, 80, 120, 100, 140, 60, 100, 40 Enter all x and y coordinates separated by commas -30, -40, -70, -20, -100, -90, - 120, -40, -30, 0 Enter all x and y coordinates separated by commas -30, 40, -70, 20, -10, 0 Enter all x and y coordinates separated by commas Enter all x and y coordinates separated by commas 0, 40, 50, 50, 80, 0 Enter all x and y coordinates separated by commas 100, 80, 120, 100, 140, 60, 100, 40 Enter all x and y coordinates separated by commas -30, -40, -70, -20, -100, -90, - 120, -40, -30, 0 Enter all x and y coordinates separated by commas -30, 40, -70, 20, -10, 0 Enter all x and y coordinates separated by commas The program structure has three functions with parameters and functionalities as indicated.
1)get_corner(coordinates):The parameter coordinate is a list of an even number of integers.The function extracts from coordinates the first two integers and returns this pair as a list of two integers: the x and y coordinates of a corner of the polygon. The function also deletes from coordinates the first two entries, which were returned by it.
2)draw_polygon(corners,polygon_color):Draws a polygon whose corners are available as a list in the parameter corners. The polygon is filled with the color given in the second parameter color. The function calls get_corner() repeatedly to extract successive corners.
3)main():Create the list of colors . .It then prompts for and reads lines of integers until a line with no characters in it is read. You may assume that each non-empty line has an even number of integers with each pair separated by a comma. The function then creates a list out of these integers and calls draw_polygon().
Explanation / Answer
import turtle
counter=0
color = 0
def main():
polygon_color = ['red','green','blue']
cordlist=[]
while True: # infinite loop
coordinates = input("Enter all x and y coordinates separated by commas:")
if coordinates == "":
break # stops the loop
numbers = coordinates.split(',')
numbers = [ int(x) for x in numbers ]
cordlist.append(numbers)
for i in cordlist:
global counter
counter = 0
draw_polygon(i,polygon_color)
def get_corner(coordinates):
global counter
cord = []
cord.append(coordinates[counter])
cord.append(coordinates[counter+1])
counter = counter +1
return cord
def draw_polygon(corners, polygon_color):
global color
my = turtle.Turtle()
my.color(polygon_color[color])
my.begin_fill()
for i in range(len(corners)):
cord = get_corner(corners)
my.goto(cord[0],cord[1])
my.goto(corners[0],corners[1])
color = 0
main()