UNPKG

3.49 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.QuadTree = void 0;
4const Rectangle_1 = require("./Rectangle");
5const Circle_1 = require("./Circle");
6const CircleWarp_1 = require("./CircleWarp");
7const NumberUtils_1 = require("./NumberUtils");
8class QuadTree {
9 constructor(rectangle, capacity) {
10 this.rectangle = rectangle;
11 this.capacity = capacity;
12 this.points = [];
13 this.divided = false;
14 }
15 subdivide() {
16 const x = this.rectangle.position.x;
17 const y = this.rectangle.position.y;
18 const w = this.rectangle.size.width;
19 const h = this.rectangle.size.height;
20 const capacity = this.capacity;
21 this.northEast = new QuadTree(new Rectangle_1.Rectangle(x, y, w / 2, h / 2), capacity);
22 this.northWest = new QuadTree(new Rectangle_1.Rectangle(x + w / 2, y, w / 2, h / 2), capacity);
23 this.southEast = new QuadTree(new Rectangle_1.Rectangle(x, y + h / 2, w / 2, h / 2), capacity);
24 this.southWest = new QuadTree(new Rectangle_1.Rectangle(x + w / 2, y + h / 2, w / 2, h / 2), capacity);
25 this.divided = true;
26 }
27 insert(point) {
28 var _a, _b, _c, _d, _e;
29 if (!this.rectangle.contains(point.position)) {
30 return false;
31 }
32 if (this.points.length < this.capacity) {
33 this.points.push(point);
34 return true;
35 }
36 if (!this.divided) {
37 this.subdivide();
38 }
39 return ((_e = (((_a = this.northEast) === null || _a === void 0 ? void 0 : _a.insert(point)) ||
40 ((_b = this.northWest) === null || _b === void 0 ? void 0 : _b.insert(point)) ||
41 ((_c = this.southEast) === null || _c === void 0 ? void 0 : _c.insert(point)) ||
42 ((_d = this.southWest) === null || _d === void 0 ? void 0 : _d.insert(point)))) !== null && _e !== void 0 ? _e : false);
43 }
44 queryCircle(position, radius) {
45 return this.query(new Circle_1.Circle(position.x, position.y, radius));
46 }
47 queryCircleWarp(position, radius, containerOrSize) {
48 const container = containerOrSize;
49 const size = containerOrSize;
50 return this.query(new CircleWarp_1.CircleWarp(position.x, position.y, radius, container.canvas !== undefined ? container.canvas.size : size));
51 }
52 queryRectangle(position, size) {
53 return this.query(new Rectangle_1.Rectangle(position.x, position.y, size.width, size.height));
54 }
55 query(range, found) {
56 var _a, _b, _c, _d;
57 const res = found !== null && found !== void 0 ? found : [];
58 if (!range.intersects(this.rectangle)) {
59 return [];
60 }
61 else {
62 for (const p of this.points) {
63 if (!range.contains(p.position) && (0, NumberUtils_1.getDistance)(range.position, p.position) > p.particle.getRadius()) {
64 continue;
65 }
66 res.push(p.particle);
67 }
68 if (this.divided) {
69 (_a = this.northEast) === null || _a === void 0 ? void 0 : _a.query(range, res);
70 (_b = this.northWest) === null || _b === void 0 ? void 0 : _b.query(range, res);
71 (_c = this.southEast) === null || _c === void 0 ? void 0 : _c.query(range, res);
72 (_d = this.southWest) === null || _d === void 0 ? void 0 : _d.query(range, res);
73 }
74 }
75 return res;
76 }
77}
78exports.QuadTree = QuadTree;