这种递归Quadtree/细分算法有什么问题?
如果窗口内有一定的点,则应该找到此功能,以及是否在其周围有一个矩形,然后通过递归调用上一个矩形所有四个象限的相同函数来细分。这种行为对于前两个递归几乎是正确的,然后似乎有点糟糕。.我还设定了一个最大深度,以使其不会恢复太久。我使用pygame在屏幕上绘制矩形。我假设我在某个地方的逻辑中陷入困境。
def recursion(x,x2,y,y2,max):
#draws a square
pygame.draw.rect(screen,(0,255,255),(x,y,x2,y2),1)
currentMax = 0
total_points = 0
if max >= 30:
return None
currentMax = max + 1
for i in range(len(xlist)):
if xlist[i] in range(x,x2) and ylist[i] in range(y,y2):
total_points += 1
if total_points > 3:
recursion(int(x),int(x2/2),int(y),int(y2/2),currentMax)#top_left
recursion(int(x2/2),int(x2),int(y),int(y2/2),currentMax)#top_right
recursion(int(x),int(x2/2),int(y2/2),int(y2),currentMax)#bottom_left
recursion(int(x2/2),int(x2),int(y2/2),int(y2),currentMax)#bottom_right
我还称其为启动递归:
recursion(int(0),int(1000),int(0),int(1000),0)
使用以下方式生成:这些点是使用以下方式生成的:
for i in range(5):
xlist.append(random.randint(0,1000))
ylist.append(random.randint(0,1000))
this function is supposed to find if there is above a set number of points within the window, and if there is draw a rectangle around it and then subdivide by recursively calling the same function on all four quadrants of the previous rectangle. The behaviour seems almost right for the first 2 recursions and then seems to go a bit awry.. I also set a max depth so that it dosn't recurse too long. Im using pygame to draw the rectangles on the screen. Im assuming I have got muddled in my logic somewhere.
def recursion(x,x2,y,y2,max):
#draws a square
pygame.draw.rect(screen,(0,255,255),(x,y,x2,y2),1)
currentMax = 0
total_points = 0
if max >= 30:
return None
currentMax = max + 1
for i in range(len(xlist)):
if xlist[i] in range(x,x2) and ylist[i] in range(y,y2):
total_points += 1
if total_points > 3:
recursion(int(x),int(x2/2),int(y),int(y2/2),currentMax)#top_left
recursion(int(x2/2),int(x2),int(y),int(y2/2),currentMax)#top_right
recursion(int(x),int(x2/2),int(y2/2),int(y2),currentMax)#bottom_left
recursion(int(x2/2),int(x2),int(y2/2),int(y2),currentMax)#bottom_right
I also call it once to start the recursion with:
recursion(int(0),int(1000),int(0),int(1000),0)
The points are generated using:
for i in range(5):
xlist.append(random.randint(0,1000))
ylist.append(random.randint(0,1000))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当用您必须指定顶部,左点以及宽度和高度,而不是右下点。
此外,中心的计算是错误的。最大深度为30(2^30)太多了,您可以使用
//
(落地)操作员。以
最大= 5
和total_points> = 1
:最小示例:
When drawing a rectangle with
pygame.draw.rect
you have to specify the top, left point and the width and height instead of the bottom right point.Furthermore, the computation of the center is wrong. A maximum depth of 30 (2^30) is far too much and you can use the
//
(floor division) operator.Start with
max >= 5
andtotal_points >= 1
:Minimal example: