Advertisement
Tulanir

Animated "raytraced" sphere in unicode Braille (run in console)

Jan 17th, 2022 (edited)
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // WARNING edit: this has no framerate limit and will genuinely draw a lot of electrical power.
  2. // press CTRL+C to stop animation! (or just close window)
  3. // if you get question marks in boxes, you may need to run it in smth like Git Bash
  4. w = 52; // If the sphere looks squished on your screen, change width and height here
  5. h = 27;
  6. iter = 2000; // increase iter to slow down animation, decrease to speed up
  7. dot = (u, v) => u.reduce((a, x, i)=>a + x*v[i], 0);
  8. PI = Math.PI;
  9. f = "⠈⠑⠋⠛⠽⠿".split("");
  10. for(;;){
  11.     for(i=0; i < iter; i++){
  12.         S=[];
  13.         v = [-1/2, Math.cos(2*PI*i/iter), Math.sin(2*PI*i/iter)];
  14.         v = v.map(x => x/dot(v,v));
  15.         for(y = 0; y < h; y++){
  16.             for(x = 0; x < w; x++){
  17.                 [p, t] = [x/w, y/h].map(e => e*PI/2 + PI/4);
  18.                 [x1, y1, z1] = [0, 0, -1.6];
  19.                 [x2, y2, z2] = [Math.sin(p)*Math.cos(t), Math.cos(p), Math.sin(p)*Math.sin(t)];
  20.                 a = x2*x2 + y2*y2 + z2*z2;
  21.                 b = -2*(-x1*x2 - y1*y2 - z1*z2);
  22.                 c = x1*x1 + y1*y1 + z1*z1-1;
  23.                 d = b*b - 4*a*c;
  24.                 if(d >= 0) {
  25.                     u = (-b + Math.sqrt(d))/2/a;
  26.                     n = [x1 + x2*u, y1 + y2*u, z1 + z2*u];
  27.                     r = Math.acos(dot(n,v));
  28.                     S.push(f[Math.floor(r*6/PI)]);
  29.                 }
  30.                 else S.push("⠀");
  31.             }
  32.             S.push("\n");
  33.         }
  34.         console.log(S.join(""));
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement