@gridworkjs/query 1.0.0 → 1.1.0

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
@@ -62,12 +62,12 @@ const primary = targets[0]?.item
62
62
 
63
63
  ### Ray - "What does this line hit?"
64
64
 
65
- Cast a ray from the player eastward. A bullet, a line of sight check, or a laser:
65
+ Cast a ray from the player toward an enemy. A bullet, a line of sight check, or a laser:
66
66
 
67
67
  ```js
68
- const hits = ray(tree, { x: 100, y: 200 }, { x: 1, y: 0 })
69
- // => [{ item: { id: 'chest', ... }, distance: 5 },
70
- // { item: { id: 'enemy-2', ... }, distance: 300 }]
68
+ const hits = ray(tree, { x: 100, y: 200 }, { x: 1, y: 0.5 })
69
+ // => [{ item: { id: 'chest', ... }, distance: 5.59 },
70
+ // { item: { id: 'enemy-1', ... }, distance: 22.36 }]
71
71
 
72
72
  const firstHit = hits[0]?.item
73
73
  ```
@@ -113,10 +113,10 @@ Cast a ray and find all items it intersects. Returns `{ item, distance }[]` sort
113
113
 
114
114
  ### `within(index, region)`
115
115
 
116
- Find all items fully contained within a region. Unlike `search()` which returns items that intersect, `within()` requires complete containment. Returns plain items (no distance annotation).
116
+ Find all items fully contained within a region's bounding box. Unlike `search()` which returns items that intersect, `within()` requires complete containment. Returns plain items (no distance annotation).
117
117
 
118
118
  - `index` - any spatial index implementing the gridwork protocol
119
- - `region` - bounds object, or any gridwork geometry (point, rect, circle)
119
+ - `region` - bounds object, or any gridwork geometry (point, rect, circle). Circles and other geometries are converted to their axis-aligned bounding box
120
120
 
121
121
  ## Works with Any Gridwork Index
122
122
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gridworkjs/query",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Higher-level spatial queries against any gridwork index",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -38,7 +38,7 @@
38
38
  "@gridworkjs/core": "^1.0.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@gridworkjs/quadtree": "file:../quadtree",
41
+ "@gridworkjs/quadtree": "^1.1.0",
42
42
  "typescript": "^5.9.3"
43
43
  }
44
44
  }
package/src/knn.js CHANGED
@@ -62,5 +62,6 @@ export function knn(index, point, k, options) {
62
62
  }
63
63
  }
64
64
 
65
+ results.sort((a, b) => a.distance - b.distance)
65
66
  return results
66
67
  }
package/src/ray.js CHANGED
@@ -24,11 +24,11 @@ export function ray(index, origin, direction, options) {
24
24
  const dx = direction.x / len
25
25
  const dy = direction.y / len
26
26
 
27
- const maxDist = (options && options.maxDistance) || Infinity
28
27
  if (options && options.maxDistance !== undefined) {
29
28
  validateFiniteNumber(options.maxDistance, 'maxDistance')
30
29
  if (options.maxDistance < 0) throw new Error('maxDistance must be non-negative')
31
30
  }
31
+ const maxDist = (options && options.maxDistance !== undefined) ? options.maxDistance : Infinity
32
32
 
33
33
  const indexBounds = index.bounds
34
34
  if (!indexBounds) return []
@@ -55,9 +55,11 @@ export function ray(index, origin, direction, options) {
55
55
  function raySearchBounds(origin, dx, dy, maxDist, indexBounds) {
56
56
  let endDist = maxDist
57
57
  if (!Number.isFinite(endDist)) {
58
- const w = indexBounds.maxX - indexBounds.minX
59
- const h = indexBounds.maxY - indexBounds.minY
60
- endDist = Math.sqrt(w * w + h * h) * 2
58
+ const farX = dx >= 0 ? indexBounds.maxX : indexBounds.minX
59
+ const farY = dy >= 0 ? indexBounds.maxY : indexBounds.minY
60
+ const dfx = farX - origin.x
61
+ const dfy = farY - origin.y
62
+ endDist = Math.sqrt(dfx * dfx + dfy * dfy) * 1.5
61
63
  }
62
64
 
63
65
  const endX = origin.x + dx * endDist