/*
 * Line.java    2.0 97/06/10
 * Copyright (c) 1997, B. F. Caviness
 *
 * This software may be copied/modified for any non-commercial educational use.
 */

package sg;

/**
 * The <code>class Line</code> defines a geometric object line.
 *
 * @version   2.0, 10 June 1997
 * @author    B. F. Caviness
 */

public class Line implements Polygon{

  Pixel p1 = new Pixel(),	// one end point of line. Note initializations
        p2 = new Pixel();	// other end point

  /**
   * Default zero-parameter constructor that sets the line
   * to the default line consisting of a single pixel at (0,0).
   */
  public Line()			// zero-param Constructor
  { }				// Line constructor initializes both pts
				// to (0,0)

  /**
   * Two-parameter constructor
   *
   * @param start starting pixel of the line
   *
   * @param end ending pixel of the line
   */
  public Line( Pixel start, Pixel end )	// 2-param constructor
  {
     p1.x = start.x; p1.y = start.y;
     p2.x = end.x;   p2.y = end.y;
  }

  /**
   * Computes the slope of the line.
   *
   * @return double the slope.  If the line is vertical, return
   *         Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY as 
   *         appropriate.
   */
  public double slope()		// Compute the slope of a line
  {
     // Must negate usual formula for slope because y coordinate
     // increases as it moves down the page.

     if ( p2.x == p1.x )	// Line is vertical
         return ( p1.y - p2.y >= 0 ? Double.POSITIVE_INFINITY :
                                     Double.NEGATIVE_INFINITY );

     return ( - (double)(p2.y - p1.y)/(p2.x - p1.x) );
  }

  /**
   * Returns <code>true</code> if the slope of the object line is the same as
   * the slope of the line L.
   *
   * @param L the line whose slope is to be compared to the slope
   *          of the object line.
   * @return <code>true</code> if the slope of the object is the same as
   *         the slope of the argument; <code>false</code> otherwise.
   */
  public boolean isParallel( Line L )	// Return true if instance
  {					// is parallel to L.
     return ( this.slope() == L.slope() );
  }

  /**
   * Draws the line on the graphic device g.
   *
   * @param g the simple graphics device on which to draw.
   */
  public void paint( Graphics g )	// Paint line on grid
  { g.drawLine( p1.x,p1.y, p2.x,p2.y ); }
}
