Rational billiard embroidery in C
A complete program. Standard C, a compiler, and the rule behind the shape.

Run it in three steps.
- Save the source.
Download geo-billiards.c into a folder on your computer.
- Compile it.
In that folder, run this with GCC or Clang:
cc -std=c11 -O2 geo-billiards.c -lm -o geo-billiards - Make the image.
./geo-billiardsOpen
geo-billiards.svgin a browser to see the result.
On Windows with GCC, name the executable geo-billiards.exe and run it from the same folder.
How the picture is built
Unfold the square, move along a line of rational slope, then fold both coordinates by a triangular wave. Exact wall-crossing times determine every bounce.
Make it your own
P = 13 and Q = 21, each 1..37. Velocity is (Q/gcd(P,Q), P/gcd(P,Q)); the irrational starting point avoids corners.
The SVG displays one period of an ideal point particle without physical size or friction. Rational slope does not imply rational angle. Each run produces one image; use Graphic mode for the interactive animation.
/* Arithmos: geo-billiards
* Compile: cc -std=c11 -O2 geo-billiards.c -lm -o geo-billiards
* Run: ./geo-billiards
* Output: geo-billiards.svg (open this file in a browser)
* Optional output path: ./geo-billiards my-image.svg
* Edit the constants in draw() to explore another case.
*/
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static FILE *nt_out;
#define NT_PI 3.14159265358979323846
/* The small SVG writer keeps this program free of graphics dependencies.
* Coordinates are pixels on a 1000 x 700 drawing surface.
* t runs from 0 to 1 through mint, blue, rose, and gold.
*/
static inline void nt_color(double t, char hex[8]) {
const double stops[4][3] = {
{91,227,201}, {128,146,240}, {218,138,220}, {244,200,127}
};
t = fmax(0.0, fmin(1.0,t)) * 3.0;
int band = (int)fmin(2.0,floor(t));
double blend = t - band;
int r[3];
for (int k=0;k<3;k++) r[k]=(int)lround(stops[band][k]*(1.0-blend)+stops[band+1][k]*blend);
snprintf(hex,8,"#%02x%02x%02x",r[0],r[1],r[2]);
}
static inline void nt_line(double x,double y,double X,double Y,double t,double alpha,double width) {
char color[8];nt_color(t,color);
fprintf(nt_out,"<path d=\"M%.3f %.3f L%.3f %.3f\" fill=\"none\" stroke=\"%s\" stroke-opacity=\"%.3f\" stroke-width=\"%.3f\"/>\n",x,y,X,Y,color,alpha,width);
}
static inline void nt_dot(double x,double y,double r,double t,double alpha) {
char color[8];nt_color(t,color);
fprintf(nt_out,"<circle cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\" fill=\"%s\" fill-opacity=\"%.3f\"/>\n",x,y,r,color,alpha);
}
static inline void nt_circle(double x,double y,double r,double t,double alpha,double width) {
char color[8];nt_color(t,color);
fprintf(nt_out,"<circle cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\" fill=\"none\" stroke=\"%s\" stroke-opacity=\"%.3f\" stroke-width=\"%.3f\"/>\n",x,y,r,color,alpha,width);
}
static inline void nt_rect(double x,double y,double w,double h,double t,double alpha) {
char color[8];nt_color(t,color);
fprintf(nt_out,"<rect x=\"%.3f\" y=\"%.3f\" width=\"%.3f\" height=\"%.3f\" fill=\"%s\" fill-opacity=\"%.3f\"/>\n",x,y,w,h,color,alpha);
}
static inline void nt_text(double x,double y,const char *text) {
fprintf(nt_out,"<text x=\"%.3f\" y=\"%.3f\" fill=\"#ededf3\" font-family=\"monospace\" font-size=\"16\">",x,y);
for (;*text;text++) {
if (*text=='&') fputs("&",nt_out);
else if (*text=='<') fputs("<",nt_out);
else if (*text=='>') fputs(">",nt_out);
else fputc(*text,nt_out);
}
fputs("</text>\n",nt_out);
}
static inline int nt_gcd(int a,int b) {a=abs(a);b=abs(b);while(b){int r=a%b;a=b;b=r;}return a;}
static inline int nt_prime(int n) {if(n<2)return 0;for(int d=2;d<=n/d;d++)if(n%d==0)return 0;return 1;}
/* Edit horizontal Q and vertical P (each 1..37). */
static int compare_time(const void *a,const void *b) {
double x=*(const double*)a,y=*(const double*)b;return (x>y)-(x<y);
}
static double fold(double v) {double z=fmod(v,2);if(z<0)z+=2;return z<=1?z:2-z;}
static void draw(void) {
const int P=13,Q=21;
int g=nt_gcd(P,Q),p=P/g,q=Q/g,n=0;
double x0=sqrt(2.0)/5,y0=sqrt(3.0)/7,times[160];
times[n++]=0;times[n++]=2;
for(int k=1;k<=(int)ceil(x0+2*q);k++){double t=(k-x0)/q;if(t>0&&t<2)times[n++]=t;}
for(int k=1;k<=(int)ceil(y0+2*p);k++){double t=(k-y0)/p;if(t>0&&t<2)times[n++]=t;}
qsort(times,n,sizeof(double),compare_time);
nt_line(220,70,780,70,.1,.5,1);nt_line(780,70,780,630,.1,.5,1);
nt_line(780,630,220,630,.1,.5,1);nt_line(220,630,220,70,.1,.5,1);
for(int i=1;i<n;i++) {
double a=times[i-1],b=times[i];
nt_line(220+560*fold(x0+q*a),630-560*fold(y0+p*a),220+560*fold(x0+q*b),630-560*fold(y0+p*b),(double)i/(n-1),.72,1.6);
}
nt_text(30,35,"Rational billiards: a rational-slope line reflected inside a unit square");
}
int main(int argc, char **argv) {
if (argc > 2) {
fprintf(stderr, "Usage: %s [OUTPUT.svg]\n", argv[0]);
return EXIT_FAILURE;
}
const char *filename = argc == 2 ? argv[1] : "geo-billiards.svg";
nt_out = fopen(filename, "wb");
if (!nt_out) { perror(filename); return EXIT_FAILURE; }
fputs("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1000\" height=\"700\" viewBox=\"0 0 1000 700\">\n"
"<rect width=\"1000\" height=\"700\" fill=\"#171721\"/>\n", nt_out);
draw();
fputs("</svg>\n", nt_out);
int failed = ferror(nt_out);
if (fclose(nt_out) != 0) failed = 1;
if (failed) { fputs("Could not finish writing the image.\n", stderr); return EXIT_FAILURE; }
printf("Wrote %s\n", filename);
return EXIT_SUCCESS;
}