Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tuple index out of range problem
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
102jon




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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.
102jon




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: