Comments (2)
Hey, I think the easiest way to achieve this is to modify the retrieve function and collect the nodes too while retrieving the objects.
I put together a demo showcasing this: https://timohausmann.github.io/quadtree-js/retrieve-nodes.html
In this example the retrieve function now returns an object with both objects and nodes, like { objects: Rect[], nodes: Quadtree[] }
Note that multiple nodes can be retrieved, or none if there are no matching objects. Does this work for you?
Modified branch: https://github.com/timohausmann/quadtree-js/tree/retrieve-nodes
If you use the npm package you could simply overwrite the Quadtree.prototype.retrieve function with the modified version after importing it.
from quadtree-js.
Thanks for the quick implementation! Looking at your sample it seems it does what I'm looking for.
Just in case you decided to include this feature later, I just thought in the same way getIndex retrieve the first node index, perhaps a method like getLastIndex
could get the deepest node in the tree,
As an aside, I put another method which collects all nodes indexes along the path leading to the object.
It only works for my specific case without objects overlaping over several nodes, and a unique path to the object
Also this is a quick non optimized implementation but in case it would be useful for someone else , here it is
/**
* Path in subtree leading to the specified element
* @param rootNode
* @param targetElt
* @returns node indices
*/
retrievePath(rootNode: Quadtree, targetElt: Rect) {
if (rootNode.retrieve(targetElt).length) {
let nodesIndices: any = [];
let nodeIndex ;
let currentNode = rootNode;
// until we reach the deepest node (=leaf)
while (currentNode.nodes.length) {
// look for the index of the next child node containing the object
nodeIndex = currentNode.nodes.findIndex((childNode, i) => childNode.retrieve(targetElt).length);
nodesIndices.push(nodeIndex);
// update current node
currentNode = currentNode.nodes[nodeIndex];
}
// print found path
console.log("Matching path found: " + nodesIndices.reduce((concat, nodeIndex) => concat + " => " + nodeIndex));
return nodesIndices;
} else return null;
}
from quadtree-js.
Related Issues (17)
- Insert / Retrieve seems to be wrong HOT 3
- how to build quadtree? HOT 2
- Rounding sub quad bounds HOT 1
- update elements? HOT 3
- Best method to check all players against all players in the quadtree? HOT 2
- TypeError: t is Undefined HOT 4
- Adding support for more primitives checks HOT 9
- Adding a licence file HOT 1
- It just doesn't work? HOT 3
- is the width and height from the center or from a corner? HOT 3
- Modify and remove objects from quad-tree HOT 3
- Infinite loop issue HOT 5
- hello,i'm chinese,My English is very terrible.I want to how to use this quadtreejs in nodejs HOT 2
- retrieval of object out side the node HOT 6
- This visualization looks good, but there are too many objects on parent node HOT 3
- Quad Tree should only store objects on leaf nodes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quadtree-js.