Small programs

Some small C programs with big consequences.
Let's pretend it's art.


The following program generates the following image.

double X=-1.256,Y=.38,R=.02;
int a(int i,int j){double d=i*2*R/768+X-R*1024/768,e=j*-2*R/768+Y+R,x=d,y=e;
int c;for(c=255;c>0;c--){double x1=x*x-y*y+d,y1=2*x*y+e,r=x1*x1+y1*y1;if(r>4)
break;x=x1;y=y1;}return c;}int main(void){int i,j;for(j=0;j<768;j++)for(i=0;i
<1024;i++){unsigned char x=a(i,j);if(x>232)x=0;if(x<128)x=0;else if(x>200)x=(
256-x)*2;else x=x-100;putchar(x);}fflush(stdout);return 0;}

Download source: mandelbrot.c

[image: mandelbrot set]

This is the most famous example of fractals.

Mandelbrot zoom

A small variation of the previous program, generating a video.

double X,Y,R,o,i,j,k,z,d,e,x,y,t,u,r;int main(void){for(;k<=1.005;k+=.005){X=-
stdout);return 0;}

Download source: mandelbrot-zoom.c

To create the video, I ran:

ffmpeg -f rawvideo -s 320x240 -r 25 -pix_fmt gray -i /tmp/A -vf scale=1024:768 -sws_flags neighbor -b:v 1M /tmp/A.ogg

I love the pixelated look (even if the encoding destroys it a little bit, not squared enough).


This one I found by accident while having fun with assembly language and good old mode 13h (old memories). I was doing plot(x, x and y) by mistake, and that was the result. Here we use a different but equivalent trick. The reader will have to figure out by herself, or politely ask me and I'll be more than happy to explain. I still remember how surprized (surprised?) I was when that Sierpinski's triangle popped up on my dark screen, late at night.

I embellished a bit to create a video (animated GIF actually, to annoy you, even though I don't know who you are and I doubt I would want to annoy you in the first place so let me apologize for this) instead of a single image.

#include <stdio.h>
#include <math.h>
int main(void){double B,C,D,E;int A,i,j,u,v;for(A=0;A<240;A++){B=M_PI*2/

Download source: sierpinski.c

[image: rotating sierpinski triangle]

To produce the video, I ran:

gcc -Wall sierpinski.c  -lm -O3; ./a.out > KA; ffmpeg -f rawvideo -s 320x240 -r 24 -pix_fmt gray -i KA sierpinski.gif


I was inspired by the total solar eclipse we've had 1999-08-11 not too far from Paris. Some friend of mine (Hulud, if you read this...) had a car and we went to √Čtretat to experience it fully. All in a sudden all was dark and cold. Everyone shouted. Happy? Then the light and warmth came back. Strange and good moment.

Technical details on demand. It's just a bunch of tricks in there, nothing really fancy. You may also see this, the effect is in there with a more expansed source code (maybe not enough to understand though).

This program is a bit long, sorry for that.

#define F(Y,Z)for(Y=0;Y<Z;Y++)
#define R rand()/(RAND_MAX+1.)
unsigned char A[76800],B[32][32],C[256][256],D[256][256],*p,*t;int E[2364][2],
i,j,k,z,X,Y,d,g,a,b;double l,x,y,m;int main(void){F(i,32)F(j,32){k=(14-sqrt((

Download source: eclipse.c



This is my version to generate this (sorry if deadlink, this is out of my control).

The code is not very small. And not obfuscated. But it's still quite small. Minification and obfuscation left as an exercise to the reader! (Send me your work!)

spirals.c contains the code.

[image: the output of spirals.c, made small, animated gif]
(Click this small image for a larger version. The file is big though!)

And here is the order of plotting all the bars. Let's reveal the trick!

[image: the steps of plotting one frame of the animation]

If you edit the code, look for this line:

    if (repaint) { paint(curtime, 0, 0); redraw = 1; repaint = 0; }

And replace by this line:

    if (repaint) { paint(curtime, 0, 1); redraw = 1; repaint = 0; }

Then you also get the plotting done bar by bar in X Window mode.

Oh yes, only works on X Window. Is there something else out there?

Ah, you can comment out the X Window code and have it compile on other systems. (Exercise two for the reader, way easier than minification and obfuscation.) You still need cairo for the plotting (which you want to get rid of for the minification I'd say, and find a small algorithm to plot the bars).

The program is very slow, sorry for that. Let's pretend cairo is the guilty one.

I think it should not be too hard to write a javascript/canvas version. Exercise three for the reader. Send me your work!



char A[1<<18];int main(void){double K=0.5257311121191334,J=0.8506508083520399,
B,C,D,E,F,G,H;int I,L;for(B=0;B<60;B++){for(I=0;I<1<<18;)A[I++]=0;for(C=-2;C<2
1<<18,1,stdout);}return 0;}


[image: points moving in a complex repetitive way]



(10 is pronounced "dis" in french.)


[image: anarchist A symbol with some noise, hard to describe, sorry]

Animated image.

Was nice to do, lots of fun. I should describe the various tricks involved.

Maybe I'll do an obfuscated version when I feel like I want to (but if I wait too much then when I read the code I won't understand it anymore...). Bah, so be it. Laziness! I love you.

Pi day

Pi is special. It has its own webpage. And with some javascript in it!



P-norm is funny. Let's have fun with it.

norm.c generates an animation. Here are a few screenshots.


The animation is norm.gif, big file!

And norm_infinity.c does another animation, with more stuff. Some screenshots? There you go.


And there comes norm_infinity.gif!

I loved doing this! My only problem is that these programs are not realtime. I've always been bad with raytracing... I'm almost sure this can be rendered in realtime. If you read this and can help...

2022-10-07: I made some GLSL versions of the above! Check here. Might be realtime (that is: more than 25 frames per second) depending on your GPU. This GPU thing is insane me says.



Let's have fun with the Koch curve.

koch.c produces the following image when piped with gui.c and then some ffmpeg magic to generate the GIF.

[Koch curve with moving top point]

koch-full.c is a little variation. I struggled a bit for this one! I'm not proud...

[Koch curve with moving top point, variation of previous]

I think I prefer first version. I'm not sure.

What about koch-wave.c? Nice, no?

[Koch curve with moving top point, variation of previous - looks like water and waves, sorry if you're blind, too visual to describe]


A rewrite in plain C of a nice shader.
increment-tan.c (just the code)
increment-tan-verbose.c (some doc)
format-increment.c (tool to generate formatted output)

Contact: see this

Created: 2019-12-20
Last update (more or less accurate): 2023-03-09