@gridworkjs/hashgrid 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,35 +14,30 @@ npm install @gridworkjs/hashgrid
14
14
 
15
15
  ## Usage
16
16
 
17
+ A particle simulation where thousands of uniform-sized particles need fast collision checks every frame:
18
+
17
19
  ```js
18
20
  import { createHashGrid } from '@gridworkjs/hashgrid'
19
- import { point, rect, bounds } from '@gridworkjs/core'
20
-
21
- // create a hash grid - cellSize should match your data's density
22
- const grid = createHashGrid(item => bounds(item.position), { cellSize: 50 })
23
-
24
- // insert items
25
- grid.insert({ id: 1, position: point(10, 20) })
26
- grid.insert({ id: 2, position: point(50, 60) })
27
- grid.insert({ id: 3, position: rect(70, 70, 90, 90) })
21
+ import { point, bounds } from '@gridworkjs/core'
28
22
 
29
- // search for items intersecting a region
30
- grid.search({ minX: 0, minY: 0, maxX: 55, maxY: 65 })
31
- // => [{ id: 1, ... }, { id: 2, ... }]
23
+ // particles are ~10 units across, so a 20-unit cell size works well
24
+ const particles = createHashGrid(p => bounds(p.position), { cellSize: 20 })
32
25
 
33
- // also accepts geometry objects as queries
34
- grid.search(rect(0, 0, 55, 65))
26
+ particles.insert({ id: 'a', position: point(10, 20), vx: 1, vy: 0 })
27
+ particles.insert({ id: 'b', position: point(15, 22), vx: -1, vy: 1 })
28
+ const c = { id: 'c', position: point(300, 400), vx: 0, vy: -1 }
29
+ particles.insert(c)
35
30
 
36
- // find nearest neighbors
37
- grid.nearest({ x: 12, y: 22 }, 2)
38
- // => [{ id: 1, ... }, { id: 2, ... }]
31
+ // each frame, check for collisions near each particle
32
+ const nearby = particles.search({ minX: 5, minY: 15, maxX: 25, maxY: 30 })
33
+ // => [particle a, particle b] - fast O(1) cell lookup
39
34
 
40
- // remove by identity
41
- grid.remove(item)
35
+ // find the closest neighbor for attraction/repulsion forces
36
+ particles.nearest({ x: 10, y: 20 }, 1)
37
+ // => [particle b]
42
38
 
43
- grid.size // number of items
44
- grid.bounds // bounding box of all items
45
- grid.clear()
39
+ // particle leaves the simulation (by reference)
40
+ particles.remove(c)
46
41
  ```
47
42
 
48
43
  ## When to Use a Hash Grid
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gridworkjs/hashgrid",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Spatial hash grid for uniform distributions and fast neighbor lookups",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/hashgrid.js CHANGED
@@ -154,6 +154,8 @@ export function createHashGrid(accessor, options = {}) {
154
154
  const index = {
155
155
  [SPATIAL_INDEX]: true,
156
156
 
157
+ accessor,
158
+
157
159
  get size() { return size },
158
160
 
159
161
  get bounds() { return totalBounds },