//threshold tartanaters //fixed code 26/04/05 torch [] t; BImage swap; boolean drawit = false; int w,h; int saveCount = 0; void setup(){ swap = loadImage ("test.gif"); w = swap.width; h = swap.height; println("canvas size:"+w+" by "+h); size (350,200);//change to w,h for image export t = new torch[1000]; for (int i = 0; i < t.length/2; i++){ t[i] = new torch(int(random(width)),int(random(height)),int(random(4)),false); } for (int i = t.length/2; i < t.length; i++){ t[i] = new torch(int(random(width)),int(random(height)),int(random(4)),true); } } void loop(){ background(swap); for (int i = 0; i < t.length; i++){ t[i].trace(); if (drawit){ t[i].draw(); } } } void keyPressed(){ switch(key){ case 's': case 'S': swap.save("tartan"+(saveCount)+".tif"); println("saved tartan"+(++saveCount)+".tif"); break; case 'd': case 'D': drawit = !drawit; break; } } class torch{ int x,y,d; boolean light; torch(int x, int y, int d, boolean light){ this.x = x; this.y = y; this.d = d; this.light = light; } void draw(){ color c = swap.pixels[x + y * w]; int r=c>>16&0xff; int g=c>>8&0xff; int b=c&0xff; noStroke(); if (light){ fill(r-30,g-30,b-30,130); } else { fill(r+30,g+30,b+30,130); } rect(x-3,y-3,6,6); } void trace(){ int c = swap.pixels[x + y * width]; if (light){ switch(d){ case 0: if (grey(swap.pixels[((x+1)%w) + y * width]) > 10){ x = (x+1)%w; }else{ d = (d+1)%4; } break; case 1: if (grey(swap.pixels[x + ((y+1)%h) * width]) > 10){ y = (y+1)%h; }else{ d = (d+1)%4; } break; case 2: if (grey(swap.pixels[wr(x-1,w-1) + y * width]) > 10){ x = wr(x-1,w-1); }else{ d = (d+1)%4; } break; case 3: if (grey(swap.pixels[x + wr(y-1,h-1) * width]) > 10){ y = wr(y-1,h-1); }else{ d = (d+1)%4; } break; } if (grey(swap.pixels[x + y * width])- 10 > 0){ c = tone(swap.pixels[x + y * width],-10); } }else{ switch(d){ case 0: if (grey(swap.pixels[((x+1)%w) + y * width]) <= 245){ x = (x+1)%w; }else{ d = (d+1)%4; } break; case 1: if (grey(swap.pixels[x + ((y+1)%h) * width]) <= 245){ y = (y+1)%h; }else{ d = (d+1)%4; } break; case 2: if (grey(swap.pixels[wr(x-1,w-1) + y * width]) <= 245){ x = wr(x-1,w-1); }else{ d = (d+1)%4; } break; case 3: if (grey(swap.pixels[x + wr(y-1,h-1) * width]) <= 245){ y = wr(y-1,h-1); }else{ d = (d+1)%4; } break; } if (grey(swap.pixels[x + y * width])+ 10 < 255){ c = tone(swap.pixels[x + y * width],10); } } swap.pixels[x + y * width] = c; } } int grey(color p) { return max((p >> 16) & 0xff, (p >> 8 ) & 0xff, p & 0xff); } color tone (color c, int val){ int r=c>>16&0xff; int g=c>>8&0xff; int b=c&0xff; r += val; g += val; b += val; return color(r,g,b); } int wr(int v, int lim){ if (v < 0){ v = lim; } return v; }