'use strict'; class AABB { constructor(x, y, width, height) { this.x = x; this.y = y; this.width = width; this.height = height; } contains(point) { if (point.x >= this.x && point.y >= this.y && point.x < this.x + this.width && point.y < this.y + this.height) return true; return false; } intersects(aabb) { const overlap = (ax, arange, bx, brange) => { const range = ax < bx ? arange : brange; if (Math.abs(bx - ax) < range) return true; return false; }; if (overlap(this.x, this.width, aabb.x, aabb.width) && overlap(this.y, this.height, aabb.y, aabb.height)) return true; return false; } } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * QUADTREE * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ class QTNodeType { constructor(name) { this.name = name; } toString() { return `QTNode.${this.name}`; } } class QTNode { static Empty = new QTNodeType('Empty'); static Leaf = new QTNodeType('Leaf'); static Branch = new QTNodeType('Branch'); constructor(x, y, width, height) { this.aabb = new AABB(x, y, width, height); this.type = QTNode.Empty; } insert(point) { if (this.aabb.contains(point)) { this.type = QTNode.Leaf; this.point = point; return true; } return false; } } export { AABB, QTNode };