QuadTree-py
An interactive pygame implementation of quadtree spatial quantization
Contents
Installation
Clone the repo and navigate into it.
git clone https://github.com/ethanavatar/QuadTree-py.git
cd QuadTree-py/
Install pygame if you don't have it already,
python -m pip install pygame
Usage
Run the main module using:
python main.py
You can use:
ESCAPEto clear the current boardRto create a new random boardLEFT MOUSEto add or remove cells
By default, the window is 1200x1200 pixels, the game board is 200x200 cells, and it runs at 60fps. These constants are stored at the top of the locals.py file if you feel like changing them.
API Reference
class Point(x : int, y : int)
x: X-positiony: Y-position
class Rect( x : int, y : int, w : int, h : int)
x: X-positiony: Y-positionw: Widthh: Height
class Quad(boundary : Rect, capacity : int)
-
boundary: ARectrepresenting the size of the initial quad -
capacity: The number of points allowed in a quad before it subdivides -
subdivide() -> NoneSplits the quad into four equally sized quadrants -
insert(point : Point) -> boolTries to insert a point into the quad. If it doesnt exist within its boundary or its at capacity, it subdivides and calls
insertrecursively to the newly made quads.point: ThePointto insert.
-
query(rect : Rect) - listReturns a list of Points that are within the given Rect.
rect: ARectrepresenting the space in which to query point within.
-
draw(surface : pygame.surface, color) -> NoneDraws a wireframe rectangle for the quad, and recursively, for all of its child quads.
surface: Thepygame.surfaceto draw the quadtree to.color: The color to draw the wireframe rectangles in.
TODO
- Make more generic; framework agnostic