001/* Point2D.java -- generic point in 2-D space
002   Copyright (C) 1999, 2000, 2002, 2004, 2006,  Free Software Foundation
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.awt.geom;
040
041/**
042 * This class implements a generic point in 2D Cartesian space. The storage
043 * representation is left up to the subclass. Point includes two useful
044 * nested classes, for float and double storage respectively.
045 *
046 * @author Per Bothner (bothner@cygnus.com)
047 * @author Eric Blake (ebb9@email.byu.edu)
048 * @since 1.2
049 * @status updated to 1.4
050 */
051public abstract class Point2D implements Cloneable
052{
053  /**
054   * The default constructor.
055   *
056   * @see java.awt.Point
057   * @see Point2D.Float
058   * @see Point2D.Double
059   */
060  protected Point2D()
061  {
062  }
063
064  /**
065   * Get the X coordinate, in double precision.
066   *
067   * @return the x coordinate
068   */
069  public abstract double getX();
070
071  /**
072   * Get the Y coordinate, in double precision.
073   *
074   * @return the y coordinate
075   */
076  public abstract double getY();
077
078  /**
079   * Set the location of this point to the new coordinates. There may be a
080   * loss of precision.
081   *
082   * @param x the new x coordinate
083   * @param y the new y coordinate
084   */
085  public abstract void setLocation(double x, double y);
086
087  /**
088   * Set the location of this point to the new coordinates. There may be a
089   * loss of precision.
090   *
091   * @param p the point to copy
092   * @throws NullPointerException if p is null
093   */
094  public void setLocation(Point2D p)
095  {
096    setLocation(p.getX(), p.getY());
097  }
098
099  /**
100   * Return the square of the distance between two points.
101   *
102   * @param x1 the x coordinate of point 1
103   * @param y1 the y coordinate of point 1
104   * @param x2 the x coordinate of point 2
105   * @param y2 the y coordinate of point 2
106   * @return (x2 - x1)^2 + (y2 - y1)^2
107   */
108  public static double distanceSq(double x1, double y1, double x2, double y2)
109  {
110    x2 -= x1;
111    y2 -= y1;
112    return x2 * x2 + y2 * y2;
113  }
114
115  /**
116   * Return the distance between two points.
117   *
118   * @param x1 the x coordinate of point 1
119   * @param y1 the y coordinate of point 1
120   * @param x2 the x coordinate of point 2
121   * @param y2 the y coordinate of point 2
122   * @return the distance from (x1,y1) to (x2,y2)
123   */
124  public static double distance(double x1, double y1, double x2, double y2)
125  {
126    return Math.sqrt(distanceSq(x1, y1, x2, y2));
127  }
128
129  /**
130   * Return the square of the distance from this point to the given one.
131   *
132   * @param x the x coordinate of the other point
133   * @param y the y coordinate of the other point
134   * @return the square of the distance
135   */
136  public double distanceSq(double x, double y)
137  {
138    return distanceSq(getX(), getY(), x, y);
139  }
140
141  /**
142   * Return the square of the distance from this point to the given one.
143   *
144   * @param p the other point
145   * @return the square of the distance
146   * @throws NullPointerException if p is null
147   */
148  public double distanceSq(Point2D p)
149  {
150    return distanceSq(getX(), getY(), p.getX(), p.getY());
151  }
152
153  /**
154   * Return the distance from this point to the given one.
155   *
156   * @param x the x coordinate of the other point
157   * @param y the y coordinate of the other point
158   * @return the distance
159   */
160  public double distance(double x, double y)
161  {
162    return distance(getX(), getY(), x, y);
163  }
164
165  /**
166   * Return the distance from this point to the given one.
167   *
168   * @param p the other point
169   * @return the distance
170   * @throws NullPointerException if p is null
171   */
172  public double distance(Point2D p)
173  {
174    return distance(getX(), getY(), p.getX(), p.getY());
175  }
176
177  /**
178   * Create a new point of the same run-time type with the same contents as
179   * this one.
180   *
181   * @return the clone
182   */
183  public Object clone()
184  {
185    try
186      {
187        return super.clone();
188      }
189    catch (CloneNotSupportedException e)
190      {
191        throw (Error) new InternalError().initCause(e); // Impossible
192      }
193  }
194
195  /**
196   * Return the hashcode for this point. The formula is not documented, but
197   * appears to be the same as:
198   * <pre>
199   * long l = Double.doubleToLongBits(getY());
200   * l = l * 31 ^ Double.doubleToLongBits(getX());
201   * return (int) ((l >> 32) ^ l);
202   * </pre>
203   *
204   * @return the hashcode
205   */
206  public int hashCode()
207  {
208    // Talk about a fun time reverse engineering this one!
209    long l = java.lang.Double.doubleToLongBits(getY());
210    l = l * 31 ^ java.lang.Double.doubleToLongBits(getX());
211    return (int) ((l >> 32) ^ l);
212  }
213
214  /**
215   * Compares two points for equality. This returns true if they have the
216   * same coordinates.
217   *
218   * @param o the point to compare
219   * @return true if it is equal
220   */
221  public boolean equals(Object o)
222  {
223    if (! (o instanceof Point2D))
224      return false;
225    Point2D p = (Point2D) o;
226    return getX() == p.getX() && getY() == p.getY();
227  }
228
229  /**
230   * This class defines a point in <code>double</code> precision.
231   *
232   * @author Eric Blake (ebb9@email.byu.edu)
233   * @since 1.2
234   * @status updated to 1.4
235   */
236  public static class Double extends Point2D
237  {
238    /** The X coordinate. */
239    public double x;
240
241    /** The Y coordinate. */
242    public double y;
243
244    /**
245     * Create a new point at (0,0).
246     */
247    public Double()
248    {
249    }
250
251    /**
252     * Create a new point at (x,y).
253     *
254     * @param x the x coordinate
255     * @param y the y coordinate
256     */
257    public Double(double x, double y)
258    {
259      this.x = x;
260      this.y = y;
261    }
262
263    /**
264     * Return the x coordinate.
265     *
266     * @return the x coordinate
267     */
268    public double getX()
269    {
270      return x;
271    }
272
273    /**
274     * Return the y coordinate.
275     *
276     * @return the y coordinate
277     */
278    public double getY()
279    {
280      return y;
281    }
282
283    /**
284     * Sets the location of this point.
285     *
286     * @param x the new x coordinate
287     * @param y the new y coordinate
288     */
289    public void setLocation(double x, double y)
290    {
291      this.x = x;
292      this.y = y;
293    }
294
295    /**
296     * Returns a string representation of this object. The format is:
297     * <code>"Point2D.Double[" + x + ", " + y + ']'</code>.
298     *
299     * @return a string representation of this object
300     */
301    public String toString()
302    {
303      return "Point2D.Double[" + x + ", " + y + ']';
304    }
305  } // class Double
306
307  /**
308   * This class defines a point in <code>float</code> precision.
309   *
310   * @author Eric Blake (ebb9@email.byu.edu)
311   * @since 1.2
312   * @status updated to 1.4
313   */
314  public static class Float extends Point2D
315  {
316    /** The X coordinate. */
317    public float x;
318
319    /** The Y coordinate. */
320    public float y;
321
322    /**
323     * Create a new point at (0,0).
324     */
325    public Float()
326    {
327    }
328
329    /**
330     * Create a new point at (x,y).
331     *
332     * @param x the x coordinate
333     * @param y the y coordinate
334     */
335    public Float(float x, float y)
336    {
337      this.x = x;
338      this.y = y;
339    }
340
341    /**
342     * Return the x coordinate.
343     *
344     * @return the x coordinate
345     */
346    public double getX()
347    {
348      return x;
349    }
350
351    /**
352     * Return the y coordinate.
353     *
354     * @return the y coordinate
355     */
356    public double getY()
357    {
358      return y;
359    }
360
361    /**
362     * Sets the location of this point.
363     *
364     * @param x the new x coordinate
365     * @param y the new y coordinate
366     */
367    public void setLocation(double x, double y)
368    {
369      this.x = (float) x;
370      this.y = (float) y;
371    }
372
373    /**
374     * Sets the location of this point.
375     *
376     * @param x the new x coordinate
377     * @param y the new y coordinate
378     */
379    public void setLocation(float x, float y)
380    {
381      this.x = x;
382      this.y = y;
383    }
384
385    /**
386     * Returns a string representation of this object. The format is:
387     * <code>"Point2D.Float[" + x + ", " + y + ']'</code>.
388     *
389     * @return a string representation of this object
390     */
391    public String toString()
392    {
393      return "Point2D.Float[" + x + ", " + y + ']';
394    }
395  } // class Float
396} // class Point2D