This is an extension and not part of the main GoJS library.
Note that the API for this class may change at any time.
If you intend to use an extension in production, you should copy the code to your own source directory.
Extensions can be found in the GoJS kit under the
See the Extensions intro page for more information.
Implementation of the quadtree data structure using the Rect class. Each Quadtree has defined bounds found at bounds, an array of member rectangles, and an array of child nodes (Quadtrees themselves). If the Quadtree has no children, the nodes array will have four nulls. To construct a Quadtree, you can call its constructor with no arguments. Then, to insert a rectangle, call add. This tree supports adding points (rectangles with 0 width and height), segments (rectangles with either 0 width or 0 height), and rectangles with nonzero widths and heights.
Quadtrees can be used to calculate intersections extremely quickly between a given rectangle and all of the rectangles in the quadtree. Use of this data structure prevents having to do precise intersection calculations for every rectangle in the tree. To calculate all of the rectangular intersections for a given rectangle, use intersecting.
Other common operations are detailed below.
In most cases, simply calling this constructor with no arguments will produce the desired behaviour.
The node capacity of this quadtree. This is the number of objects a node can contain before it splits. Defaults to 1.
The maximum depth the Quadtree will allow before it will no longer split. Defaults to Infinity (no maximum depth).
The bounding box surrounding the entire Quadtree. If the bounds are unset or a node is inserted outside of the bounds, the tree will automatically grow.
Gets the boundaries of the node. All nodes should be square.
Gets the maximum depth the Quadtree will allow before it will no longer split..
Gets the node capacity of this quadtree. This is the number of objects a node can contain before it splits.
Gets the root node of the tree
Insert the object into the quadtree. If the node exceeds the capacity, it will split and add all objects to their corresponding nodes. If the object is outside the bounds of the tree's root node, the tree will grow to accomodate it. Possibly restructures the tree if a more efficient configuration can be found with the new dimensions. Bounds can be given either as a single Rect or as any combination of arguments which is valid for the Rect constructor.
the object to insert
The Rect bounds of the object, or top-left Point, or x value.
Bottom-right Point or Size or y value.
Width to be used if x,y are specified; must be non-negative.
Height to be used if x,y are specified;
Clears the Quadtree, removing all objects and children nodes. Keeps the current bounds of the root node.
Returns the square of the distance from the centers of the given objects
square of the distance between the centers of obj1 and obj2
Return the node that contains the given object.
the object to find
the node containing the given object, null if the object is not found
Finds the most furthest object in each direction stored in the tree. Bounds are tested using the center x and y coordinate.
maximum and minimum objects in the tree, in the format [min x, max x, min y, max y].
Visits every object stored in the tree (depth first)
the callback to execute on each object.
Convenience method, calls find and returns a boolean indicating whether or not the tree contains the given object
the object to check for
whether or not the given object is present in the tree
Return all objects that intersect (wholly or partially) with the given Rect or Point. Touching edges and objects overlapping by 1e-7 or less (to account for floating point error) are both not considered intersections.
the Rect or Point to check intersections for. If a point is given, a Rect with size (0, 0) is created for intersection calculations.
array containing all intersecting objects
Can be called as either (obj, x, y) or (obj, point). Translate the given object to given x and y coordinates or to a given Point.
the object to move
the x coordinate or Point to move the object to
the y coordinate to move the object to
whether or not the move was successful. False if the object was not in the tree.
Remove the given object from the tree, restructuring to get rid of empty nodes that are unneeded.
the object to remove
whether or not the deletion was successful. False when the object is not in the tree.
Can be called as either (obj, width, height) or (obj, size). Resize the given object to given width and height or to a given Size.
the object to resize
the width or Size to resize the object to
the height to resize the object to
whether or not the resize was successful. False if the object was not in the tree.
Updates the given object to have the bounds given, provided as either a Rect or x, y, width, and height.
the object to change the bounds of
the x-coordinate or Rect to set the object to
the y-coordinate to set the object to, unnecessary if a Rect was given
the width to set the object to, unnecessary if a Rect was given
the height to set the object to, unnecessary if a Rect was given
Recursively traverses the tree (depth first) and executes the given callback on each node.
the callback to execute on each node. Takes the form of (n: Quadtree) => void
whether or not to execute the callback on the root node as well. Defaults to true