Question
Write a Python program to draw a sequence of filled polygons of possibly different colors. The program reads in the corners (vertices) of the polygons
Write a Python program to draw a sequence of filled polygons of possibly different colors. The program reads in the corners (vertices) of the polygons as a sequence of integers separated by commas, from the keyboard. Each corner is specified by a pair of x and y coordinates. 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 interaction should be as below.
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 should be structured such that it has three functions with parameters and functionalities as indicated:
1) get_corner(coordinates): The parameter coordinates 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(): Creates 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().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started