Draw ASCII circles

by Yaroslav Samardak

HTML

<pre id="out"></pre>

CSS

body {
  background: #202731;
}

pre {
  color: #FFF;
  outline: none;
  width: 100%;
  height: 100%;
  line-height: 1;
  font-family: "Courier New", Courier, monospace;
  transform: scaleY(.8);
}

JavaScript

Number.prototype.clamp = function(min, max) {
  return Math.min(Math.max(this, min), max);
};

let canvas = []; // 32x24 1024x768

(function() {
	let row = ''
  for (let j = 0; j < 32; j++) {
		row += ' ';
  }
  for (let i = 0; i < 24; i++) {
		canvas[i] = row;
  }
})()

String.prototype.replaceAt=function(index, char) {
	return this.substr(0, index) + char + this.substr(index + char.length);
}

Number.prototype.x = function() { 
	return Math.round(32 * (this / 1024)) }
Number.prototype.y = function() { 
	return Math.round(24 * (this / 768)) }
Number.prototype.r = function() { 
	return Math.round(this / 32) }

function drawCircle(circleX, circleY, circleR, color) {
	function setPixel(x, y, color) {
  	if (isNaN(x) || isNaN(y) || x < 0 || x >= 32 || y < 0 || y >= 24) return;
  	canvas[y] = canvas[y].replaceAt(x, color);
  }

	function dcc(cx, cy, r) {
  	console.log("DCC", cx, cy, r, color);
		let r2 = r * r;
    let ly = 25;
		let dx, x, y, k;
        
		for (x = -r; x <= r; x++) {
    	y = Math.round(Math.sqrt(r2 - x * x));
      
    	if (ly != 25 && Math.abs(ly - y) > 1) {
        if (x < 0) dx = x - 1; else dx = x;
        
      	while (Math.abs(ly - y) != 1) {
        	if (ly < y) ly++; else ly--;
          setPixel(cx + dx, cy + ly, color);
	 		    setPixel(cx + dx, cy - ly, color);
        }
      }
      
      ly = y;
      setPixel(cx + x, cy + y, color);
      setPixel(cx + x, cy - y, color);
		}
  }

	dcc(circleX.x(), circleY.y(), circleR.r());
}

drawCircle(920, 510, 5, '☻');
drawCircle(920, 710, 17, '█');
drawCircle(622, 500, 16, '☻');
drawCircle(128, 128, 80, '█');
drawCircle(128, 512, 64, '☻');
drawCircle(521, 128, 128, '█');
drawCircle(512, 384, 256, '☻');

document.getElementById('out').innerText = canvas.join("\n");
console.log('---');