public class LineFunction {
  
  // private fields
  private double slope     = 0.0 ;
  private double intercept = 0.0 ;

  // Accessor methods
  public double getSlope() {
    return slope ;
  }
  public double getIntercept() {
    return intercept ;
  }
  
  // Modifier methods
  public void setSlope(double slope) {
    this.slope = slope ;
  }
  public void setIntercept(double intercept) {
    this.intercept = intercept ;
  }
  
  public String toString() {
    return "LineFunction: " + slope + "*x + " + intercept ;
  }
  
  public double plotXtoY(double x) {
    return slope*x + intercept ;
  }
  
  public void setFromPoints(double x1, double y1, double x2, double y2) {
    if (x1==x2) {
      // This should be an exception
      slope = 0.0 ;
      intercept = 0.0 ;
    } else {
      slope = (y2-y1)/(x2-x1) ;
      intercept = y1 - slope*x1 ;
    }
  }
  
  public double getInterceptX() {
    if (slope == 0.0) {
      // ??
      return Double.NaN ;
    } else {
      return - intercept/slope ;
    }
  }
  

  public LineFunction(double slope, double intercept) {
    this.slope = slope ;
    this.intercept = intercept ;
  }
  
  public LineFunction() {
    this(0.0, 0.0) ;
  }
  
  public LineFunction(double x1, double y1, double x2, double y2) {
    setFromPoints(x1, y1, x2, y2) ;
  }
  
  public LineFunction(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2) {
    this(p1.getX(), p1.getY(), p2.getX(), p2.getY()) ;
  }
  
  public LineFunction(java.awt.geom.Line2D l) {
    this(l.getP1(), l.getP2()) ;
  }
}