Updated grid size heuristic to use statistical model

This commit is contained in:
Mitchell McCaffrey 2020-10-10 16:58:41 +11:00
parent fa659bc80a
commit 57ac662468

View File

@ -29,30 +29,36 @@ function dividers(a, b) {
return factors(d); return factors(d);
} }
const commonGridScales = [35, 70, 72, 111, 140, 144, 300]; // The mean and standard deviation of > 1500 maps from the web
const gridSizeMean = { x: 31.567792, y: 32.597987 };
const gridSizeStd = { x: 14.438842, y: 15.582376 };
// Most grid scales are above 10 and below 100 // Most grid sizes are above 10 and below 100
function gridScaleVaild(x, y) { function gridSizeVaild(x, y) {
return x > 10 && y > 10 && x < 100 && y < 100; return x > 10 && y > 10 && x < 100 && y < 100;
} }
export function gridSizeHeuristic(width, height) { export function gridSizeHeuristic(width, height) {
const div = dividers(width, height); const div = dividers(width, height);
if (div.length > 0) { if (div.length > 0) {
let x = 1; // Find the best division by comparing the absolute z-scores of each axis
let y = 1; let bestX = 1;
// Check common scales but make sure the grid size is above 10 and below 100 let bestY = 1;
for (let scale of commonGridScales) { let bestScore = Number.MAX_VALUE;
const tempX = Math.floor(width / scale); for (let scale of div) {
const tempY = Math.floor(height / scale); const x = Math.floor(width / scale);
if (div.includes(scale) && gridScaleVaild(tempX, tempY)) { const y = Math.floor(height / scale);
x = tempX; const xScore = Math.abs((x - gridSizeMean.x) / gridSizeStd.x);
y = tempY; const yScore = Math.abs((y - gridSizeMean.y) / gridSizeStd.y);
if (xScore < bestScore || yScore < bestScore) {
bestX = x;
bestY = y;
bestScore = Math.min(xScore, yScore);
} }
} }
if (gridScaleVaild(x, y)) { if (gridSizeVaild(bestX, bestY)) {
return { x, y }; return { x: bestX, y: bestY };
} }
} }
return { x: 22, y: 22 }; return { x: 22, y: 22 };