Basically I am creating a function to determine the perimeter of a polygon based on the vertices. The input parameter is an xyList that contains the coordinates, basically a nested list of coordinate pairs within another list, like so: [ [x1,y1], [x2, y2], [x3,y3], ...]
I am using the good ol' distance formula and summing each distance. My question is related to the syntax of accessing the individual x and y coordinates within the list. Here is the function I have written:
perim = float(0)
count = len(xyList)
b = 0; db = 1
while b < count:
tempDist = math.sqrt((float(xyList[b+1]) - float(xyList[0]))**2 + (float((xyList[b+1]) - float(xyList[0])**2)))
perim = perim + tempDist
b += db
Here is the error I am getting in the Interactive Window:
>>> Unhandled exception while debugging...
Traceback (most recent call last):
File "C:\GEOG560\hw3_perimRect_v2.py", line 24, in polyPerimeter
tempDist = math.sqrt((float(xyList[1]) - float(xyList[0]))**2 + (float((xyList[1]) - float(xyList[0])**2))) # distance formula
TypeError: 'int' object is unsubscriptable
I am pretty sure it is the syntax related to accessing the coordinates. I have tried to make sure that the coordinates are not still strings after being read in from the text file, but I have been using the syntax "print xyList[0]" (i.e. to print the first x coordinate) which is obviously wrong. I haven't been able to find this in the documentation and am still waiting to receive my "Learning Python" text in the mail. Any help is greatly appreciated!