Skip to content

Instantly share code, notes, and snippets.

@sebinsua
Last active August 22, 2022 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebinsua/31ef9766d47e65920623ba4d8c6fc72a to your computer and use it in GitHub Desktop.
Save sebinsua/31ef9766d47e65920623ba4d8c6fc72a to your computer and use it in GitHub Desktop.
function* unroll(matrix) {
const height = matrix.length;
const width = matrix[0].length;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
yield { y, x };
}
}
}
function print(matrix) {
const height = matrix.length;
console.log();
for (let y = 0; y < height; y++) {
console.log(matrix[y].map((v) => (v ? v : "-")).join(" "));
}
console.log();
}
function mirror(matrix, { y, x } = {}) {
const height = matrix.length;
const width = matrix[0].length;
const mirrorPoint = {
y: height - 1 - y,
x: width - 1 - x
};
return mirrorPoint;
}
//
// 1 2 3
// 4 5 6
// 7 8 9
//
// becomes:
//
// 7 4 1
// 8 5 2
// 9 6 3
//
// therefore:
//
// 0, 0 => 0, 2 (+ 0, + 2)
// 0, 1 => 1, 2 (+ 1, + 1)
// 0, 2 => 2, 2 (+ 2, 0)
// 1, 0 => 0, 1 (- 1, + 1)
// 1, 1 => 1, 1 ( 0, 0)
// 1, 2 => 2, 1 (+ 1, - 1)
// 2, 0 => 0, 0 (- 2, 0)
// 2, 1 => 1, 0 (- 1, - 1)
// 2, 2 => 2, 0 ( 0, - 2)
function rotate(matrix, { y, x } = {}) {
const height = matrix.length;
// const width = matrix[0].length;
const rotatedPoint = {
y: x,
x: height - y - 1
};
// console.log({ y, x }, ` => `, rotatedPoint);
return rotatedPoint;
}
// rotate 180 degrees
function mirrorImage(matrix) {
const height = matrix.length;
const width = matrix[0].length;
const newMatrix = Array(height)
.fill(() => Array(width).fill(undefined))
.map((createRow) => createRow());
const seen = new Set();
for (const { y, x } of unroll(matrix)) {
const { y: y2, x: x2 } = mirror(matrix, { y, x });
if (seen.has(`y:${y}, x:${x}`)) {
continue;
}
[newMatrix[y][x], newMatrix[y2][x2]] = [matrix[y2][x2], matrix[y][x]];
seen.add(`y:${y2}, x:${x2}`);
}
return newMatrix;
}
// rotate 90 degrees
function rotateImage(matrix) {
const height = matrix.length;
const width = matrix[0].length;
const newMatrix = Array(width)
.fill(() => Array(height).fill(undefined))
.map((createRow) => createRow());
for (const { y, x } of unroll(matrix)) {
const { y: y2, x: x2 } = rotate(matrix, { y, x });
newMatrix[y2][x2] = matrix[y][x];
}
return newMatrix;
}
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
print(matrix);
print(rotateImage(matrix));
const lmao = [
[100, 200, 300, 400],
[80, 150, 250, 350],
[60, 100, 200, 300],
[30, 50, 80, 240]
];
print(lmao);
print(rotateImage(lmao));
const lopsided = [
[1, 2, 3, 10, 12],
[4, 5, 6, 11, 13],
[7, 8, 9, 15, 17]
];
print(lopsided);
print(rotateImage(lopsided));
// const rotatedLopsided = [
// [7, 4, 1],
// [8, 5, 2],
// [9, 6, 3],
// [15, 11, 10],
// [17, 13, 12]
// ];
// console.log(rotate(matrix, { y: 0, x: 0 }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment