FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   brightness eaters (malfunctioning program)
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: brightness eaters (malfunctioning program)  (Read 798 times)
st33d

WWW Email
brightness eaters (malfunctioning program)
« on: Mar 15th, 2005, 10:18pm »

I've writen a particle class that crawls across an image eating the brightness. The particles eat the color down to 150. If starved of food they move to the next brightest pixel and chow down on that one.
 
My question is, it seems to work fine ramped up to 1000 or so particles but for some reason there are random particles that refuse to travel, despite having enough stamina to travel 15 pixels in search of food. Can anyone see what might be the cause?
 
Thanks for your time.
Code:

//brightness eaters
BImage swap;
eater [] e;
int w,h,count;
void setup(){
count = 0;
swap = loadImage("b.gif");
w = swap.width;
h = swap.height;
size(w,h);
e = new eater[100];
for (int i = 0; i < e.length; i++){
e[i] = new eater(int(random(w)),int(random(h)));
}
colorMode(HSB);
noStroke();
}
void loop(){
background(swap);
for (int i = 0; i < e.length; i++){
if(e[i].hp > 0){
if (e[i].hp < 15 && e[i].hp > 0){
e[i].move();
}
e[i].eat();
e[i].draw();//reminder to not draw eater sprites
}
}
}
//save state (reminder sprite drawing)
void mousePressed(){
save("drain.tif");
println("save count "+count++);
}
class eater{
int x,y,hp;
eater(int x, int y){
this.x = x;
this.y = y;
hp = 20;
}
void draw(){
float s = hp;
if (s > 255){
s = 255;
}
colorMode(RGB);
fill(255,0,0,hp*10);
ellipse(x-3,y-3,6,6);
colorMode(HSB);
}
void eat(){
color c = swap.pixels[x + y * w];
float u = hue(c);
float s = saturation(c);
float b = brightness(c);
if (b > 150){
hp++;
swap.pixels[x + y * w] = drain (c,5);
//path halo
/*
swap.pixels[wr(x+1,w-1) + y * w] = drain (swap.pixels[wr(x+1,w-1) + y * w],2);
swap.pixels[x + wr(y+1,h-1) * w] = drain (swap.pixels[x + wr(y+1,h-1) * w],2);
swap.pixels[wr(x-1,w-1) + y * w] = drain (swap.pixels[wr(x-1,w-1) + y * w],2);
swap.pixels[x + wr(y-1,h-1) * w] = drain (swap.pixels[x + wr(y-1,h-1) * w],2);
*/
} else if (hp > 0){
hp--;
}
}
void move(){
//locate nearest bright pixel and move to when starved
float [] opt = new float[4];
opt[0] = brightness(swap.pixels[wr(x+1,w-1) + y * w]);
opt[1] = brightness(swap.pixels[x + wr(y+1,h-1) * w]);
opt[2] = brightness(swap.pixels[wr(x-1,w-1) + y * w]);
opt[3] = brightness(swap.pixels[x + wr(y-1,h-1) * w]);
float [] cmp = new float[4];
System.arraycopy(opt, 0, cmp, 0, opt.length);
sort(cmp);
if (cmp[cmp.length-1] == opt[0]){
x = wr(x+1,w-1);
}
if (cmp[cmp.length-1] == opt[1]){
y = wr(y+1,h-1);
}
if (cmp[cmp.length-1] == opt[2]){
x = wr(x-1,w-1);
}
if (cmp[cmp.length-1] == opt[3]){
y = wr(y-1,h-1);
}
}
}
//screen wrap around function
int wr (int valu, int limi){
  int car=valu;
if (car>limi){ car=(car)%limi; }
  if (car<0){
    car=limi-car;
    car=(car)%limi;
    car=limi-car;
  }
  return car;
}
color drain (color c, float val){
float u = hue(c);
float s = saturation(c);
float b = brightness(c);
b -= val;
return color(u,s,b);
}
 

I could murder a pint.
st33d

WWW Email
Re: brightness eaters (malfunctioning program)
« Reply #1 on: Mar 17th, 2005, 10:56pm »

Hello me.
 
What seems to be the problem is the eaters move function. The way you have it set out allows them to move through all the directions back to the same spot!
 
Reply:
 
I see, thanks me. I've solved the problem by brute coding a switch after my move commands so the eaters are forced into a linear decision about moving. I've also adapted the code for eaters that eat bright and dark and operate on different thresholds.
 
http://www.st33d.net/processing/eatb.html
 
Thanks also to Sjeiti for pointing out the save() method on BImage.
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »