I'm writing a program at the moment that will display rectangles in PyGame according to the sizes of files. This is fully based on search trees, with the root being a folder, its subtrees being either folders or files, and files being the leaves, as they dont have any subtrees. I want my program to recursively fill the square with reectangles based on the sizes of the files in relation to the root folder. I.E: root folder has size 151, file1 has size 50, so file1 would take up roughly 33% of the whole square. I also need to make it such that I could make horizontal rectangles if need be (i.e if there isn't any space to do it vertically, do a horizontal rectangle). The function takes in a rect to use, which is different from self.rect defined in my object Filetree (described below)
So my program is based around an object named FileTree, which has self.rect, which is its rectangle size (with x, y being the startpoint, width, height, being its width and height). It has randomly assigned colors, and it has subtrees. Basically I just don't see where my program is going wrong, because when I call the function, I get a bunch of rectangles with the right ratios, just one on top of the other,so it doesn't space it properly. I was wondering if it was a problem with the recursion formula I'm using, since debugging it gives me multiple rectangles with the same starting point, when they're supposed to be one next to the other (i.e one starts where the other one ends)
x, y, width, height = rect
#rect is a tuple (x, y, width, height)
if self.is_empty():
self.rect = (0, 0, 0, 0)
if self._parent_tree is None:
self.rect = rect
else:
p = self
while p._parent_tree is not None:
p = p._parent_tree
percentsize = self.data_size / p.data_size
newsize = math.floor(p.data_size * percentsize)
if p.rect[2] >= newsize + x > y + newsize and percentsize != 1:
self.rect = (x, y, width, newsize)
elif p.rect[3] >= newsize + y >= newsize + x and percentsize != 1:
self.rect = (x, y, newsize, height)
elif percentsize == 1:
self.rect = rect
else:
if newsize + width > height + newsize:
self.rect = (x, y, width, p.rect[3])
elif newsize + height >= newsize + width:
self.rect = (x, y, p.rect[2], height)
for sub in self._subtrees:
sub.update_rectangles(self.rect)