Class QuadTree<E>

Quad tree.

Entries must implement the QuadTreeEntry interface.

Type Parameters

Constructors

  • Construct a new quad tree.

    Type Parameters

    Parameters

    • bounds: RectLike

      Overall bounds of the quad tree

    • maxEntriesPerQuad: number = 64

      Max entries a quad can contain until it has to be subdivided

    • maxDepth: number = 4

      Max subdivision depth. A quad will not be subdivided further if this value is met - even if maxEntriesPerQuad is exceeded

    Returns QuadTree<E>

Properties

bounds: ReadonlyRect

Quad tree bounds.

Accessors

Methods

  • Try to add an entry to the quad tree.

    Parameters

    • entry: E

      Entry to add

    Returns boolean

    true if the entry was added, false if the entry was outside quad tree bounds

  • Delete an entry from the quad tree.

    Parameters

    • entry: E

      Entry to delete (via reference comparison)

    • merge: boolean = false

      If true, try to merge quads after the deletion

    Returns boolean

    true if entry was deleted

    Traverses the whole tree to ensure the entry is deleted. If the entry's position and size has not changed, use QuadTree.deleteSpatial for a delete that uses the quad tree's fast spatial query.

  • Delete an entry from the quad tree.

    Parameters

    • entry: E

      Entry to delete (via reference comparison)

    • merge: boolean = false

      If true, try to merge quads after the deletion

    Returns boolean

    true if entry was deleted

    If the entry's position or shape has changed, use QuadTree.delete to ensure its deletion.

  • Get all entries that match the predicate.

    Parameters

    • entryPredicate: (entry: E) => boolean

      Entry predicate

    • quadPredicate: (rect: ReadonlyRect) => boolean

      Quad predicate. Will be called with quad bounds

    • Optionalresults: ArraySet<E>

      Optional Set to put results into. Will allocate a new Set otherwise

    Returns ArraySet<E>

    A Set containing all matched entries

  • Merge subdivided quads that fall below the max entry value.

    Returns void

  • Update an entry in the tree after it was transformed. Entry may be added to the tree if it was not present before. It may also be deleted even if it was present if the new shape is outside the quad tree's bounds.

    Parameters

    • entry: E

      Entry to update

    • merge: boolean = false

      If true, try to merge quads after the deletion

    Returns boolean

    true if the entry is present after this operation

    Combines both QuadTree.add and QuadTree.delete into one operation which removes the need to traverse part of the tree again in QuadTree.add.