remove result

This commit is contained in:
Awen lelu 2025-11-24 16:51:30 +01:00
parent a3ec861faa
commit 77080eae25
3 changed files with 50 additions and 24 deletions

View File

@ -4,13 +4,14 @@
{ {
devShells.x86_64-linux = devShells.x86_64-linux =
let let
python = pkgs.python312; python = pkgs.python312Full;
pythonEnv = python.withPackages ( pythonEnv = python.withPackages (
ps: with pkgs.python312Packages; [ ps: with ps; [
pip pip
ipython ipython
tkinter matplotlib
numpy
ruff ruff
jedi-language-server jedi-language-server

4
main.py Normal file
View File

@ -0,0 +1,4 @@
from src.quadtree import TkQuadTree
if __name__ == "__main__":
TkQuadTree.fromFile("files/quadtree.txt").paint()

View File

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
from tkinter import ttk, Tk, Canvas from tkinter import ttk, Tk, Canvas
from typing import Self
class QuadTree: class QuadTree:
@ -17,19 +18,26 @@ class QuadTree:
self.bd = bd self.bd = bd
self.bg = bg self.bg = bg
@property
def corners(self) -> list[bool | QuadTree]:
"""
utility function returning all the corners of the quad as a tuple
"""
return (self.hg, self.hd, self.bd, self.bg)
@property @property
def depth(self) -> int: def depth(self) -> int:
"""Recursion depth of the quadtree""" """Recursion depth of the quadtree"""
depths = [] depths = []
for i in ["hg", "hd", "bd", "bg"]: for corner in self.corners:
try: try:
depths.append(self.__dict__[i].depth + 1) depths.append(corner.depth + 1)
except AttributeError: except AttributeError:
depths.append(1) depths.append(1)
return max(depths) return max(depths)
@staticmethod @classmethod
def fromFile(filename: str) -> QuadTree: def fromFile(cls, filename: str) -> Self:
"""Open a given file, containing a textual representation of a list""" """Open a given file, containing a textual representation of a list"""
def parse(file) -> QuadTree: def parse(file) -> QuadTree:
@ -37,11 +45,11 @@ class QuadTree:
while char := file.read(1): while char := file.read(1):
match char: match char:
case "0" | "1": case "0" | "1":
nodes.append(char) nodes.append(char == "1")
case "[": case "[":
nodes.append(parse(file)) nodes.append(parse(file))
case "]": case "]":
return QuadTree(*nodes) return cls(*nodes)
case _: case _:
pass pass
if len(nodes) == 1: if len(nodes) == 1:
@ -59,24 +67,37 @@ class QuadTree:
class TkQuadTree(QuadTree): class TkQuadTree(QuadTree):
def paint(self): def paint(self, size_x=1000, size_y=1000):
"""TK representation of a Quadtree""" """TK representation of a Quadtree"""
root = Tk() root = Tk()
canvas = Canvas(root, width=1000, height=1000) canvas = Canvas(root, width=size_y, height=size_x)
canvas.pack() canvas.pack()
self.draw_tree((0,0),(1000,1000)) self.draw_tree((0, 0, size_x, size_y), canvas)
canvas.create_rectangle(0, 0, 500, 500, fill = "white")
root.mainloop() root.mainloop()
def draw_tree(boundaries: tuple[tuple[int]]): def draw_tree(self, boundaries: tuple[int], canvas):
pass """
draw the quadtree where each quad will be half of the specified boundarie.
@staticmethod boundaries: a tuple of 4 items describing the start / end corner of the canvas boundarie.
def fromFile(filename): ex: (start_x, start_y, end_x, end_y)
tree = QuadTree.fromFile(filename) """
return TkQuadTree( center = (
hg = tree.hg, # compute the center of the current boundary
hd = tree.hd, boundaries[0] + abs((boundaries[2] - boundaries[0]) // 2),
bd = tree.bd, boundaries[1] + abs((boundaries[3] - boundaries[1]) // 2),
bg = tree.bg
) )
rectangles = [
# compute boundaries of each future quad
(*boundaries[:2], *center),
(center[0], *boundaries[1:3], center[1]),
(*center, *boundaries[2:]),
(boundaries[0], center[1], center[0], boundaries[3]),
]
for rectangle, corner in zip(rectangles, self.corners):
try:
corner.draw_tree(rectangle, canvas)
except AttributeError:
canvas.create_rectangle(
*rectangle,
fill="white" if corner else "black",
)