MCPcopy Create free account
hub / github.com/32comic/image2pixel-web / calculateSizeStatistics

Method calculateSizeStatistics

pixelizer.js:350–371  ·  view source on GitHub ↗
(sizes)

Source from the content-addressed store, hash-verified

348 }
349
350 calculateSizeStatistics(sizes) {
351 if (sizes.length === 0) return { mode: 1, gcd: 1, variance: 0 };
352
353 // 计算众数
354 const frequency = {};
355 sizes.forEach(size => {
356 frequency[size] = (frequency[size] || 0) + 1;
357 });
358
359 const mode = parseInt(Object.keys(frequency).reduce((a, b) =>
360 frequency[a] > frequency[b] ? a : b
361 ));
362
363 // 计算GCD
364 const gcd = sizes.reduce((a, b) => this.gcd(a, b));
365
366 // 计算方差
367 const mean = sizes.reduce((a, b) => a + b) / sizes.length;
368 const variance = sizes.reduce((sum, size) => sum + Math.pow(size - mean, 2), 0) / sizes.length;
369
370 return { mode, gcd, variance, sizes };
371 }
372
373 gcd(a, b) {
374 return b === 0 ? a : this.gcd(b, a % b);

Callers 2

Calls 1

gcdMethod · 0.95

Tested by

no test coverage detected