Question: Python coding Consider now a much larger file with points: points 3 . txt . This file also has only two lines ( one for

Python coding
Consider now a much larger file with points: points3.txt. This file also has only two lines (one for x-coordinates and one for y-coordinates), but they are much longer now. connect the dots given by this file. Create a new Python file
Read the x and y coordinates of points from the file points3.txt into the lists x and y.
Create a list of Point objects from those coordinates. There are two many points (1095 to be exact) to type an append command for each. Instead, you need to append them all using a for-loop
Once you have p_list, you create a Polygon object giving this list of points as an input to the Polygon() function.
Create a Graph Window to draw a polygon in.
Make the window size 500 by 500 pixels and
set its coordinates to go from (0,0) to (800,800).
Make sure to add connecting the dots to the window title.
Set the window background to lightyellow.
Draw your polygon in the window. Set the polygons fill color to lightblue.
Finish with win.getMouse() and win.close(), so the window closes after the click.
my code so far
from graphics import *
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def create_points(x, y):
return [Point(x[i], y[i]) for i in range(len(x))]
def read_coordinates(file_name):
with open(file_name, 'r') as infile:
x =[float(x_str) for x_str in infile.readline().strip().split(',')]
y =[float(y_str) for y_str in infile.readline().strip().split(',')]
return x, y
def main():
file_name = "points3.txt"
x, y = read_coordinates(file_name)
points_list = create_points(x, y)
# Print all points
for point in points_list:
print(f'Point: ({point.x},{point.y})')
# Further processing can be done here (e.g., drawing points)
if __name__=="__main__":
win = GraphWin("Connecting Dots - Melissa Carpenter", 500,500)
win.setCoords(0,0,800,800)
win.setBackground("lightyellow")
win.getMouse()
win.close()
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!