Skip to content

Instantly share code, notes, and snippets.

@auselen
Created February 13, 2017 16:28
Show Gist options
  • Save auselen/a28c843cef4febe492f2031dbea51297 to your computer and use it in GitHub Desktop.
Save auselen/a28c843cef4febe492f2031dbea51297 to your computer and use it in GitHub Desktop.
Rendering a sphere with AWK

Rendering a sphere with AWK

Sphere

sphere.gif

Code

LC_ALL=ISO-8859-1 awk 'BEGIN{ \
  pi=atan2(0,-1); r=128; \
  for (j=0; j<360; j+=10) { \
    lx=cos(pi/180*j); ly=sin(pi/180*j); lz=2; lm=sqrt(lx*lx+ly*ly+lz*lz); \
    print "P6"; print r*2,r*2; print "255"; \
    for (y=-r; y<r; y++) \
      for (x=-r; x<r; x++) \
        if (x*x+y*y < r*r) { \
          z=sqrt(r*r-x*x-y*y); a=(x*lx+y*ly+z*lz)/(r*lm); \
          printf "%c%c%c",((a>0)?int(a*255):0),0,0 \
        } else printf "%c%c%c",0,0,0 \
  }}'\
| convert -delay 8 - sphere.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment