/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
public class Rings extends OneArgumentOperator implements ELOperator 
{
    public void apply(ELMatrix argument[], ELMatrix out, ELEnvironment environment)
    {
	checkArguments(argument);
        
	ELMatrix arg = argument[0];
	final int width = out.getWidth();
	final int height = out.getHeight();
	final int midx = width / 2;
	final int midy = height / 2;
	final double angle = Math.toRadians(5);

	final double maxRad = Math.sqrt(midx * midx + midy * midy);

	for(int oy = height - 1; oy >= 0; oy--) {
	    for(int ox = width - 1; ox >= 0; ox--) {
		
		int xx = ox - midx;
		int yy = oy - midy;

		final double radius = Math.sqrt(xx * xx + yy * yy);
		final double a = angle * Math.cos((radius / maxRad) * 8.0 * Math.PI);
		final double cos = Math.cos(a);
		final double sin = Math.sin(a);

		int ix = (int)(width * 2 + midx + cos * xx + sin * yy) % width;
		int iy = (int)(height * 2 + midy + cos * yy - sin * xx) % height;
		int ioffset = ix + iy * width;
		int ooffset = ox + oy * width;
		for(int c = 0; c < 3; c++) {
		    out.channel[c][ooffset] = arg.channel[c][ioffset];
		}
	    }
	}  
    }

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

    public String description()
    {
	return "Rotates the image by 5 degrees, with direction as a function of radius.";
    }  

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