礼品包装算法索引超出索引>979的范围

2024-06-02 19:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

如上所述,我编写了一个礼品包装算法:

def giftwrap(listPts):
    """Returns the convex hull vertices computed using the
          giftwrap algorithm as a list of 'h' tuples
          [(u0,v0), (u1,v1), ...]    
    """
    y = 99**99
    #FINDING MIN Y-VAL POINT:
    for i in range(0,len(listPts)):
        if listPts[i][1] < y:
            y = listPts[i][1]
            lowPt = listPts[i]
            k = i
    listPts.append(lowPt) #pts[n] = Pk
    n = len(listPts) -1
    i = 0
    v = 0
    solution = []
    while k != n:
        listPts[i],listPts[k] = listPts[k],listPts[i]
        minAngle = 361
        solution.append(listPts[i])
        for j in range(i+1,n+1):
            angle = theta(listPts[i],listPts[j])
            if(angle < minAngle and angle > v and listPts[j] != listPts[i]):
                minAngle = angle
                k = j
        i = i+1 
        v = minAngle
    return solution

对于小于980的点列表,它可以正常工作。在这个数字上,我得到了一个错误:

^{pr2}$

我引发了一个索引器,发现I值要到981(比列表的长度高1),为什么它会出现在这个整数和比这个高的整数处?在

注意:我尝试在第54行放入以下if循环:

if i != n:
            i = i+1 
            v = minAngle
        else:
            break

但是,这会将解决方案作为所有顶点的列表返回。 谢谢


Tags: andthein列表forlenifrange