class Node {
constructor(bottomLeftPoint, topRightPoint) {
this.point = null
this.range = new RectangularRange(bottomLeftPoint, topRightPoint)
this.hasBeenPopulated = false
this.topLeftQuad = null
this.topRightQuad = null
this.bottomLeftQuad = null
this.bottomRightQuad = null
this.vertialLine = null
this.horizontalLine = null
}
populateQuads() {
const tr = this.range.topRightPoint
const bl = this.range.bottomLeftPoint
const a = (tr.x - bl.x) / 2
this.hasBeenPopulated = true
this.topLeftQuad = new Node(new Point(bl.x, bl.y + a), new Point(bl.x + a, tr.y))
this.topRightQuad = new Node(new Point(bl.x + a, bl.y + a), tr)
this.bottomLeftQuad = new Node(bl, new Point(bl.x + a, bl.y + a))
this.bottomRightQuad = new Node(new Point(bl.x + a, bl.y), new Point(tr.x, bl.y + a))
this.vertialLine = [[this.bottomRightQuad.range.bottomLeftPoint.x,
this.bottomRightQuad.range.bottomLeftPoint.y],
[this.topRightQuad.range.bottomLeftPoint.x,
this.topRightQuad.range.topRightPoint.y]]
this.horizontalLine = [[this.topLeftQuad.range.bottomLeftPoint.x,
this.topLeftQuad.range.bottomLeftPoint.y],
[this.topRightQuad.range.topRightPoint.x,
this.topRightQuad.range.bottomLeftPoint.y]]
}
isPointInRange(point) {
return this.range.isPointIn(point, this.bottomLeftPoint, this.topRightPoint)
}
insertIntoQuad(point) {
if (!this.hasBeenPopulated) {
throw "Node is not populated."
}
if (this.topLeftQuad.isPointInRange(point)) {
insertPoint(this.topLeftQuad, point)
} else if (this.topRightQuad.isPointInRange(point)) {
insertPoint(this.topRightQuad, point)
} else if (this.bottomLeftQuad.isPointInRange(point)) {
insertPoint(this.bottomLeftQuad, point)
} else if (this.bottomRightQuad.isPointInRange(point)) {
insertPoint(this.bottomRightQuad, point)
}
}
}