
February 11th, 2012, 01:47 PM
|
 |
Contributing User
|
|
|
|
Code:
import sys
'''
sample data file contains the four lines:
60 20
20 60
-60 20
20 -60
'''
# First step: get a file
filename = (sys.argv+[''])[1]
if not filename:
raise ValueError('Use: '+sys.argv[0]+' data_file_name')
# Second step: read the file
with open(filename,'r') as inf:
lines = inf.readlines()
# Third step: convert the data to numbers.
points =[list(map(float,line.split())) for line in lines]
# step 4 is optional, step 5 required:
# these two steps are intertwined.
def adjust(data,desired_minimum,desired_maximum):
# improvement. implement a equation of line to reduce passes through data
n = min(data)
data = [datum-n for datum in data]
x = max(data)
span = desired_maximum - desired_minimum
m = span/x
return [desired_minimum+datum*m for datum in data]
(x,y,) = zip(*points) # required, step 5
x = adjust(x,50,250) # optional scale and translate x
y = adjust(y,50,250) # optional scale and translate y
# finish step 5. create_polygon requires a flat list.
points = []
for xy in zip(x,y):
points.extend(xy)
# Sixth step: Display and enjoy
import Tkinter # import Tkinter is slow. That's why I put it after possible error conditions.
canvas=Tkinter.Canvas()
canvas.create_polygon(*points)
canvas.pack()
canvas.mainloop()
Last edited by b49P23TIvg : February 11th, 2012 at 02:00 PM.
Reason: unclear---exactly what was optional?
|