【问题标题】:maximum recursion depth exceeded when inserting points into a quadtree using Python (dying looking for the bug)使用 Python 将点插入四叉树时超出最大递归深度(正在寻找错误)
【发布时间】:2014-12-26 22:11:57
【问题描述】:

我正在尝试构建一个点区域四叉树,它使用 Python 在 2D 地图上存储点,但是当我尝试插入两个彼此接近(不太接近)的点时,我遇到了错误:RuntimeError:在 cmp 中超出了最大递归深度。我试图将最大递归数提高到 10000,但不起作用。所以我猜我的代码有问题。有人可以帮我吗?我是编程新手,已经坚持了两天。顺便说一句,如果您发现任何不是“专业”编写的代码,如果您能教我如何以适当的方式编写它们,我将不胜感激。非常感谢!

每个点由它的坐标(x,z)和一个指向另一个文件中该点数据的指针组成。在四叉树中,每个点只存储一个点,所以 a) 将一个点插入到一个没有孩子也没有点的区域时,该点正好进入该区域; b) 当一个区域有孩子时,我们尝试将该点插入到它的一个孩子中。 c) 当将一个点插入到没有子区域但已经被一个点占据的区域时,将该区域细分为四个相等的子区域,并将旧点从该区域中取出并放入其中一个子区域中。然后,我们尝试将新点插入到孩子中。

class point():
    def __init__(self,x,z,pointer):
        self.x = x
        self.z = z
        self.pointer = pointer


class Node():
    #_______________________________________________________
    # In the case of a root node "parent" will be None. The
    # "rect" lists the minx,minz,maxx,maxz of the rectangle
    # represented by the node.
    def __init__(self, parent, rect):
        self.parent = parent
        self.children = None
        self.point = None
        self.leaf = 0   # node is a leaf(=1) if it contains a point

        if parent == None:
            self.depth = 0
        else:
            self.depth = parent.depth + 1
        self.rect = rect
        x0,z0,x1,z1 = rect


    #_______________________________________________________
    # Subdivides a rectangle. Division occurs 
    def subdivide(self):
        self.point = None
        self.leaf = 0
        self.children = [None,None,None,None]
        x0,z0,x1,z1 = self.rect
        h = (x1 - x0)/2
        rects = []
        rects.append( (x0, z0, x0 + h, z0 + h) )
        rects.append( (x0, z0 + h, x0 + h, z1) )
        rects.append( (x0 + h, z0 + h, x1, z1) )
        rects.append( (x0 + h, z0, x1, z0 + h) )
        for n in xrange(len(rects)):
            self.children[n] = Node(self,rects[n])

    #_______________________________________________________
    # A utility proc that returns True if the coordinates of
    # a point are within the bounding box of the node.
    def contains(self, x, z):
        x0,z0,x1,z1 = self.rect
        if x >= x0 and x <= x1 and z >= z0 and z <= z1:
            return True
        return False



    def insert(self, p):
        if self.contains(p.x, p.z) == False:
            return
        if self.children == None:
            if self.leaf == 1:
                temp_point = copy.copy(self.point)
                self.subdivide()
                self.insert(temp_point)
                self.insert(p)
            else:
                self.point = p
                self.leaf = 1
        else:
            for child in self.children:
                child.insert(p)

【问题讨论】:

  • 顺便说一句,我刚刚在我的代码中发现了另一个错误:在细分中,h 应该替换为 hx 和 hz 以适用于矩形。单独使用 h 仅适用于正方形。

标签: python recursion data-structures quadtree


【解决方案1】:
    h = (x1 - x0)/2

如果您使用的是 Python 2.7,并且此处的 x1 和 x0 都是整数,则此除法的结果将被截断为最接近的整数。例如,如果 x1 为 1,x0 为 0,您可能期望它们的中点为 0.5。但是(1-0)/2 等于 0。这会导致subdivide 出现问题,其中四个子矩形中的三个将无限小,而第四个将与父矩形相同大小。尝试强制浮点除法。

    h = (x1 - x0)/2.0

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 2021-12-11
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    相关资源
    最近更新 更多