Skip to content

Instantly share code, notes, and snippets.

@auselen
Last active August 28, 2023 21:43
Show Gist options
  • Save auselen/906a53b47a7d616b080dbef85eb8f776 to your computer and use it in GitHub Desktop.
Save auselen/906a53b47a7d616b080dbef85eb8f776 to your computer and use it in GitHub Desktop.
Drawing a heart with AWK

Drawing a heart with AWK

Heart

Math

  • t = 0 → 2𝜋
  • x = 16 sin³(t)
  • y = 13 cos(t) - 5 cos(2 t) - 2 cos(3 t) - cos(4 t)

Code

LC_ALL=ISO-8859-1 awk 'BEGIN { \
  pi=atan2(0, -1); \
  for (t=0; t<(pi*2); t+=pi/180) { \
    print "P6"; print "256 256"; print "255"; \
    if (t==0) { \
      for (i=0;i<256*256;i++) \
        printf "%c%c%c",128,0,128; \
      continue \
    } \
    px=16*(sin(t)^3); \
    px*=7.5; \
    py=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t); \
    py*=7.5; \
    for (y=127;y>=-128;y--) \
      for (x=-128;x<128;x++) \
        if (sqrt((px-x)^2 + (py-y)^2) < 3) \
          printf "%c%c%c",255,0,0; \
        else \
          printf "%c%c%c",255,255,255; \
  }}' | convert -transparent white -compose Copy -set delay 1 -coalesce -layers RemoveDups - heart.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment