/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
import java.awt.*;

public class SawPulse extends ZeroArgumentOperator implements ELOperator 
{
  public void apply(ELMatrix argument[], ELMatrix out, ELEnvironment environment)
  {
    checkArguments(argument);
    
    // For extra performance, pull the arrays into locals so we avoid
    // excess dereferences.
    float red[] = out.red;
    float grn[] = out.grn;
    float blu[] = out.blu;

    int width = out.getWidth();
    int height = out.getHeight();

    double v = environment.getTime();
    long iValue = (long) v;
    float value;
    
    value = (float)(v - Math.floor(v)) * 2.0f - 1.0f;

    if ((iValue & 1) == 0) {
	value = -value;
    }

    for(int offset = width * height - 1; offset >= 0; offset--) {
      red[offset] = value;
      grn[offset] = value;
      blu[offset] = value;
    }
  }

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

  public String description()
  {
    return "Saw pulse that varies with time between -1 and 1.";
  }

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

  public boolean hasExternalInfluences() {
    return true;
  }

  public void paintIcon(Graphics2D graphics, int x, int y, int width, int height)
  {
    graphics.setColor(Color.black);
    graphics.drawLine(x,y,x+width/4,y+height);
    graphics.drawLine(x+width/4,y+height,x+width/2,y);
    graphics.drawLine(x+width/2,y,x+3*width/4,y+height);
    graphics.drawLine(x+3*width/4,y+height, x+width, y);
  }

}
