Я пытаюсь визуализировать древовидную карту, где корнем древовидной карты является папка, а ее дочерними элементами являются либо папки, либо файлы, а дочерними элементами папки могут быть дополнительные папки или файлы и т. Д. (Дерево поиска).Я могу получить размеры древовидных карт в порядке, но проблема, которую я имею, пытается сделать так, чтобы алгоритм фактически устанавливал их так, чтобы они соответствовали прямоугольнику, указанному в 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)
. Любая помощь будет приветствоваться!