Quadtree¶
- detroit.quadtree(data: list[T] | None = None, x: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float] = None, y: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float] = None) Quadtree[T]¶
Creates a new, empty quadtree with an empty extent and the default x and y accessors. If data is specified, adds the specified iterable of data to the quadtree.
- class detroit.quadtree.quadtree.Quadtree(x: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float], y: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float], x0: float, y0: float, x1: float, y1: float)¶
A quadtree recursively partitions two-dimensional space into squares, dividing each square into four equally-sized squares. Each distinct point exists in a unique leaf node; coincident points are represented by a linked list. Quadtrees can accelerate various spatial operations, such as the Barnes–Hut approximation for computing many-body forces, collision detection, and searching for nearby points.
- Parameters:
- add(d: T) Quadtree¶
Adds the specified datum to the quadtree, deriving its coordinates \((x, y)\) using the current x and y accessors, and returns the quadtree.
If the new point is outside the current extent of the quadtree, the quadtree is automatically expanded to cover the new point.
- Parameters:
d (T) – Datum
- Returns:
Itself
- Return type:
- add_all(data: list[T]) Quadtree¶
Adds the specified iterable of data to the quadtree, deriving each element’s coordinates \((x, y)\) using the current x and y accessors, and return this quadtree.
- Parameters:
data (list[T]) – List of data values
- Returns:
Itself
- Return type:
- cover(x: float, y: float) Quadtree¶
Expands the quadtree to cover the specified point \((x, y)\), and returns the quadtree.
- Parameters:
x (float) – X value
y (float) – Y value
- Returns:
Itself
- Return type:
- data() list[T]¶
Returns an array of all data in the quadtree.
- Returns:
List of all data
- Return type:
list[T]
- find(x: float, y: float, radius: float | None = None) Quadtree¶
Returns the datum closest to the position \((x, y)\) with the given search radius. If radius is not specified, it defaults to infinity.
- Parameters:
x (float) – x value
y (float) – y value
radius (float | None) – radius value
- Returns:
Itself
- Return type:
- remove(d: T) Quadtree¶
Removes the specified datum from the quadtree, deriving its coordinates \((x,y)\) using the current x and y accessors, and returns the quadtree.
- Parameters:
d (T) – Datum
- Returns:
Itself
- Return type:
- remove_all(data: list[T]) Quadtree¶
Removes the specified data from the quadtree, deriving their coordinates \((x,y)\) using the current x and y accessors, and returns the quadtree.
- Parameters:
data (list[T]) – List of data
- Returns:
Itself
- Return type:
- set_extent(extent: list[tuple[float, float], tuple[float, float]]) Quadtree¶
Expands the quadtree to cover the specified points
[[x0, y0], [x1, y1]]and returns the quadtree.- Parameters:
extent (list[tuple[float, float], tuple[float, float]]) – Extent values
- Returns:
Itself
- Return type:
- size() float¶
Returns the total number of data in the quadtree.
- Returns:
Total number of data in sthe quadtree.
- Return type:
float
- visit(callback: Callable[[list | dict | None, float, float, float, float], bool]) Quadtree¶
Visits each node in the quadtree in pre-order traversal, invoking the specified callback with arguments
node,x0,y0,x1,y1for each node, where node is the node being visited, \((x_0, y_0)\) are the lower bounds of the node, and \((x_1, y_1)\) are the upper bounds, and returns the quadtree. (Assuming that positive x is right and positive y is down, as is typically the case in Canvas and SVG, \((x_0, y_0)\) is the top-left corner and \((x_1, y_1)\) is the lower-right corner; however, the coordinate system is arbitrary, so more formally \(x_0 \le x_1\) and \(y0 \le y1\).)If the callback returns true for a given node, then the children of that node are not visited; otherwise, all child nodes are visited. This can be used to quickly visit only parts of the tree, for example when using the Barnes–Hut approximation. Note, however, that child quadrants are always visited in sibling order: top-left, top-right, bottom-left, bottom-right. In cases such as search, visiting siblings in a specific order may be faster.
- Parameters:
callback (Callable[[list | dict | None, float, float, float, float], bool]) – Callback function
- Returns:
Itself
- Return type:
Notes
The number of arguments for the
callbackfunction is automatically determined. Therefore, you can pass 0 to 5 arguments to thecallbackfunction. For example, def callback(node): is a valid function.
- visit_after(callback: Callable[[dict, float, float, float, float], bool]) Quadtree¶
Visits each node in the quadtree in post-order traversal, invoking the specified callback with arguments
node,x0,y0,x1,y1for each node, where node is the node being visited,(x_0, y_0)are the lower bounds of the node, and(x_1, y_1)are the upper bounds, and returns the quadtree. (Assuming that positivexis right and positiveyis down, as is typically the case in Canvas and SVG,(x_0, y_0)is the top-left corner and(x_1, y_1)is the lower-right corner; however, the coordinate system is arbitrary, so more formally \(x_0 \lt x_1\) and \(y0 \lt y1\).). Returns root.- Parameters:
callback (Callable[[dict, float, float, float, float], bool]) – Callback function
- Returns:
Itself
- Return type:
Notes
The number of arguments for the
callbackfunction is automatically determined. Therefore, you can pass 0 to 5 arguments to thecallbackfunction. For example, def callback(node): is a valid function.
- x(x: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float]) Quadtree¶
Sets the x-coordinate accessor and returns the quadtree. The x accessor is used to derive the x coordinate of data when adding to and removing from the tree. It is also used when finding to re-access the coordinates of data previously added to the tree; therefore, the x and y accessors must be consistent, returning the same value given the same input.
- y(y: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float]) Quadtree¶
Sets the y-coordinate accessor and returns the quadtree. The y accessor is used to derive the y coordinate of data when adding to and removing from the tree. It is also used when finding to re-access the coordinates of data previously added to the tree; therefore, the x and y accessors must be consistent, returning the same value given the same input.