Skip to content

Instantly share code, notes, and snippets.

@rgrig
Created March 18, 2014 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgrig/9621141 to your computer and use it in GitHub Desktop.
Save rgrig/9621141 to your computer and use it in GitHub Desktop.
beginfig(1)
path digit;
numeric d;
string digit_string;
u := 5mm;
max_base = 10;
max_num = 99;
digit_string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
color c_d[];
n := 1;
forever: n := n+1; exitif n * (n + 1) / 2 >= max_base; endfor;
color A, B;
c_d[0] = red;
d := 1;
for i=2 upto n:
A := ((i-1)/(n-1))[red,green];
B := ((i-1)/(n-1))[red,blue];
for j=1 upto i:
c_d[d] := ((j-1)/(i-1))[A,B];
d := d+1;
endfor;
endfor;
digit := unitsquare scaled (.9u);
defaultscale := u/fontsize defaultfont;
% well, some approx of log, good for this drawing
def log(expr b,x) = if x < 1: 0 else: (1 + log(b,x/b)) fi enddef;
def draw_num(expr b, n, x, y) =
numeric d, xx, nn;
if b = 1:
for i=1 upto n:
fill digit shifted (x-i*u,y) withcolor c_d[0];
%draw digit shifted (x-i*u,y) withcolor black;
endfor;
else:
xx := x;
nn := n;
forever:
d := nn - floor(nn/b)*b;
xx := xx - u;
fill digit shifted (xx,y) withcolor c_d[d];
%draw digit shifted (xx,y) withcolor black;
%label(substring(d,d+1)of digit_string,(xx+u/2,y+u/2));
nn := (nn - d) / b;
exitif nn = 0;
endfor;
fi;
enddef;
x := 0;
y := 0;
for i = 0 upto max_num:
y := y - u;
draw_num(1, i, x, y);
endfor;
for base=2 upto max_base:
x := x + u*(0.5 + log(base, max_num));
y := 0;
for i = 0 upto max_num:
y := y - u;
draw_num(base, i, x, y);
endfor;
endfor;
endfig;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment