Skip to content

Instantly share code, notes, and snippets.

@auselen
Last active March 24, 2017 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auselen/38f7519c8108da61e65193ebacf7f818 to your computer and use it in GitHub Desktop.
Save auselen/38f7519c8108da61e65193ebacf7f818 to your computer and use it in GitHub Desktop.
Drawing Mandelbrot with AWK

Drawing Mandelbrot with AWK

Mandelbrot

Code

$ LC_ALL=ISO-8859-1 awk 'BEGIN { \
  max=256; \
  print "P6"; \
  print "256 256"; \
  print "255"; \
  for (y=127; y>=-128; y--) \
    for (x=-128; x<128; x++) { \
      z_r=0; \
      z_i=0; \
      c_r=x/64; \
      c_i=y/64; \
      i=0; \
      while (z_r*z_r+z_i*z_i <= 4.0 && i<max) { \
        t=z_r*z_r-z_i*z_i+c_r; \
        z_i=z_r*z_i*2+c_i; \
        z_r=t; \
        i++; \
      } \
      if (i<max) { \
        printf "%c%c%c",(i*32>255)?255:i*32,(i*16>255)?255:i*16,(i*2>255)?255:i*2 \
      } else \
        printf "%c%c%c",0,0,0 \
    } \
}' | convert - -scale 200% fractal.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment