@zeitfall/quadtree 1.0.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/.github/workflows/publish.yml +34 -0
- package/LICENSE +21 -0
- package/index.ts +231 -0
- package/package.json +33 -0
- package/tsconfig.json +43 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Build and Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [created]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build-and-publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
permissions:
|
|
12
|
+
contents: read
|
|
13
|
+
id-token: write
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- name: Fetch Repository
|
|
17
|
+
uses: actions/checkout@v6
|
|
18
|
+
with:
|
|
19
|
+
ref: main
|
|
20
|
+
|
|
21
|
+
- uses: actions/setup-node@v6
|
|
22
|
+
with:
|
|
23
|
+
node-version-file: package.json
|
|
24
|
+
registry-url: 'https://registry.npmjs.org/'
|
|
25
|
+
|
|
26
|
+
- name: Build Library
|
|
27
|
+
run: |
|
|
28
|
+
npm ci
|
|
29
|
+
npm run build
|
|
30
|
+
|
|
31
|
+
- name: Publish
|
|
32
|
+
run: npm publish --provenance --access public
|
|
33
|
+
env:
|
|
34
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vlad Matsko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.ts
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
class QuadTree<I = unknown> {
|
|
2
|
+
#boundary: QuadTreeBoundary;
|
|
3
|
+
#threshold: number;
|
|
4
|
+
#depth: number;
|
|
5
|
+
#maxDepth: number;
|
|
6
|
+
|
|
7
|
+
#insertables: QuadTreeInsertable<I>[];
|
|
8
|
+
#divided: boolean;
|
|
9
|
+
|
|
10
|
+
#nodeNW: QuadTree<I> | null;
|
|
11
|
+
#nodeNE: QuadTree<I> | null;
|
|
12
|
+
#nodeSW: QuadTree<I> | null;
|
|
13
|
+
#nodeSE: QuadTree<I> | null;
|
|
14
|
+
|
|
15
|
+
constructor(boundary: QuadTreeBoundary, threshold: number, depth = 0, maxDepth = 8) {
|
|
16
|
+
this.#boundary = boundary;
|
|
17
|
+
this.#threshold = threshold;
|
|
18
|
+
this.#depth = depth;
|
|
19
|
+
this.#maxDepth = maxDepth;
|
|
20
|
+
|
|
21
|
+
this.#insertables = [];
|
|
22
|
+
this.#divided = false;
|
|
23
|
+
|
|
24
|
+
this.#nodeNW = null;
|
|
25
|
+
this.#nodeNE = null;
|
|
26
|
+
this.#nodeSW = null;
|
|
27
|
+
this.#nodeSE = null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
insert(x: number, y: number, value: I): boolean {
|
|
31
|
+
const positionInsideBoundary = this.#boundary.contains(x, y);
|
|
32
|
+
|
|
33
|
+
if (!positionInsideBoundary) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (this.#divided) {
|
|
38
|
+
return this.#tryInsertIntoChildren(x, y, value);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (this.#insertables.length < this.#threshold || this.#depth >= this.#maxDepth) {
|
|
42
|
+
this.#insertables.push({ x, y, value });
|
|
43
|
+
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this.#subdivide();
|
|
48
|
+
|
|
49
|
+
for (const insertable of this.#insertables) {
|
|
50
|
+
this.#tryInsertIntoChildren(insertable.x, insertable.y, insertable.value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.#insertables.length = 0;
|
|
54
|
+
|
|
55
|
+
return this.#tryInsertIntoChildren(x, y, value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
query(region: QuadTreeRegion, result: QuadTreeInsertable<I>[]) {
|
|
59
|
+
const regionIntersectsBoundary = region.intersects(this.#boundary);
|
|
60
|
+
|
|
61
|
+
if (!regionIntersectsBoundary) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (this.#divided) {
|
|
66
|
+
this.#nodeNW!.query(region, result);
|
|
67
|
+
this.#nodeNE!.query(region, result);
|
|
68
|
+
this.#nodeSW!.query(region, result);
|
|
69
|
+
this.#nodeSE!.query(region, result);
|
|
70
|
+
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const insertable of this.#insertables) {
|
|
75
|
+
if (region.contains(insertable.x, insertable.y)) {
|
|
76
|
+
result.push(insertable);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
clear() {
|
|
82
|
+
this.#insertables.length = 0;
|
|
83
|
+
this.#divided = false;
|
|
84
|
+
|
|
85
|
+
this.#nodeNW?.clear();
|
|
86
|
+
this.#nodeNE?.clear();
|
|
87
|
+
this.#nodeSW?.clear();
|
|
88
|
+
this.#nodeSE?.clear();
|
|
89
|
+
|
|
90
|
+
this.#nodeNW = null;
|
|
91
|
+
this.#nodeNE = null;
|
|
92
|
+
this.#nodeSW = null;
|
|
93
|
+
this.#nodeSE = null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#tryInsertIntoChildren(x: number, y: number, value: I) {
|
|
97
|
+
return this.#nodeNW!.insert(x, y, value)
|
|
98
|
+
|| this.#nodeNE!.insert(x, y, value)
|
|
99
|
+
|| this.#nodeSW!.insert(x, y, value)
|
|
100
|
+
|| this.#nodeSE!.insert(x, y, value);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#subdivide() {
|
|
104
|
+
const boundaryHalfWidth = this.#boundary.width / 2;
|
|
105
|
+
const boundaryHalfHeight = this.#boundary.height / 2;
|
|
106
|
+
const nodeNextDepth = this.#depth + 1;
|
|
107
|
+
|
|
108
|
+
const northWestBoundary = new QuadTreeBoundary(
|
|
109
|
+
this.#boundary.x,
|
|
110
|
+
this.#boundary.y,
|
|
111
|
+
boundaryHalfWidth,
|
|
112
|
+
boundaryHalfHeight
|
|
113
|
+
);
|
|
114
|
+
this.#nodeNW = new QuadTree(northWestBoundary, this.#threshold, nodeNextDepth, this.#maxDepth);
|
|
115
|
+
|
|
116
|
+
const northEastBoundary = new QuadTreeBoundary(
|
|
117
|
+
this.#boundary.x + boundaryHalfWidth,
|
|
118
|
+
this.#boundary.y,
|
|
119
|
+
boundaryHalfWidth,
|
|
120
|
+
boundaryHalfHeight
|
|
121
|
+
);
|
|
122
|
+
this.#nodeNE = new QuadTree(northEastBoundary, this.#threshold, nodeNextDepth, this.#maxDepth);
|
|
123
|
+
|
|
124
|
+
const southWestBoundary = new QuadTreeBoundary(
|
|
125
|
+
this.#boundary.x,
|
|
126
|
+
this.#boundary.y + boundaryHalfHeight,
|
|
127
|
+
boundaryHalfWidth,
|
|
128
|
+
boundaryHalfHeight
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
this.#nodeSW = new QuadTree(southWestBoundary, this.#threshold, nodeNextDepth, this.#maxDepth);
|
|
132
|
+
|
|
133
|
+
const southEastBoundary = new QuadTreeBoundary(
|
|
134
|
+
this.#boundary.x + boundaryHalfWidth,
|
|
135
|
+
this.#boundary.y + boundaryHalfHeight,
|
|
136
|
+
boundaryHalfWidth,
|
|
137
|
+
boundaryHalfHeight
|
|
138
|
+
);
|
|
139
|
+
this.#nodeSE = new QuadTree(southEastBoundary, this.#threshold, nodeNextDepth, this.#maxDepth);
|
|
140
|
+
|
|
141
|
+
this.#divided = true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
class QuadTreeBoundary implements QuadTreeRegion {
|
|
146
|
+
#x: number;
|
|
147
|
+
#y: number;
|
|
148
|
+
#width: number;
|
|
149
|
+
#height: number;
|
|
150
|
+
|
|
151
|
+
#left: number;
|
|
152
|
+
#right: number;
|
|
153
|
+
#top: number;
|
|
154
|
+
#bottom: number;
|
|
155
|
+
|
|
156
|
+
constructor(x: number, y: number, width: number, height: number) {
|
|
157
|
+
this.#x = x;
|
|
158
|
+
this.#y = y;
|
|
159
|
+
this.#width = width;
|
|
160
|
+
this.#height = height;
|
|
161
|
+
|
|
162
|
+
this.#left = x;
|
|
163
|
+
this.#right = x + width;
|
|
164
|
+
this.#top = y;
|
|
165
|
+
this.#bottom = y + height;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
get x() {
|
|
169
|
+
return this.#x;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
get y() {
|
|
173
|
+
return this.#y;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
get width() {
|
|
177
|
+
return this.#width;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
get height() {
|
|
181
|
+
return this.#height;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
get left() {
|
|
185
|
+
return this.#left;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
get right() {
|
|
189
|
+
return this.#right;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
get top() {
|
|
193
|
+
return this.#top;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
get bottom() {
|
|
197
|
+
return this.#bottom;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
contains(x: number, y: number) {
|
|
201
|
+
return x >= this.#left
|
|
202
|
+
&& x < this.#right
|
|
203
|
+
&& y >= this.#top
|
|
204
|
+
&& y < this.#bottom;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
intersects(other: QuadTreeBoundary) {
|
|
208
|
+
return this.#left < other.#right
|
|
209
|
+
&& this.#right > other.#left
|
|
210
|
+
&& this.#top < other.#bottom
|
|
211
|
+
&& this.#bottom > other.#top;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
interface QuadTreeRegion {
|
|
216
|
+
contains(x: number, y: number): boolean;
|
|
217
|
+
intersects(boundary: QuadTreeBoundary): boolean;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
interface QuadTreeInsertable<I = unknown> {
|
|
221
|
+
x: number;
|
|
222
|
+
y: number;
|
|
223
|
+
value: I;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export {
|
|
227
|
+
QuadTree,
|
|
228
|
+
QuadTreeBoundary,
|
|
229
|
+
type QuadTreeRegion,
|
|
230
|
+
type QuadTreeInsertable
|
|
231
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zeitfall/quadtree",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Implementation of point-region quadtree algorithm",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "tsc --watch | node --watch ./dist/index.js",
|
|
17
|
+
"build": "tsc"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22.19.0"
|
|
21
|
+
},
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Vladyslav",
|
|
24
|
+
"email": "zzeitfall@gmail.com"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/zeitfall/quadtree.git"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "^6.0.3"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
// "rootDir": "./src",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "nodenext",
|
|
11
|
+
"target": "esnext",
|
|
12
|
+
"types": [],
|
|
13
|
+
// For nodejs:
|
|
14
|
+
// "lib": ["esnext"],
|
|
15
|
+
// "types": ["node"],
|
|
16
|
+
// and npm install -D @types/node
|
|
17
|
+
|
|
18
|
+
// Other Outputs
|
|
19
|
+
"sourceMap": true,
|
|
20
|
+
"declaration": true,
|
|
21
|
+
"declarationMap": true,
|
|
22
|
+
|
|
23
|
+
// Stricter Typechecking Options
|
|
24
|
+
"noUncheckedIndexedAccess": true,
|
|
25
|
+
"exactOptionalPropertyTypes": true,
|
|
26
|
+
|
|
27
|
+
// Style Options
|
|
28
|
+
"noImplicitReturns": true,
|
|
29
|
+
"noImplicitOverride": true,
|
|
30
|
+
"noUnusedLocals": true,
|
|
31
|
+
"noUnusedParameters": true,
|
|
32
|
+
// "noFallthroughCasesInSwitch": true,
|
|
33
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
34
|
+
|
|
35
|
+
// Recommended Options
|
|
36
|
+
"strict": true,
|
|
37
|
+
"verbatimModuleSyntax": true,
|
|
38
|
+
"isolatedModules": true,
|
|
39
|
+
"noUncheckedSideEffectImports": true,
|
|
40
|
+
"moduleDetection": "force",
|
|
41
|
+
"skipLibCheck": true,
|
|
42
|
+
}
|
|
43
|
+
}
|