我正在使用jarvis march
制作凸包程序。
我的问题是我有一个使用右手法则的公式,因此我需要4个变量(起点,下一点,第三x,第三y)作为该公式。我打算迭代给定点列表中的值。因此,我拥有的每个while循环都是一种迭代值的方法。
问题在于,每当我创建一个用于迭代每个变量的新列表时,我都使用.remove()
,这将在运行它时导致范围索引丢失。我知道在循环中使用.remove()
不好,但是我找不到解决此问题的方法。
我已经看到了很多凸包问题,但是自从我仍然在学习以来,我想坚持我的想法。
import math
a = [[1,2], [5,8], [-9,2], [0,-9], [4,0], [0,-4], [-1,-1]]
def orient(xa, ya, xb, yb, xc, yc):
u=(yb-ya)*(xc-xb)-(yc-yb)*(xb-xa)
if (u<0):
return (1)
if (u==0):
return (0)
if (u>0):
return (-1)
l2=1
l3=1
l4=1
i=0
k=0
b=-1
ppoint=[]
startpoint=[-9,2]
while l2>0:
a1=a
a1.remove(startpoint)
q=a1
while l3>0:
t=q
nextpoint=t[k]
t.remove(nextpoint)
n=t
while l4>0:
thirdx=n[i][0]
thirdy=n[i][1]
if orient(startpoint[0],startpoint[1],nextpoint[0],nextpoint[1],thirdx,thirdy)>0:
i+=1
l4+=1
if i==len(n):
ppoint.append(nextpoint)
print (ppoint)
k+=1
i=0
break
elif orient(startpoint[0],startpoint[1],nextpoint[0],nextpoint[1],thirdx,thirdy)<0:
k+=1
i=0
break
if k==len(q):
startpoint=a[b]
b+=1
break
if b==len(a):
break
print (ppoint)