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: pointstxt This file also has only two lines one for xcoordinates and one for ycoordinates 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 pointstxt into the lists x and y
Create a list of Point objects from those coordinates. There are two many points to be exact to type an append command for each. Instead, you need to append them all using a forloop
Once you have plist, 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 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
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:
with openfilename, r as infile:
x floatxstr for xstr in infile.readlinestripsplit
y floatystr for ystr in infile.readlinestripsplit
return x y
def main:
filename "pointstxt
x y readcoordinatesfilename
pointslist createpointsx y
# Print all points
for point in pointslist:
printfPoint: pointxpointy
# Further processing can be done here eg drawing points
if namemain:
win GraphWinConnecting Dots Melissa Carpenter",
win.setCoords
win.setBackgroundlightyellow
win.getMouse
win.close
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
