2D Spatial Hash Grid
- ✔️ Elements are stored on a 2D grid
- ✔️ Filter nearby elements
- 🟦 Written in TypeScript
npm i @jgtools/shg
import SHG from "@jgtools/shg";
// ...
<script type="module">
import SHG from "https://cdn.jsdelivr.net/npm/@jgtools/shg@1.0.4/dist/index.min.js";
// ...
</script>
import SHG from "@jgtools/shg";
const width = 100;
const height = 60;
const cellSize = 10;
const shg = new SHG(width, height, cellSize);
for (let i = 0; i < 10; i++) {
const [x, y] = [Math.random() * width, Math.random() * height];
shg.set(i.toString(), x, y);
}
console.log(shg.query(40, 30, 10));
shg.update("1", 40, 30); // update item '1' position
console.log(shg.query(40, 30, 10));
shg.delete("1"); // delete item '1'
console.log(shg.query(40, 30, 10));
shg.clear(); // delete all items from grid
console.log(shg.query(40, 30, 10));
MIT