Skip to content

Instantly share code, notes, and snippets.

@gcr
Last active April 12, 2024 14:08
Show Gist options
  • Save gcr/1071fddfc5944d1d1248ba12ee30d6a4 to your computer and use it in GitHub Desktop.
Save gcr/1071fddfc5944d1d1248ba12ee30d6a4 to your computer and use it in GitHub Desktop.

donut.c, but it's in jq

Run this in your terminal:

                  jq -nr 'def R(A;B;C
            ):range(A;B;C);def R(A):range
          (A);30as$s|1as$R1|2as$R2|7as$K2|(
        $s*$K2*3/(8*($R1+$R2)))as$K1|def t($A;$B
     ):($A|cos)as$cA|($B|cos)as$cB|($A|sin)as$sA|($B
    |sin)as$sB|R(0;2*3.1415;0.1)as$theta|($theta|cos
    )as$ct|($theta|sin)as$st|R(0;2*3.1415;0.05)as$phi|
   ($phi|cos)as$cp|($phi|sin)as$sp|($R2+$R1*$ct)as$cx|(
   $R1*$st)as$cy|{x:($cx*($cB*$cp+$sA*$sB*$sp)-$cy*$cA*
  $sB),y:($cx*($sB*$cp-$sA    *$cB*$sp)+$cy*$cA*$cB),z:(
 $K2+$cA*$cx*$sp+$cy             *$sA),ooz:(1/($K2+$cA*
$cx*$sp+$cy*$sA+0.01              )),L:($cp*$ct*$sB-$cA*
$ct*$sp-$sA*$st+$cB*              ($cA*$st-$ct*$sA*$sp))};
def r:def _d($dat;$pt             ):($s/2+$K1*$pt.ooz*
$pt.x|floor)as$xp|($s             /2-$K1*$pt.ooz*$pt
 .y|floor)as$yp|if($pt.L         <=0or $pt.ooz<=$dat
  .z[$yp][$xp])then$dat else{z:($dat.z|.[$yp][$xp]=
   $pt.ooz),o:($dat.o|.[$yp][$xp]=(".,-~:;=!*#$@"[
   $pt.L*8:$pt.L*8]))}end;reduce.[]as$p({o:[R($s)|
     [R($s)|" "]],z:[R($s)|[R($s)|0]]};_d(.;$p))|
         .o|.[]|join("");foreach repeat(0)as$_
           ({A:1,B:1};{A:(.A+0.07),B:(.B+
                 0.03)};[t(.A;.B)]|r)'

Example output:

a spinning donut in your terminal

@Rudxain
Copy link

Rudxain commented Sep 6, 2022

Nice! (Posting this because GH doesn't let me react with a like)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment