Business Card

                
                    #include <stdio.h> // ./card > scala.ppm
                    #include <math.h>  // https://cassayre.me/card
                    int main(){printf("P6 512 512 255 ");for(int k=
                    0,w=512;k<w*w;k++){int x=k%w,y=k/w,j=234,r=(x+y
                    )/8|1,g=61+y/15,b=83;for(double e=6.28,h=150,d,
                    i,n=w-1,l=(x/n-.5)*h,c=35,f=h*(y/n-.5)+22;j>1;j
                    --)c>(d=sqrt(l*l+(n=c-.3*j)*n))&d>30?x=c-d>.3,y
                    =c-d<4.7,d=fmod(atan2(n,l)+e,e),(i=floor(f/c+d/
                    e)-d/e)<1.5&fmod(-f*e/c-d+3*e,e)>1.5&i>-1?g=y*r
                    /10+12,r=(int)(y*86+20+h*fabs((y^x)*n/c)),b=x*r
                    /5|1,j=0:0:0;printf("%c%c%c",r,g,b);}return 0;}
                
            

This C snippet which you may have found on the back of my business card implements a ray marching algorithm that produces the following 512x512 Scala logo:

Pretty cool, I hope.

Context

The idea of creating a ray tracer on a business card is not new. Paul Heckbert first introduced the concept in 1984 as some sort of challenge.

Later on Andrew Kensler created his own version in 2009 and is today one of the most famous example. He manages to pack a bunch of features such as reflections, shadings, or even distance blur.

Finally, the most recent creation to my knowledge is Mark Zucker's miniray who won the IOCCC-2011 contest.

My work is not quite a ray tracer although it was carefully crafted to look like it (explanations below).

Features

  • Prints a PPM image to the standard output
  • Orthographic projection of a bounded helix
  • Some basic shadows
  • A fancy gradient in the background
  • The code is cross-platform compatible aswell as the image itself (windows can have issues rendering some images in this format)
  • Fits in a perfect rectangle: this was a sine qua non condition in order to fit on a card

How it works

The 'virtual' scene contains an infinite vertical cylinder that is truncated in its center. Rays are casted orthographically from every pixel of the image towards, potentially hitting the cylinder. If it is the case, a sub-procedure performs additional checking to carve the helix shape. The shaders are done with a dot product. If the ray misses the object, a gradient is computed according to the position of the pixel on the image.

The code does not contain any additional functions (aside to the main), nor does it contain any if-else statements. In fact, they were taking too much space and thus were replaced by ternary operators (two of them in here). Thorough inspection enabled me to considerably reduce the size of the code. Variables reallocation and other tricks shaved the final few bytes off.


© Florian Cassayre 2018
Version 11ceed0