Refactor grid functions and update exports in extramath.js

Improved clusteredCellsDensity to validate cell indices and refactored coordinate calculation to be 1-based. Replaced tileStartPos with estimateTileStartPosition for more flexible tile positioning, updated exports accordingly, and bumped VERSIONINFO to 1.0.3.
This commit is contained in:
Namhyeon Go 2025-08-20 17:10:58 +09:00
parent ab869556c4
commit 83b022bf6f

View File

@ -72,13 +72,22 @@ function cartesianProduct(arr) {
function clusteredCellsDensity(numbers, size, minDensity) {
if (!numbers || !numbers.length) return false;
if (typeof minDensity === 'undefined') minDensity = 0.6;
if (typeof size !== 'number' || size <= 0) size = 4; // default grid size = 4
if (typeof minDensity === 'undefined') minDensity = 0.6;
// 1. Convert cell number → (x,y) coordinates
var coords = numbers.map(function (n) {
return { x: n % size, y: Math.floor(n / size) };
});
var maxIndex = size * size; // because it's 1-based (1..size*size)
// 1. Convert cell number → (x,y) coordinates with 1-based check
var coords = [];
for (var i = 0; i < numbers.length; i++) {
var n = numbers[i];
// must be within 1..maxIndex
if (typeof n !== 'number' || n < 1 || n > maxIndex) {
return false; // invalid index -> immediately return false
}
var idx = n - 1; // shift to 0-based
coords.push({ x: idx % size, y: Math.floor(idx / size) });
}
var xs = coords.map(function (c) { return c.x; });
var ys = coords.map(function (c) { return c.y; });
@ -96,14 +105,17 @@ function clusteredCellsDensity(numbers, size, minDensity) {
return density >= minDensity;
}
function tileStartPos(index, size, cols) {
if (typeof size !== 'number') size = 130;
if (typeof cols !== 'number') cols = 4;
function estimateTileStartPosition(index, tiles, spreadSize, gap) {
var tileSize = (spreadSize - gap * (tiles - 1)) / tiles;
var i = index - 1; // convert 1-based -> 0-based
var col = i % cols;
var row = Math.floor(i / cols);
return { x: col * size, y: row * size };
var i = index - 1;
var col = i % tiles;
var row = Math.floor(i / tiles);
return {
x: col * (tileSize + gap),
y: row * (tileSize + gap)
};
}
exports.DTM = DTM;
@ -112,9 +124,9 @@ exports.measureSimilarity = measureSimilarity;
exports.export_measureSimilarity = export_measureSimilarity;
exports.cartesianProduct = cartesianProduct;
exports.clusteredCellsDensity = clusteredCellsDensity;
exports.tileStartPos = tileStartPos;
exports.estimateTileStartPosition = estimateTileStartPosition;
exports.VERSIONINFO = "ExtraMath module (extramath.js) version 1.0.1";
exports.VERSIONINFO = "ExtraMath module (extramath.js) version 1.0.3";
exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global;
exports.require = global.require;