From 83b022bf6fbe73f472f0ed5829c7a47c8d1d2590 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Wed, 20 Aug 2025 17:10:58 +0900 Subject: [PATCH] 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. --- lib/extramath.js | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/extramath.js b/lib/extramath.js index 0b1556b..2a7254f 100644 --- a/lib/extramath.js +++ b/lib/extramath.js @@ -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;