package jasext.hist.function;

import jas2.hist.*;
import java.io.*;

public class StraightLineFunction extends Fittable1DFunction implements HasHandles	
{
	StraightLineFunction(double slope, double yoffset)
	{
		this.p[0] = slope;
		this.p[1] = yoffset;
	}
	public StraightLineFunction(double xmin, double xmax, double ymin, double ymax)
	{
		this.p[0] = (xmax!=xmin) ? (ymax-ymin)/(xmax-xmin) : 0; // slope
		this.p[1] = ymin - xmin*p[0]; // yoffset
	}
	public double valueAt(double x) 
	{
		return valueAt(x,p);
	}
	public double valueAt(double x, double[] a)
	{
		return a[1] + x*a[0];
	}
	private double valueFor(double y) // inverse of function, get X for given Y
	{
		return (y-p[1])/p[0];
	}
	public String[] getParameterNames()
	{
		final String[] result = { "slope","yoffset" };
		return result;
	}
	public double[] getParameterValues()
	{
		return p;
	}
	public void setParameter(int index, double value)
	{
		if (index>p.length) 
			throw new IllegalArgumentException("Invalid index to setParameter");

		p[index] = value;
		clearFit();
		setChanged();
	}
	public void setFit(Fitter fit, double[] a)
	{
		if (a.length != p.length) 
			throw new IllegalArgumentException("Argument to setParameters is of wrong length");

		System.arraycopy(a,0,p,0,p.length);
		setFit(fit);
		setChanged();
	}
	public String getTitle()
	{
		return "Straight Line";
	}
	public Handle[] getHandles(double xLow, double xHigh, double yLow, double yHigh)
	{		
				
		int nHandle = 0;
		double x, y;
		double[] intersectX = new double[4];
		double[] intersectY = new double[4];

		// find where the line intersects the bounding rectangle (if it does)
		
		x = xLow;
		y = valueAt(x);
		
		if (y>=yLow && y<=yHigh)
		{
			intersectX[nHandle] = x;
			intersectY[nHandle] = y;
			nHandle++;
		}
		x = xHigh;
		y = valueAt(x);
		if (y>=yLow && y<=yHigh)
		{
			intersectX[nHandle] = x;
			intersectY[nHandle] = y;
			nHandle++;
		}
		y = yLow;
		x = valueFor(y);
		if (x>xLow && xxLow && x 

Ads help maintain this website.