class GeoDictionaryViz {
constructor(decimals = 0) {
this.resolution = 10 ** decimals;
}
add(x, y, val) {
const key = [Math.floor(x * this.resolution), Math.floor(y * this.resolution)];
const tile = this[key];
if (tile) {
tile.push([x, y, val]);
} else {
this[key] = [[x, y, val]];
}
}
getClosest(x, y) {
const t0 = performance.now();
const xKey = Math.floor(x * this.resolution);
const yKey = Math.floor(y * this.resolution);
let minDist = Infinity;
let val = null;
let iterations = 0;
const visited = [];
for (let i = xKey - 1; i <= xKey + 1; i++) {
for (let j = yKey - 1; j <= yKey + 1; j++) {
const tile = this[[i, j]];
if(!tile) continue;
tile.forEach(t => {
iterations++;
const deltaX = t[0] - x;
const deltaY = t[1] - y;
const dist = deltaX * deltaX + deltaY * deltaY;
if(dist > 1 * this.resolution) return;
visited.push(t[2]);
if (dist < minDist) {
minDist = dist;
val = t[2];
}
});
}
}
const t1 = performance.now();
return {
iterations,
visited,
targeted: val,
performance: (t1 - t0),
};
}
}