Connecting Tech Pros Worldwide Help | Site Map

Trouble

Newbie
 
Join Date: Sep 2009
Posts: 2
#1: Sep 27 '09
Hello All

I am trying to figure out python but I am having some problems. I could use some help with these questions. Attached is the text file.

1. Write a program to parse the text file and use list to hold the 2 polylines refer to Python Library Reference (from pythong help document) 4.1.4: split(), strip() and 2.1 float( [x])

2. Generate 2 ployline objects

3. Calculate the length of the 2 polylines

Thanks in advance.
Attached Files
File Type: txt polylinesHw4.txt (715 Bytes, 11 views)
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#2: Sep 28 '09

re: Trouble


dharris77,

Please show us some effort to solve this problem yourself. We cannot do your homework for you. Are you using a graphics package to generate the polylines?

Here's how you could start:
Expand|Select|Wrap|Line Numbers
  1. f = open('polylinesHw4.txt')
  2. plineList = []
  3.  
  4. for line in f:
  5.     if line[0].isdigit():
The line if line[0].isdigit(): is there to skip any line that does not begin with a number. In your case it will skip the first line "Polyline;\n"

This is probably beyond the scope of your assignment, but it would be ideal to parse the file into a list of point objects. To do this, we can define a class named Pt.
Expand|Select|Wrap|Line Numbers
  1. class Pt(object):
  2.     def __init__(self, x=0.0, y=0.0, z=0.0):
  3.         self.x = x
  4.         self.y = y
  5.         self.z = z
  6.  
  7.     def __repr__(self):
  8.         return 'Pt(%f, %f, %f)' % (self.x, self.y, self.z)
  9.  
  10.     def __add__(self, other):
  11.         return Pt(self.x+other.x, self.y+other.y, self.z+other.z)
  12.  
  13.     def __sub__(self, other):
  14.         return Pt(self.x-other.x, self.y-other.y, self.z-other.z)
  15.  
  16.     def dist(self, other):
  17.         p = self-other
  18.         return (p.x**2 + p.y**2 + p.z**2)**0.5
Then the length of polygon whose points are defined in by ptList can be calculated:
Expand|Select|Wrap|Line Numbers
  1. sum([ptList[i].dist(ptList[i+1]) for i in range(len(ptList)-1)])
Newbie
 
Join Date: Sep 2009
Posts: 2
#3: Sep 28 '09

re: Trouble


Here's what I have so far, I am not for sure if I am on the right track
Expand|Select|Wrap|Line Numbers
  1. f=open('C:/Classes/GEOG650/howework4/polylinesHw4.txt','r')
  2.  
  3. import string
  4. f.seek(14)
  5. line1=f.readline()
  6.  
  7. line1a=line1.split(';')
  8. point1=line1a[0].split(',')
  9. point2=line1a[1].split(',')
  10. point3=line1a[2].split(',')
  11. point4=line1a[3].split(',')
  12. point5=line1a[4].split(',')
  13. x1=float(point1[0])
  14. y1=float(point1[1])
  15. x2=float(point2[0])
  16. y2=float(point2[1])
  17. x3=float(point3[0])
  18. y3=float(point3[1])
  19. x4=float(point4[0])
  20. y4=float(point4[1])
  21. x5=float(point5[0])
  22. y5=float(point5[1])
  23. line2=f.readline()
  24. line2=line2[3:-1]
  25. line2a=line2.split(';')
  26. p1=line2a[0].split(',')
  27. p2=lina2a[1].split
  28.  
  29. import math
  30. class Point:
  31.     def _init_(self):
  32.         print
  33. class Polyline:
  34.     def _init_(self):
  35.         print
  36.     def length(self):
  37.         i,length=1,0
  38.     while i<len(self.x):
  39.         length=length + math.sqrt((self.x[i]-self.x[i-1])**2+(self.y[i]-self.y[i-1])**2)
  40.         i=i+1
  41.     return length
  42.  
  43. polyline1 = Polyline()
  44. polyline1.x= [x1,x2]
  45. polyline1.y= [y1,y2]
  46. print polyline1.length()
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#4: Sep 28 '09

re: Trouble


Please use code tags when posting code.

Instead of assigning a multitude of variables, accumulate the point values in a list or dictionary.

A simplified example:

Expand|Select|Wrap|Line Numbers
  1. s = """45.5,50.5
  2. 25.4,99.8
  3. 16.3,77.5
  4. """
  5.  
  6. pointList = []
  7. for line in s.strip().split('\n'):
  8.     x,y = line.split(',')
  9.     pointList.append((float(x), float(y)))
Your indentation level is incorrect in the length method of the Polyline class.
Reply