@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 +6 -6
- package/package.json +2 -2
- package/src/knn.js +1 -0
- package/src/ray.js +6 -4
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
|
|
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-
|
|
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.
|
|
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": "
|
|
41
|
+
"@gridworkjs/quadtree": "^1.1.0",
|
|
42
42
|
"typescript": "^5.9.3"
|
|
43
43
|
}
|
|
44
44
|
}
|
package/src/knn.js
CHANGED
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
|
|
59
|
-
const
|
|
60
|
-
|
|
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
|