如何在控制台中记录一个给定半径的圆?

Chr*_*ast 0 javascript algorithm math

我正在尝试使用我选择的符号在控制台中绘制一个圆圈,例如:'*',这是想要的结果:

drawCircle(5); //now I want to draw a circle with a radius of 5

    **
 *      *
*        *
 *       *
  *    *
    **
//this is a very ugly circle, but you probably get my idea.
Run Code Online (Sandbox Code Playgroud)

这是我迄今为止尝试过的:

const drawCirle = (radius) =>{
    const thickness = 0.4;
    const symbol = '*';
    const rin = radius - thickness
    const rout = radius + thickness;
    for(let y = radius; y >= -radius; --y){
        for(let x = -radius; x < rout; x += 0.5){
            let value = x * x + y * y;
            if(value >= rin * rin  && value <= rout * rout){
                console.log(symbol);

            }
            else{
                console.log(" ");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,这应该可行,但它只是随机记录一堆符号,就像这样:

*

*
*

*
*
*
// and so on...
Run Code Online (Sandbox Code Playgroud)

你能告诉我我做错了什么吗,如果你有更好的算法的建议,我会很高兴。PS我想用javascript制作它,所以它可以在浏览器中使用。

小智 5

每个控制台日志都打印在下一行,因此您必须将整行存储在例如单个变量中,并在您拥有整行后打印它。

const drawCircle = (radius) => {
    const thickness = 0.4;
    const symbol = '*';
    const rin = radius - thickness
    const rout = radius + thickness;
    for (let y = radius; y >= -radius; --y) {
        let string = '';
        for (let x = -radius; x < rout; x += 0.5) {
            const value = x * x + y * y;
            if (value >= rin * rin && value <= rout * rout) {
                string += symbol;
            } else {
                string += " ";
            }
        }
        console.log(string);
    }
}
drawCircle(5);
Run Code Online (Sandbox Code Playgroud)