Skip to content

Instantly share code, notes, and snippets.

@quentint
Created September 18, 2023 08:58
Show Gist options
  • Save quentint/e25e5aebb87dfa57f6d021beec942c9c to your computer and use it in GitHub Desktop.
Save quentint/e25e5aebb87dfa57f6d021beec942c9c to your computer and use it in GitHub Desktop.
import {encode} from "uqr"
let message = 'Lorem'
const {data: matrix} = encode(message)
function getSafeMatrixItemFlag(matrix: Array<Array<boolean>>, i: number, j: number): boolean {
if (!matrix[i]) {
return false
}
return matrix[i][j] || false
}
function splitBooleanMatrixIntoSubMatrices(matrix: Array<Array<boolean>>, subMatrixWidth: number = 2, subMatrixHeight = 3): boolean[][][] {
const subMatrices: boolean[][][] = []
for (let i = 0; i < matrix.length; i += subMatrixWidth) {
const subMatrix: boolean[][] = []
for (let j = 0; j < matrix.length; j += subMatrixHeight) {
const subSubMatrix: boolean[] = []
for (let k = 0; k < subMatrixWidth; k++) {
for (let l = 0; l < subMatrixHeight; l++) {
subSubMatrix.push(getSafeMatrixItemFlag(matrix, i + k, j + l))
}
}
subMatrix.push(subSubMatrix)
}
subMatrices.push(subMatrix)
}
return subMatrices
}
console.log(splitBooleanMatrixIntoSubMatrices(matrix))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment