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);
}
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
function gridScaleVaild(x, y) {
// Most grid sizes are above 10 and below 100
function gridSizeVaild(x, y) {
return x > 10 && y > 10 && x < 100 && y < 100;
}
export function gridSizeHeuristic(width, height) {
const div = dividers(width, height);
if (div.length > 0) {
let x = 1;
let y = 1;
// Check common scales but make sure the grid size is above 10 and below 100
for (let scale of commonGridScales) {
const tempX = Math.floor(width / scale);
const tempY = Math.floor(height / scale);
if (div.includes(scale) && gridScaleVaild(tempX, tempY)) {
x = tempX;
y = tempY;
// Find the best division by comparing the absolute z-scores of each axis
let bestX = 1;
let bestY = 1;
let bestScore = Number.MAX_VALUE;
for (let scale of div) {
const x = Math.floor(width / scale);
const y = Math.floor(height / scale);
const xScore = Math.abs((x - gridSizeMean.x) / gridSizeStd.x);
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)) {
return { x, y };
if (gridSizeVaild(bestX, bestY)) {
return { x: bestX, y: bestY };
}
}
return { x: 22, y: 22 };