From e4d3eaea18a0ab82dc6d7f31f84746f7efc6387d Mon Sep 17 00:00:00 2001 From: Ian Hogers Date: Fri, 1 Nov 2024 07:50:50 -0400 Subject: [PATCH 1/2] feat: make Rectangle static for server usage --- bun.lockb | Bin 41545 -> 41545 bytes src/index.ts | 1 + src/quad-tree.ts | 29 ++++++++++++++--------------- src/rectangle.ts | 20 ++++++++++---------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/bun.lockb b/bun.lockb index 00def7ce79e8163cb984c6161f910c749836e0a6..fd9a003200e6a4ae470d91945aa5aed302ccc800 100755 GIT binary patch delta 22 ecmX?kgz4lFrVXk2?2K^+dd7MNn{)E-8UX-ly9kv4 delta 22 acmX?kgz4lFrVXk2>`V+`usJ9Ht`PuUMF&X$ diff --git a/src/index.ts b/src/index.ts index 8926f3f..042e40b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,3 +7,4 @@ export { QuadTree, Rectangle }; + diff --git a/src/quad-tree.ts b/src/quad-tree.ts index 334d94f..21e8675 100644 --- a/src/quad-tree.ts +++ b/src/quad-tree.ts @@ -72,13 +72,13 @@ export class QuadTree { */ public retrieve(from: Rectangle | Point, logging: boolean = false): Array { if (from instanceof Point) { - if (!this.rect.contains(from)) { + if (!Rectangle.contains(this.rect, from)) { return []; } } if (from instanceof Rectangle) { - if (!this.rect.intersects(from)) { + if (!Rectangle.intersects(this.rect, from)) { return []; } } @@ -108,11 +108,11 @@ export class QuadTree { const intersectingNodes = this.nodes.filter((node) => { if (from instanceof Point) { - return node.contains(from); + return Rectangle.contains(node, from); } if (from instanceof Rectangle) { - return node.intersects(from); + return Rectangle.intersects(node, from); } return false; @@ -157,24 +157,24 @@ export class QuadTree { private _delete(at: Rectangle | Point): void { if (at instanceof Point) { - if (!this.rect.contains(at)) { + if (!Rectangle.contains(this.rect, at)) { return; } } if (at instanceof Rectangle) { - if (!this.rect.intersects(at)) { + if (!Rectangle.intersects(this.rect, at)) { return; } } this.nodes = this.nodes.filter((node) => { if (at instanceof Point) { - return !node.contains(at); + return !Rectangle.contains(node, at); } if (at instanceof Rectangle) { - return !node.intersects(at); + return !Rectangle.intersects(node, at); } return true; @@ -197,7 +197,7 @@ export class QuadTree { return; } - if (!this.rect.intersects(node)) { + if (!Rectangle.intersects(this.rect,node)) { if (!this.isRoot) return QUAD_CODES.NODE_NOT_WITHIN_BOUNDS; @@ -292,12 +292,11 @@ export class QuadTree { * Returns the number of children that the current node intersects with. */ private childIntersections(node: T) { - const intersections = [ - this.topLeft!.rect.intersects(node), - this.topRight!.rect.intersects(node), - this.bottomLeft!.rect.intersects(node), - this.bottomRight!.rect.intersects(node) - ] + const intersections: boolean[] = [] + if (this.topLeft) intersections.push(Rectangle.intersects(this.topLeft.rect, node)) + if (this.topRight) intersections.push(Rectangle.intersects(this.topRight.rect, node)) + if (this.bottomLeft) intersections.push(Rectangle.intersects(this.bottomLeft.rect, node)) + if (this.bottomRight) intersections.push(Rectangle.intersects(this.bottomRight.rect, node)) return intersections.filter(Boolean).length; } diff --git a/src/rectangle.ts b/src/rectangle.ts index 49fef97..c474114 100644 --- a/src/rectangle.ts +++ b/src/rectangle.ts @@ -15,21 +15,21 @@ export class Rectangle { public height: number ) {} - public contains(point: Point): boolean { + static contains(thisRect: Rectangle, point: Point): boolean { return ( - point.x >= this.x - this.width / 2 && - point.x <= this.x + this.width / 2 && - point.y >= this.y - this.height / 2 && - point.y <= this.y + this.height / 2 + point.x >= thisRect.x - thisRect.width / 2 && + point.x <= thisRect.x + thisRect.width / 2 && + point.y >= thisRect.y - thisRect.height / 2 && + point.y <= thisRect.y + thisRect.height / 2 ) } - public intersects(rect: Rectangle): boolean { + static intersects(thisRect:Rectangle, rect: Rectangle): boolean { return !( - rect.x - (rect.width / 2) > this.x + (this.width / 2) || - rect.x + (rect.width / 2) < this.x - (this.width / 2) || - rect.y - (rect.height / 2) > this.y + (this.height / 2) || - rect.y + (rect.height / 2) < this.y - (this.height / 2) + rect.x - (rect.width / 2) > thisRect.x + (thisRect.width / 2) || + rect.x + (rect.width / 2) < thisRect.x - (thisRect.width / 2) || + rect.y - (rect.height / 2) > thisRect.y + (thisRect.height / 2) || + rect.y + (rect.height / 2) < thisRect.y - (thisRect.height / 2) ) } From 14da3d810762111daa4a9274122cebaab8894859 Mon Sep 17 00:00:00 2001 From: Ian Hogers Date: Fri, 1 Nov 2024 08:27:36 -0400 Subject: [PATCH 2/2] feat[QuadTree.delete]: `shouldEmitEvent` param added for disabling events when manually deleting poitns --- src/quad-tree.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/quad-tree.ts b/src/quad-tree.ts index 21e8675..b418462 100644 --- a/src/quad-tree.ts +++ b/src/quad-tree.ts @@ -148,8 +148,9 @@ export class QuadTree { * Since this function performs an operation on the tree, it will also notify all subscribers that a delete operation has occured * and will provide the value passed to this function. */ - public delete(at: Rectangle | Point) { + public delete(at: Rectangle | Point, shouldEmitEvent: boolean = true) { this._delete(at); + if (!shouldEmitEvent) return; for (const subscriber of this.subscriptions) { subscriber({ type: "delete", node: at }); }