请按照@Arun R在how to find area of rectangle which is covering another rectangle中告诉的算法检查这段代码,其中有什么错误
我的输出是
为什么它不删除其他文件中的矩形
cdef class Ray:
cdef public:
Point2D p
Point2D q
list points
cdef class Boundbox:
cdef public:
Point2D minP
Point2D maxP
int componentID
int getComponentID(self):
return self.componentID
def __numeric_compare_by_x(self,Boundbox a,Boundbox b):
cdef Point2D tempA,tempB
tempA=a.minP
tempB=b.minP
return tempA.x-tempB.x
def __numeric_compare_by_y(self,Boundbox a,Boundbox b):
cdef Point2D tempA,tempB
tempA=a.minP
tempB=b.minP
return tempA.y-tempB.y
def isBoundbox_inside(self,b1,b2):
if((b1.minP.x<=b2.minP.x and b1.minP.y<=b2.minP.y)and(b1.maxP.x>=b2.maxP.x and b1.maxP.y>=b2.maxP.y)):
return True
return False
def boundboxFilter(self,boundbox):
cdef:
int i
int minx,miny,maxx,maxy
list pointList=[]
Boundbox p,b1,b2
#for i in xrange(len(boundbox)):
# pointList.append(boundbox[i])
pointList=boundbox
pointList.sort(cmp=self.__numeric_compare_by_x)
stack=[]
stack.append(pointList[0])
for p in pointList[1:]:
top=len(stack)-1
b1=stack[top]
b2=p
if(not(self.isBoundbox_inside(b1,b2))):
stack.append(b2)
pointList=stack
pointList.sort(cmp=self.__numeric_compare_by_y)
stack=[]
stack.append(pointList[0])
for p in pointList[1:]:
top=len(stack)-1
b1=stack[top]
b2=p
if(not(self.isBoundbox_inside(b1,b2))):
stack.append(b2)
return stack
问题在于你的过滤逻辑。你知道吗
其中
is_inside
为真,如果rect
在box
内。您需要对照所有其他矩形检查每个矩形,如果它不在其中任何一个矩形内,则保存它。你当然可以调整它的性能。你知道吗另一种需要较少比较的方法是考虑整个列表,删除其他列表中的矩形。但是你必须非常小心索引,在列表中间弹出东西也很昂贵。你知道吗
相关问题 更多 >
编程相关推荐