/************************************************}
{                                                }
{   JDK 1.1, 1.2, 1.3, 1.4                       }
{   Extensions graphiques compatibles QuickDraw  }
{   P.Chassignet, Ecole Polytechnique, 1998-2004 }
{                                                }
{   dernie`re mise a` jour:  28/5/01             }
{                                                }
{************************************************/

/**
 * This class hides the class <code>java.awt.Point</code> in order 
 * to implement QuickDraw-like definitions for points.
 * Implements also object methods for these points.
 * @version  28 may 2001
 * @author   Philippe Chassignet, Ecole Polytechnique
 * @see      MacLib
 * @see      java.awt.Point
 */
public class Point {

  /**
   * The <em>x</em> coordinate, as named in QuickDraw.
   */
  public short h = 0;

  /**
   * The <em>y</em> coordinate, as named in QuickDraw.
   */
  public short v = 0;

  /**
   * Constructs and initializes a point at coordinates <code>(0,0)</code>.
   */
  public Point() {
  }

  /**
   * Constructs and initializes a point at the given coordinates.
   * @param       h  the <em>h</em> coordinate of the new point.
   * @param       v  the <em>v</em> coordinate of the new point.
   */
  public Point(int h, int v) {
    this.h = (short)h;
    this.v = (short)v;
  }

  /**
   * Converts this point as a <code>java.awt.Point</code> object.
   * @return      a new java.awt.Point object.
   * @see         java.awt.Point
   */
  public java.awt.Point getPoint() {
    return new java.awt.Point(h, v);
  }

  /**
   * Sets the coordinates of this point to the specified location.
   * @param       h  the <em>h</em> coordinate of the new location.
   * @param       v  the <em>v</em> coordinate of the new location.
   */
  public Point setPt(int h, int v) {
    this.h = (short)h;
    this.v = (short)v;
    return this;
  }

  /**
   * Sets the coordinates of this point as specified by the given 
   * <code>Point</code> object.
   * @param       pt  the Point object used to specify the coordinates.
   */
  public Point setPt(Point pt) {
    h = pt.h;
    v = pt.v;
    return this;
  }

  /**
   * Sets the coordinates of this point as specified by the given 
   * <code>java.awt.Point</code> object.
   * @param       pt  the java.awt.Point object that gives the coordinates.
   */
  public Point setPt(java.awt.Point pt) {
    h = (short)pt.x;
    v = (short)pt.y;
    return this;
  }

  /**
   * Translates this point by the displacement vector specified as the 
   * given point.
   * Adds the coordinates of <code>p</code> 
   * to the coordinates of this point.
   * @param       p  the Point object used as a vector to specify the 
   *                 displacement.
   */
  public Point addPt(Point p) {
    h += p.h;
    v += p.v;
    return this;
  }

  /**
   * Translates this point by the displacement vector specified as the 
   * inverse of the given point.
   * Subtracts the coordinates of <code>p</code> 
   * from the coordinates of this point.
   * @param       p  the Point object used as a vector to specify the 
   *                 displacement.
   */
  public Point subPt(Point p) {
    h -= p.h;
    v -= p.v;
    return this;
  }

  /**
   * Determines whether this point and the given point have the same 
   * coordinates.
   * @param       pt  the Point object to compare with.
   * @return      <code>true</code> if both points have same coordinates; 
   *              <code>false</code> otherwise.
   */
  public boolean equalPt(Point pt) {
    return (this.h == pt.h) && (this.v == pt.v);
  }

  /**
   * Determines whether this point and the given point have the same 
   * coordinates.
   * @param       pt  the <code>java.awt.Point</code> object to compare with.
   * @return      <code>true</code> if both points have same coordinates; 
   *              <code>false</code> otherwise.
   * @see         java.awt.Point
   */
  public boolean equalPt(java.awt.Point pt) {
    return (this.h == pt.x) && (this.v == pt.y);
  }

  /**
   * Determines whether the pixel below and to the right of this point 
   * is enclosed in the specified rectangle.
   * @param       r  the Rect object to check with.
   * @return      <code>true</code> if so; <code>false</code> otherwise.
   * @see         Rect#ptInRect(Point)
   */
  public boolean inRect(Rect r) {
    return r.ptInRect(this);
  }

  /**
   * Returns a string representation for the location of this point.
   * @return     the string representation of the coordinates.
   */
  public String toString() {
    return "[h=" + h + ",v=" + v + "]";
  }

  /**
   * Returns a hash code for this point.
   * @return      the hash code build up from the coordinates.
   */
  public int hashCode() {
    return (int)h ^ ((int)v*31);
  }

}  // end class Point

