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:
- f = open('polylinesHw4.txt')
-
plineList = []
-
-
for line in f:
-
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.
- class Pt(object):
-
def __init__(self, x=0.0, y=0.0, z=0.0):
-
self.x = x
-
self.y = y
-
self.z = z
-
-
def __repr__(self):
-
return 'Pt(%f, %f, %f)' % (self.x, self.y, self.z)
-
-
def __add__(self, other):
-
return Pt(self.x+other.x, self.y+other.y, self.z+other.z)
-
-
def __sub__(self, other):
-
return Pt(self.x-other.x, self.y-other.y, self.z-other.z)
-
-
def dist(self, other):
-
p = self-other
-
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:
- sum([ptList[i].dist(ptList[i+1]) for i in range(len(ptList)-1)])