Skip to content

JGTools/SHG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SHG

npm npm GitHub

2D Spatial Hash Grid

Features

  • ✔️ Elements are stored on a 2D grid
  • ✔️ Filter nearby elements
  • 🟦 Written in TypeScript

Installation

Using npm

npm i @jgtools/shg
import SHG from "@jgtools/shg";
// ...

Using cdn

<script type="module">
    import SHG from "https://cdn.jsdelivr.net/npm/@jgtools/shg@1.0.4/dist/index.min.js";
    // ...
</script>

Usage

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));

License

MIT