【问题标题】:inserting, delating and display points into a quadtree using Python使用 Python 在四叉树中插入、删除和显示点
【发布时间】:2015-06-26 04:39:39
【问题描述】:

我正在尝试构建一个点区域quadtree,它使用 Python 在 2D 地图上存储点。

每个点由它的坐标(x, z) 和一个指向另一个文件中该点数据的指针组成。

在四叉树中,每个只存储一个点,所以

  1. 当插入一个点到一个没有孩子也没有点的区域时,这个点正好进入这个区域;
  2. 当一个区域有子区域时,我们会尝试将该点插入到它的一个子区域中。
  3. 当将一个点插入到没有子区域但已经被一个点占据时,将该区域细分为四个相等的子区域,并从该区域中取出旧点并放入其中一个子区域。然后,我们尝试将新点插入到孩子中。在我们可以从地图 2D 中检测到该点并显示它之后。

我尝试实现一个插入功能,删除和打印,但我没有成功。有人可以帮我吗?我是编程新手,已经坚持了两天。非常感谢!

【问题讨论】:

  • 你能告诉我们你到目前为止尝试过的代码吗?
  • 我使用相同的代码进行插入,但它不起作用,如果插入不起作用,我无法实现删除和显示link
  • 我在您链接到的代码中看到的唯一一件事是,由于 Gordon 的“包含”方法的重叠测试,同一点可以插入多个子节点的可能性。但是如果您提供自己的代码会很好。

标签: python recursion data-structures quadtree


【解决方案1】:

我也尝试开发这种方法,但是这样的东西效果不佳,因为我需要叶子 prendrend 确定位置的值 (x, y),这里是类 QuadTree(object):

def __init__(self, items=[], depth=6, boundingRect=None):
    # The sub-quadrants are empty to start with.
    self.nw = self.ne = self.se = self.sw = None

    # If we've reached the maximum depth then insert all items into this
    # quadrant.
    self.depth = depth
    if self.depth == 0:
        self.items = items
        return

    # Find this quadrant's centre.
    if boundingRect:
        l, t, r, b = self.l, self.t, self.r, self.b = boundingRect
    else:
        # If there isn't a bounding rect, then calculate it from the items.
        l = self.l = min(item.rect.left for item in items)
        t = self.t = min(item.rect.top for item in items)
        r = self.r = max(item.rect.right for item in items)
        b = self.b = max(item.rect.bottom for item in items)
    cx = self.cx = (l + r) * 0.5
    cy = self.cy = (t + b) * 0.5

    self.items = []
    if not items:
        return
    nw_items = []
    ne_items = []
    se_items = []
    sw_items = []

    for item in items:
        # Which of the sub-quadrants does the item overlap?
        in_nw = item.rect.left <= cx and item.rect.top <= cy
        in_sw = item.rect.left <= cx and item.rect.bottom >= cy
        in_ne = item.rect.right >= cx and item.rect.top <= cy
        in_se = item.rect.right >= cx and item.rect.bottom >= cy

        # If it overlaps all 4 quadrants then insert it at the current
        # depth, otherwise append it to a list to be inserted under every
        # quadrant that it overlaps.
        if in_nw and in_ne and in_se and in_sw:
            self.items.append(item)
        else:
            if in_nw: nw_items.append(item)
            if in_ne: ne_items.append(item)
            if in_se: se_items.append(item)
            if in_sw: sw_items.append(item)

    # Create the sub-quadrants, recursively.
    if nw_items:
        self.nw = QuadTree(nw_items, self.depth-1, (l, t, cx, cy))
    if ne_items:
        self.ne = QuadTree(ne_items, self.depth-1, (cx, t, r, cy))
    if se_items:
        self.se = QuadTree(se_items, self.depth-1, (cx, cy, r, b))
    if sw_items:
        self.sw = QuadTree(sw_items, self.depth-1, (l, cy, cx, b))

def insert(self, item):
    # If we've reached the maximum depth then insert item in this quadrant.
    if self.depth == 0:
        self.items.append(item)
        return

    in_nw = item.rect.left <= self.cx and item.rect.top <= self.cy
    in_sw = item.rect.left <= self.cx and item.rect.bottom >= self.cy
    in_ne = item.rect.right >= self.cx and item.rect.top <= self.cy
    in_se = item.rect.right >= self.cx and item.rect.bottom >= self.cy

    # If it overlaps all 4 quadrants then insert it at the current
    # depth, otherwise append it to the item list of every quadrant
    # that it overlaps.
    if in_nw and in_ne and in_se and in_sw:
        self.items.append(item)
    else:
        if in_nw:
            if self.nw:
                self.nw.insert(item)
            else:
                self.nw = QuadTree([item], self.depth-1,
                                   (self.l, self.t, self.cx, self.cy))
        if in_ne:
            if self.ne:
                self.ne.insert(item)
            else:
                self.ne = QuadTree([item], self.depth-1,
                                   (self.cx, self.t, self.r, self.cy))
        if in_se:
            if self.se:
                self.se.insert(item)
            else:
                self.se = QuadTree([item], self.depth-1,
                                   (self.cx, self.cy, self.r, self.b))
        if in_sw:
            if self.sw:
                self.sw.insert(item)
            else:
                self.sw = QuadTree([item], self.depth-1,
                                   (self.l, self.cy, self.cx, self.b))

def remove(self, item):
    # If we've reached the maximum depth remove the itam from this quadrant.
    if self.depth == 0:
        self.items.remove(item)
        return

    in_nw = item.rect.left <= self.cx and item.rect.top <= self.cy
    in_sw = item.rect.left <= self.cx and item.rect.bottom >= self.cy
    in_ne = item.rect.right >= self.cx and item.rect.top <= self.cy
    in_se = item.rect.right >= self.cx and item.rect.bottom >= self.cy

    # If it overlaps all 4 quadrants remove it, otherwise
    # search the lower quadrants for it
    if in_nw and in_ne and in_se and in_sw:
        self.items.remove(item)
    else:
        if in_nw and self.nw:
                self.nw.remove(item)
        if in_ne and self.ne:
                self.ne.remove(item)
        if in_se and self.se:
                self.se.remove(item)
        if in_sw and self.sw:
                self.sw.remove(item)

类类项(对象):

    def __init__(self, left, top, right, bottom):
        self.left = left
        self.top = top
        self.right = right
        self.bottom = bottom

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2017-08-09
    • 2020-09-29
    相关资源
    最近更新 更多