1.0.4 • Published 1 year ago

@jgtools/shg v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

SHG

npm npm GitHub

2D Spatial Hash Grid

Features

  • :heavy_check_mark: Elements are stored on a 2D grid
  • :heavy_check_mark: Filter nearby elements
  • :blue_square: 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