package ch.psi.plot.xy.generator;


/**
 * Converter to convert a simple text file to a xy series
 */
public class XYGenerator {
	
	private final double start;
	private final double stepSize;
	private final int steps;
	
	/**
	 * Current position
	 */
	private double currentStep;
	
	
	/**
	 * Function to use by the generator
	 */
	private final XYGeneratorFunction function;
	/**
	 * Noise generator
	 */
	private final Noise noiseGenerator;

	
	/**
	 * Constructor
	 * @param function		Function to use for generating data
	 * @param noise			Add some noise to the generated x  value
	 */
	public XYGenerator(XYGeneratorFunction function, double start, double stepSize, int steps, boolean noise){
		this.function = function;
		this.start = start;
		this.steps = steps;
		this.currentStep=0;
		this.stepSize = stepSize;
		
		if(noise){
			this.noiseGenerator = new Noise(stepSize);
		}
		else{
			this.noiseGenerator = null;
		}
	}
	

	/**
	 * Get new data point
	 * @return	Returns null if there is no new point. Otherwise array of double (of size 2) where array[0] == x
	 * 			and array[1] == y
	 */
	public double[] getNewPoint(){
		
		if(currentStep>steps){
			return null;
		}
		
		double x = start+(stepSize*currentStep);
		if(noiseGenerator != null){
			x = noiseGenerator.generate(x);
		}
		currentStep++;
		
		// Get y value for x
		double y = function.generate(x);
		
		return(new double[] {x, y});
	}
	
	/**
	 * Reset generator to start settings
	 */
	public void reset(){
		this.currentStep = 0;
	}

}
