Question: Python coding file with points: points 3 . txt has only two lines ( one for x - coordinates and one for y - coordinates
Python coding
file with points: pointstxt has only two lines one for xcoordinates and one for ycoordinates connect the dots given by this file. Read the x and y coordinates of points from the file pointstxt into the lists x and y Create a list of Point objects from those coordinates. you need to append them all using a forloop. Then, 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 by pixels and set its coordinates to go from to 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, please help me with what I am missing. No polygon is drawn but all my points print.
from graphics import
class Point:
def initself x y:
self.x x
self.y y
def createpointsx y:
return Pointxi yi for i in rangelenx
def readcoordinatesfilename:
try:
with openfilename, r as infile:
x floatxstr for xstr in infile.readlinestripsplit
y floatystr for ystr in infile.readlinestripsplit
return x y
except FileNotFoundError:
printfError: File filename not found."
return # Return empty lists to handle the error gracefully
def main:
filename "pointstxt
x y readcoordinatesfilename
if not x or not y:
return # Exit if there was an error reading the file
pointslist createpointsx y
polygon Polygonpointslist
polygon.setFilllightblue
win GraphWinConnecting Dots r
win.setCoords
win.setBackgroundlightyellow
polygon.drawwin
win.getMouse
win.close
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
