# C

Creates a vortex, for reasons I don't understand, with even and odd frames containing completely different vortices.

This a preview of the first 50 odd frames:

![vortex preview][1]

Sample image converted from PPM to demo complete color coverage:

![sample image][2]

Later on, when it's all blended into grey, you can still see it spinning: [longer sequence][3].

Code as follows. To run, include the frame number, e.g.:

    ./vortex 35 > 35.ppm

I used this to get an animated GIF:

<pre>convert -delay 10 `ls *.ppm | sort -n | xargs` -loop 0 vortex.gif</pre>

    #include <stdlib.h>
    #include <stdio.h>

    #define W 256
    #define H 128

    typedef struct {unsigned char r, g, b;} RGB;

    int S1(const void *a, const void *b)
    {
        const RGB *p = a, *q = b;
        int result = 0;

        if (!result)
            result = (p->b + p->g * 6 + p->r * 3) - (q->b + q->g * 6 + q->r * 3);

        return result;
    }

    int S2(const void *a, const void *b)
    {
        const RGB *p = a, *q = b;
        int result = 0;

        if (!result)
            result = p->b * 6 - p->g;
        if (!result)
            result = p->r - q->r;
        if (!result)
            result = p->g - q->b * 6;

        return result;
    }

    int main(int argc, char *argv[])
    {
        int i, j, n;
        RGB *rgb = malloc(sizeof(RGB) * W * H);
        RGB c[H];

        for (i = 0; i < W * H; i++)
        {
            rgb[i].b = (i & 0x1f) << 3;
            rgb[i].g = ((i >> 5) & 0x1f) << 3;
            rgb[i].r = ((i >> 10) & 0x1f) << 3;
        }

        qsort(rgb, H * W, sizeof(RGB), S1);

        for (n = 0; n < atoi(argv[1]); n++)
        {
            for (i = 0; i < W; i++)
            {
                for (j = 0; j < H; j++)
                    c[j] = rgb[j * W + i];
                qsort(c, H, sizeof(RGB), S2);
                for (j = 0; j < H; j++)
                    rgb[j * W + i] = c[j];
            }

            for (i = 0; i < W * H; i += W)
                qsort(rgb + i, W, sizeof(RGB), S2);
        }

        printf("P6 %d %d 255\n", W, H);
        fwrite(rgb, sizeof(RGB), W * H, stdout);

        free(rgb);

        return 0;
    }


  [1]: https://i.sstatic.net/BEyPY.gif
  [2]: https://i.sstatic.net/3GnAW.png
  [3]: http://www.mediafire.com/convkey/c01a/lz56znlgsre8cddfg.jpg