import jas.hist.*;

public class SineFunction extends Basic1DFunction
{
	private double offset;
	private double amplitude; 
	private double period;
	
	private static final String[] parameters = { "offset", "amplitude", "period" };
	/**
	 * Create the SineFunction. This constructor is required for automatic creation of the
	 * function through the GUI. The parameters passed in specify the range of the X and Y
	 * axes at the time the function is created, and may be used to specify sensible initial
	 * values so that the function is visible on the plot
	 */
	public SineFunction(double xmin, double xmax, double ymin, double ymax)
	{
		offset = 0;
		amplitude = (ymax-ymin)/2;
		period = (xmax-xmin)/2;
	}
	/**
	 * Create the SineFunction.
	 * A constructor for use by users wishing to directly create the function.
	 */
	public SineFunction(double offset, double amplitude, double period)
	{
		this.offset = offset;
		this.amplitude = amplitude;
		this.period = period;
	}
	/**
	 * Calculates the value of the function at the given X coordinate.
	 * The method can throw FunctionValueUndefined if the function is not defined
	 * for this value of X.
	 */
	public double valueAt(double x) throws FunctionValueUndefined
	{
		return amplitude*Math.sin(2*Math.PI*(x-offset)/period);
	}
	/**
	 * get the Title for this function. Used for selecting the function in the GUI.
	 */
	public String getTitle()
	{
		return "Sine Curve";
	}
	/**
	 * Get the names of the function parameters, returned as a String array.
	 */
	public String[] getParameterNames()
	{
		return parameters;
	}
	/**
	 * Get the current values for the function parameters, as a double array.
	 * The parameters must be returned in the same order as for the getParameterNames()
	 * function.
	 */
	public double[] getParameterValues()
	{
		return new double[]{ offset, amplitude, period };
	}
	/**
	 * Called to set the value of a parameter.
	 * The index passed in is the index of the function in the getParameterNames()
	 * method.
	 */
	public void setParameter(int index, double value)
	{
		if      (index == 0) offset = value;
		else if (index == 1) amplitude = value;
		else if (index == 2) period = value;
		else throw new IllegalArgumentException("index="+index);
		
		setChanged(); // Tell the GUi the function has changed (so it can be redrawn)
	}
}