Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
ngraph.quadtreebh
Advanced tools
Readme
Implementation of Barnes-Hut N-Body simulation in Javascript.
I'm still working on API and it might be changed in future.
var Body = require('ngraph.physics.primitives').Body;
// Create new bodies at (1, 0) and (2, 0):
var bodies = [];
bodies.push(new Body(1, 0));
bodies.push(new Body(2, 0));
/* ... create more as you need ... */
// build quad tree:
var createQuadTree = require('ngraph.quadtreebh');
var quadTree = createQuadTree();
// insert bodies into the quad tree
quadTree.insertBodies(bodies); // performance: O(n * log n)
// calculate forces acting on each body in the tree O(n * log n):
bodies.forEach(function(body) {
quadTree.updateBodyForce(body);
});
// At this point every body object has valid 2d force vecor
console.dir(bodies[0].force);
Note: You don't necessary have to use ngraph.physics.primitives. That package merely defines an interface for a physical Body, which is expected by quad tree. As long as your Body object implements this interface (mass
, pos
and force
) - you can use current quad tree structure to calculate forces.
Quad tree allows to change two global options:
gravity
- Gravitational constant, used in force value calculation. Defaults to -1
;theta
- a threshold which determines when group of bodies is considered distant enough to be approximated as a single body. The value should be > 0
, and usually little less than 1
. It defaults to 0.8
if not suplied. This argument affects accuracy and speed of simulation. Please refer to [1] for more details.You can pass these setting to quad tree as follows:
var createQuadTree = require('ngraph.quadtreebh');
var quadTree = createQuadTree({
theta: 1.2,
gravity: -10
});
To query current options of the tree use:
var createQuadTree = require('ngraph.quadtreebh');
var quadTree = createQuadTree();
console.dir(quadTree.options()); // prints { theta: 0.8, gravity: -1};
To change options at run time:
var createQuadTree = require('ngraph.quadtreebh');
var quadTree = createQuadTree();
quadTree.options({
theta: 0.5,
gravity: -42
});
Mass of each body affects overall result of computation. You can tweak it when creating new bodies:
var Body = require('ngraph.physics.primitives').Body;
var earth = new Body(); earth.mass = 5.972;
var sun = new Body(); sun.mass = 1989000;
npm install ngraph.quadtreebh
BSD 3-Clause
FAQs
Quad tree data structure for Barnes-Hut simulation
The npm package ngraph.quadtreebh receives a total of 1,491 weekly downloads. As such, ngraph.quadtreebh popularity was classified as popular.
We found that ngraph.quadtreebh demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.