我正在尝试可视化树图,树图的根是文件夹,它的子级是文件夹或文件,文件夹的子级可以是更多文件夹或文件,等等(搜索树)。我可以很好地设置树图的大小,但是我想解决的问题是使算法实际上将它们设置为适合PyGame中给定的矩形
x, y, width, height = rect
# x, y, represent the upper left bounds of the rectangle given at first,
# and the upper left bounds of any following rectangle when working recursively,
# and width, height are the coordinates of the lower right bounds
# of the original rectangle.
if self.is_empty():
self.rect = (0, 0, 0, 0)
else:
if self._parent_tree is not None:
p = self._parent_tree
# Percent is the value that the size of the child
# is in relation to its parent (i.e 0.9, 0.28, etc)
percent = self.data_size / p.data_size
if width > height:
# Percentsize is the actual size of the child in bytes
percentsize = math.floor(width * percent)
self.rect = (x, y, percentsize, height)
x += percentsize
elif height >= width:
percentsize = math.floor(height * percent)
self.rect = (x, y, width, percentsize)
y += percentsize
else:
self.rect = rect
for sub in self._subtrees:
sub.update_rectangles(self.rect)
输出的结果是一堆大小合适的矩形,最后一个接一个。我不太确定我要去哪里。我做了一个单元测试的例子,输出为
(0, 0, 94, 0)
(0, 0, 76, 0)
(0, 0, 18, 0)
欢迎任何帮助!