Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function calculateDiamond(matrix, chunkSize, randomFactor) {
let sumComponents = 0;
let sum = 0;
for (let i = 0; i < matrix.length - 1; i += chunkSize) {
for (let j = 0; j < matrix.length - 1; j += chunkSize) {
const BOTTOM_RIGHT = matrix[j + chunkSize]
? matrix[j + chunkSize][i + chunkSize]
: null;
const BOTTOM_LEFT = matrix[j + chunkSize]
? matrix[j + chunkSize][i]
: null;
const TOP_LEFT = matrix[j][i];
const TOP_RIGHT = matrix[j][i + chunkSize];
const { count, sum } = [
BOTTOM_RIGHT,
BOTTOM_LEFT,
TOP_LEFT,
TOP_RIGHT
].reduce(
(result, value) => {
if (isFinite(value) && value != null) {
result.sum += value;
result.count += 1;
}
return result;
},
{ sum: 0, count: 0 }
);
const changed = {row: j + chunkSize / 2, column: i + chunkSize / 2};
matrix[changed.row][changed.column] =
sum / count + randomInRange(-randomFactor, randomFactor);
}
}
return matrix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment