mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-12-07 23:03:40 +00:00
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:
parent
ab869556c4
commit
83b022bf6f
|
|
@ -72,13 +72,22 @@ function cartesianProduct(arr) {
|
||||||
|
|
||||||
function clusteredCellsDensity(numbers, size, minDensity) {
|
function clusteredCellsDensity(numbers, size, minDensity) {
|
||||||
if (!numbers || !numbers.length) return false;
|
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 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 maxIndex = size * size; // because it's 1-based (1..size*size)
|
||||||
var coords = numbers.map(function (n) {
|
|
||||||
return { x: n % size, y: Math.floor(n / 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 xs = coords.map(function (c) { return c.x; });
|
||||||
var ys = coords.map(function (c) { return c.y; });
|
var ys = coords.map(function (c) { return c.y; });
|
||||||
|
|
@ -96,14 +105,17 @@ function clusteredCellsDensity(numbers, size, minDensity) {
|
||||||
return density >= minDensity;
|
return density >= minDensity;
|
||||||
}
|
}
|
||||||
|
|
||||||
function tileStartPos(index, size, cols) {
|
function estimateTileStartPosition(index, tiles, spreadSize, gap) {
|
||||||
if (typeof size !== 'number') size = 130;
|
var tileSize = (spreadSize - gap * (tiles - 1)) / tiles;
|
||||||
if (typeof cols !== 'number') cols = 4;
|
|
||||||
|
|
||||||
var i = index - 1; // convert 1-based -> 0-based
|
var i = index - 1;
|
||||||
var col = i % cols;
|
var col = i % tiles;
|
||||||
var row = Math.floor(i / cols);
|
var row = Math.floor(i / tiles);
|
||||||
return { x: col * size, y: row * size };
|
|
||||||
|
return {
|
||||||
|
x: col * (tileSize + gap),
|
||||||
|
y: row * (tileSize + gap)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.DTM = DTM;
|
exports.DTM = DTM;
|
||||||
|
|
@ -112,9 +124,9 @@ exports.measureSimilarity = measureSimilarity;
|
||||||
exports.export_measureSimilarity = export_measureSimilarity;
|
exports.export_measureSimilarity = export_measureSimilarity;
|
||||||
exports.cartesianProduct = cartesianProduct;
|
exports.cartesianProduct = cartesianProduct;
|
||||||
exports.clusteredCellsDensity = clusteredCellsDensity;
|
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.AUTHOR = "gnh1201@catswords.re.kr";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = global.require;
|
exports.require = global.require;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user