/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
public class Particles extends OneArgumentOperator implements ELOperator 
{
    public void apply(ELMatrix argument[], ELMatrix out, ELEnvironment environment)
    {
	int width = out.getWidth();
	int height = out.getHeight();

	checkArguments(argument);

	out.setAll(0f, 0f, 0f);
    
	ELMatrix arg = argument[0];
    
	float red[] = out.red;
	float grn[] = out.grn;
	float blu[] = out.blu;

	float ired[] = arg.red;
	float igrn[] = arg.grn;
	float iblu[] = arg.blu;

	int numParticles = environment.getNumParticles();
	int numPixels = width * height - 1;

	int psize = (int)(2.0 * ((width > height) ? width : height) / 128.0 + 0.5);
	if (psize == 0) {
	    psize = 1;
	}

	for(int p = numParticles - 1; p >=0; p--) {
	    // input offset
	    int iffset = (p * numPixels / numParticles);

	    float r = ired[iffset];
	    float g = igrn[iffset];
	    float b = iblu[iffset];

	    int x0 = (environment.getParticleX(p) * width) >> 14;
	    int y0 = (environment.getParticleY(p) * height) >> 14;
	    
	    // Draw a nice square particle
	    for(int xx = 0; xx <= psize; xx++) {
		for(int yy = 0; yy <= psize; yy++) {
		    int x = (x0 + xx + width) % width;
		    int y = (y0 + yy + height) % height;
		    int offset = x + y * width;	    
		    red[offset] = r;
		    grn[offset] = g;
		    blu[offset] = b;
		}
	    }
	}
    }

    public String name()
    {
	return "Particles";
    }

    public String description()
    {
	return "A swarm of particles trace pseudo-random paterns";
    }  

    protected String iconString()
    {
	return "Prt";
    }

    public boolean hasExternalInfluences() {
	return true;
    }
}





