Computer Science Canada

Tuple index out of range problem

Author:  102jon [ Thu Feb 03, 2011 10:34 am ]
Post subject:  Tuple index out of range problem

I'm writing a script that essentially splits a polygon (a list of coordinate pairs) into two for a given special case. For the first polygon I run a for loop as follows:
code:

for i in range(1, len(polygon)):             
            if (polygon[i][0] < 0) and (polygon[i+1][0] > 0):
                boundIntercept = polygon[i][1] - ((polygon[i][1] - polygon[i+1][1]) / (polygon[i][0] - polygon[i+1][0])) * polygon[i][0]
                newPolygon1[i] = (0, boundIntercept)               
            elif (polygon[i-1][0] > 0) and (polygon[i][0] < 0):
                boundIntercept = polygon[i][1] - ((polygon[i][1] - polygon[i-1][1]) / (polygon[i][0] - polygon[i-1][0])) * polygon[i][0]
                newPolygon1[i] = (0, boundIntercept)
            elif ((polygon[i][0] < 0) and (polygon[i-1][0] < 0) and (polygon[i+1][0] < 0)):
                newPolygon1[i] = None
        firstPolygon = [item for item in newPolygon1 if item != None]


No problem there. However, I do the exact same thing for the second polygon:

code:

for i in range(1, len(polygon)):
            if (polygon[i][0] > 0) and (polygon[i+1][0] < 0):
                boundIntercept = polygon[i][1] - ((polygon[i][1] - polygon[i+1][1]) / (polygon[i][0] - polygon[i+1][0])) * polygon[i][0]
                newPolygon2[i] = (0, boundIntercept)
            elif (polygon[i-1][0] < 0) and (polygon[i][0] > 0):
                boundIntercept = polygon[i][1] - ((polygon[i][1] - polygon[i-1][1]) / (polygon[i][0] - polygon[i-1][0])) * polygon[i][0]
                newPolygon2[i] = (0, boundIntercept)
            elif ((polygon[i][0] > 0) and (polygon[i-1][0] > 0) and (polygon[i+1][0] > 0)):
                newPolygon2[i] = None
        secondPolygon = [item for item in newPolygon2 if item != None]


And I get a "error: tuple index out of range". Any insight would be appreciated.

Author:  DemonWasp [ Thu Feb 03, 2011 12:52 pm ]
Post subject:  RE:Tuple index out of range problem

It would be easier for us to help if you also listed the initialization of the two polygons, and if your code were properly indented in your [ code ] elements.

Author:  102jon [ Fri Feb 04, 2011 4:08 pm ]
Post subject:  Re: Tuple index out of range problem

I figured it out, but thanks. The code was indented properly but something happened when copying and pasting it here.


: