Skip to content

Commit

Permalink
Create donut.js
Browse files Browse the repository at this point in the history
  • Loading branch information
znareak authored Nov 6, 2024
1 parent b3855d5 commit 9a33c99
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Javascript Algorithms/donut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//https://www.a1k0n.net/2011/07/20/donut-math.html

const sin = Math.sin;
const cos = Math.cos;

let A = 0, B = 0;
const renderDonut = () => {
const b = [];
const z = [];
for (let k = 0; k < 1760; k++) {
b[k] = ' ';
z[k] = 0;
}

for (let j = 0; j < 6.28; j += 0.07) {
for (let i = 0; i < 6.28; i += 0.02) {
const c = sin(i),
d = cos(j),
e = sin(A),
f = sin(j),
g = cos(A),
h = d + 2,
D = 1 / (c * h * e + f * g + 5),
l = cos(i),
m = cos(B),
n = sin(B),
t = c * h * g - f * e;

const x = Math.floor(40 + 30 * D * (l * h * m - t * n));
const y = Math.floor(12 + 15 * D * (l * h * n + t * m));
const o = x + 80 * y;
const N = Math.floor(8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n));

if (y > 0 && y < 22 && x > 0 && x < 80 && D > z[o]) {
z[o] = D;
b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0];
}
}
}

console.clear();
let output = '';
for (let k = 0; k < 1760; k++) {
output += k % 80 ? b[k] : '\n';
}
console.log(output);

A += 0.04;
B += 0.02;
};

setInterval(renderDonut, 120);

0 comments on commit 9a33c99

Please sign in to comment.