Skip to content

Instantly share code, notes, and snippets.

@vexdy
Created December 6, 2021 20:37
Show Gist options
  • Save vexdy/f0393c2d2e58de3bf6c4f447d472b4e4 to your computer and use it in GitHub Desktop.
Save vexdy/f0393c2d2e58de3bf6c4f447d472b4e4 to your computer and use it in GitHub Desktop.
Matrix Homework
function drawMatrix(size, defaultValue) {
return Array(size).fill(defaultValue).map(()=>Array(size).fill(defaultValue));
};
function fillRectangle(x, y, width, height, matrix) {
for (let i = x; i < x + width; i++) {
for (let j = y; j < y + height; j++) {
matrix[i][j] = 1;
};
};
return matrix;
};
function drawRectangle(x, y, width, height, matrix) {
matrix = drawHLine(x, y, width, matrix);
matrix = drawVLine(x, y, width, matrix);
matrix = drawHLine(x + height - 1, y, width, matrix);
matrix = drawVLine(x, y + height - 1, width, matrix);
return matrix;
};
function drawVLine(x, y, width, matrix) {
return fillRectangle(x, y, width, 1, matrix);
};
function drawHLine(x, y, height, matrix) {
return fillRectangle(x, y, 1, height, matrix);
};
function clearScreen(matrix) {
return drawMatrix(matrix.length, 0);
};
function printScreen(matrix) {
console.log(" " + "-".repeat(matrix.length));
for (let i = 0; i < matrix.length; i++) {
console.log("|" + matrix[i].map((el) => el == 0 ? " " : "*").join('') + "|");
};
console.log(" " + "-".repeat(matrix.length));
return matrix;
};
function main() {
let screen = drawMatrix(10, 0);
printScreen(screen);
screen = drawRectangle(2, 1, 3, 3, screen);
printScreen(screen);
screen = fillRectangle(2, 6, 3, 3, screen);
printScreen(screen);
screen = drawHLine(7, 1, 3, screen);
printScreen(screen);
screen = drawVLine(6, 7, 3, screen);
printScreen(screen);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment