function f(i){return (i-i%a)/a}function r(x,y,i){t.fillStyle='#'+i;t.fillRect(x*a+1,y*a+1,9,9)}var g=[],n=[],a=10,i,j,h=f(innerHeight-a),w=f(innerWidth),b=document.body,c=b.children[0],t=c.getContext('2d');b.style.margin=0;b.bgColor='#333333';c.width=a*w;c.height=a*h;for(i=w;i-->0;){g[i]=[];for(j=h;j-->0;){g[i][j]=-1;r(i,j,'444')}}function z(){for(i=n.length;i-->0;){var N=n[i],x=N[0],y=N[1],v=N[2],G=g[x][y];r(x,y,G>0?'fff':'000');g[x][y]*=-1;x+=G*(1-v)%2;y+=G*(2-v)%2;v+=G;x=x<0?w-1:x%w;y=y<0?h-1:y%h;v=v<0?3:v%4;n[i]=[x,y,v];r(x,y,'fc0')}setTimeout(z,100)}c.onclick=function(e){x=f(e.pageX);if(x<w&&n.push([x,f(e.pageY),1])&&!n[1])z()}
// a 1k cellula automata, based on Langton's Ant
// . http://en.wikipedia.org/wiki/Langton's_ant
// . http://www.paulhammond.org/2010/08/1k/
// this is a reworked version of my 2001 5k entry:
// . http://web.archive.org/web/20030101055404/www.the5k.org/description.asp/entry_id=549
// . http://www.paulhammond.org/2001/the5k/
// lookup:
// a = 10
// b = body
// c = canvas
// d = document
// e = event
// f() = floor(x/10)
// g = grid
// i = iterator
// j = iterator
// h = height of grid (g[1].length)
// n = "nodes" - array of [x,y,v]
// o = e.pageX (used in c.onclick)
// r() = rectangle()
// t = canvas.context
// w = width of grid (g.length)
// z() = zoom!()
// to compress:
// perl -p -0 -e 's!(//[^\n]*)?\n\s*!!g;s!;}!}!g;s!;+!;!g' <script.js >compressed.js
function f(i){
return (i-i%a)/a
}
function r(x,y,i){
t.fillStyle='#'+i;
t.fillRect(x*a+1,y*a+1,9,9);
}
var g=[],n=[],a=10,i,j,h=f(innerHeight-a),w=f(innerWidth),b=document.body,c=b.children[0],t=c.getContext('2d');
b.style.margin=0;b.bgColor='#333333';
c.width=a*w;c.height=a*h;
for(i=w;i-->0;){
g[i]=[];
for(j=h;j-->0;){
g[i][j]=-1;
r(i,j,'444')
}
}
// G = this grid square
// N = this node info [x,y,dx,dy]
// x = y
// y = y
// v = direction ("velocity")
function z(){
for(i=n.length;i-->0;){
var N=n[i],x=N[0],y=N[1],v=N[2],G=g[x][y];
r(x,y,G>0?'fff':'000');
g[x][y]*=-1;
x+=G*(1-v)%2;
y+=G*(2-v)%2;
v+=G;
x=x<0?w-1:x%w;
y=y<0?h-1:y%h;
v=v<0?3:v%4;
n[i]=[x,y,v];
r(x,y,'fc0');
}
setTimeout(z,100);
}
c.onclick=function(e){
x=f(e.pageX);
if(x<w&&n.push([x,f(e.pageY),1])&&!n[1])z()
}