From 717b141cb7151d5a004ed286b34ede520aefc528 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 18 Aug 2025 04:16:52 +0900 Subject: [PATCH] Add clusteredCellsDensity function to extramath.js Introduces clusteredCellsDensity to evaluate density of selected cells within a grid. Updates module version to 1.0.0 and exports the new function for external use. --- lib/extramath.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/extramath.js b/lib/extramath.js index 8076ff7..db416fd 100644 --- a/lib/extramath.js +++ b/lib/extramath.js @@ -70,13 +70,40 @@ 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 + + // 1. Convert cell number → (x,y) coordinates + var coords = numbers.map(function (n) { + return { x: n % size, y: Math.floor(n / size) }; + }); + + var xs = coords.map(function (c) { return c.x; }); + var ys = coords.map(function (c) { return c.y; }); + + // 2. Compute bounding box of selected cells + var minX = Math.min.apply(Math, xs), maxX = Math.max.apply(Math, xs); + var minY = Math.min.apply(Math, ys), maxY = Math.max.apply(Math, ys); + + // 3. Compute area and density + var w = maxX - minX + 1; + var h = maxY - minY + 1; + var boxArea = w * h; + + var density = coords.length / boxArea; + return density >= minDensity; +} + exports.DTM = DTM; exports.arrayCos = arrayCos; exports.measureSimilarity = measureSimilarity; exports.export_measureSimilarity = export_measureSimilarity; exports.cartesianProduct = cartesianProduct; +exports.clusteredCellsDensity = clusteredCellsDensity; -exports.VERSIONINFO = "ExtraMath module (extramath.js) version 0.0.5"; +exports.VERSIONINFO = "ExtraMath module (extramath.js) version 1.0.0"; exports.AUTHOR = "gnh1201@catswords.re.kr"; exports.global = global; exports.require = global.require;