如題,我寫的四叉樹做碰撞檢測的類,里面自帶一個二維Nodes串列,在呼叫類里面的Insert函式時會出現無法識別self.Nodes是一個串列的問題,導致self.Nodes[].append()方法失效,求助大佬應該怎么解決??這里貼上源代碼和報錯提示:
class Quadtree(object):
MaxObjects=10
MaxLevels=5
Level=0
Objects=[]
Bounds=[]
Nodes=[[],[],[],[]]
VerticalMidpoint=0
HorizontalMidpoint=0
def __init__(self,level,bound):
self.Level=level
self.Bounds=bound
def Clear(self):
self.Objects.clear()
n=len(self.Nodes)
for i in range(0,n):
self.Nodes[i]=[]
def Split(self):
SubWidth=self.Bounds.GetWidth()/2
SubHeight=self.Bounds.GetHeight()/2
x=self.Bounds.GetX()
y=self.Bounds.GetY()
self.Nodes[2]=Quadtree(self.Level+1,Rectangle([x+SubWidth,y+SubHeight],SubWidth,SubHeight))
self.Nodes[3]=Quadtree(self.Level+1,Rectangle([x,y+SubHeight],SubWidth,SubHeight))
self.Nodes[0]=Quadtree(self.Level+1,Rectangle([x,y],SubWidth,SubHeight))
self.Nodes[1]=Quadtree(self.Level+1,Rectangle([x+SubWidth,y],SubWidth,SubHeight))
def GetIndex(self,particle):
index=-1
a=0
self.VerticalMidpoint=self.Bounds.GetX()+self.Bounds.GetWidth()/2
self.HorizontalMidpoint=self.Bounds.GetY()+self.Bounds.GetHeight()/2
if particle.Location[1]<self.HorizontalMidpoint and particle.Location[1]+particle.R<self.HorizontalMidpoint:
a='TopQuadrant'
else:
a='BottomQuadrant'
if particle.Location[0]<self.VerticalMidpoint and particle.Location[0]+particle.R<self.VerticalMidpoint:
if a=='TopQuadrant':
index=3
elif a=='BottomQuadrant':
index=0
elif particle.Location[0]>self.VerticalMidpoint:
if a== 'TopQuadrant':
index=2
elif a== 'BottomQuadrant':
index=1
return index
def Insert(self,particle):
if self.Nodes[0]!=[]:
index=self.GetIndex(particle)
if index !=-1:
self.Nodes[index].append(particle)
self.Objects.append(particle)
if len(self.Objects)>self.MaxObjects and self.Level<self.MaxLevels:
if self.Nodes[0]==[]:
self.Split()
i=0
while i<len(self.Objects):
index=self.Objects[i].GetIndex(self)
if index !=-1:
self.Nodes[index].append(self.Objects[i])
del self.Objects[i]
else:
i+=1
def Retrieve(self,returnObjects,particle):
index=particle.GetIndex(self)
if index!=-1 and self.Nodes[0]!=[]:
for n in self.Nodes[index]:
returnObjects.append(n)
return returnObjects
這是具體的報錯代碼

求大佬指教,跪謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/7038.html
上一篇:Fortran轉c
