Skip to content

Instantly share code, notes, and snippets.

@EchoZhaoH
Last active August 18, 2021 09:32
Show Gist options
  • Save EchoZhaoH/d9705f6fd34c9611cf78fd96a3249ca4 to your computer and use it in GitHub Desktop.
Save EchoZhaoH/d9705f6fd34c9611cf78fd96a3249ca4 to your computer and use it in GitHub Desktop.
print with matrix
function drawCicle(diameter = 3, placeholder='*') {
diameter = Math.floor(diameter)
diameter = diameter < 3 ? 3 : diameter
const mtx = matrix(diameter)
return translate(mtx, placeholder)
}
function matrix(len) {
const m = len
const n = len
const g = function (l) {
return new Array(l).fill(0)
}
return new Array(n).fill(0).map(() => g(m))
}
function translate(mtx, placeholder) {
const len = mtx.length
const mid = Math.ceil(len/2)
const m = mtx.map((tx, i) => {
const dis = Math.abs(i + 1 - mid)
return tx.map((x, j) => {
const c = j + 1
if (c < mid) {
return c <= dis ? 0 : 1
} else if (c === mid) {
return 1
} else {
const d = len - c
return d < dis ? 0 : 1
}
}).map(x => x === 0 ? ' ' : placeholder).join('')+'\n'
}).join('')
return m
}
console.log(drawCicle(7, 'o'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment