/[classpath]/classpath/java/awt/geom/Point2D.java
ViewVC logotype

Diff of /classpath/java/awt/geom/Point2D.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by mark, Tue Jan 22 22:26:59 2002 UTC revision 1.3 by ericb, Thu Mar 21 07:58:00 2002 UTC
# Line 1  Line 1 
1  /* Copyright (C) 1999, 2000, 2002  Free Software Foundation  /* Point2D.java -- generic point in 2-D space
2       Copyright (C) 1999, 2000, 2002 Free Software Foundation
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 37  exception statement from your version. * Line 38  exception statement from your version. *
38  package java.awt.geom;  package java.awt.geom;
39    
40  /**  /**
41     * This class implements a generic point in 2D Cartesian space. The storage
42     * representation is left up to the subclass. Point includes two useful
43     * nested classes, for float and double storage respectively.
44     *
45   * @author Per Bothner <bothner@cygnus.com>   * @author Per Bothner <bothner@cygnus.com>
46   * @date February 8, 1999.   * @author Eric Blake <ebb9@email.byu.edu>
47     * @since 1.2
48     * @status updated to 1.4
49   */   */
   
 /* Written using "Java Class Libraries", 2nd edition, plus online  
  * API docs for JDK 1.2 beta from http://www.javasoft.com.  
  * Status:  Believed complete and correct, except that neither toString  
  * nor hashCode have been compared with JDK output.  
  */  
   
50  public abstract class Point2D implements Cloneable  public abstract class Point2D implements Cloneable
51  {  {
52    public abstract double getX();    /**
53    public abstract double getY();     * The default constructor.
54       *
55    public abstract void setLocation (double x, double y);     * @see Point
56       * @see Point2D.Float
57    protected Point2D ()     * @see Point2D.Double
58       */
59      protected Point2D()
60    {    {
61    }    }
62    
63    public void setLocation (Point2D pt)  { setLocation(pt.getX(), pt.getY()); }    /**
64       * Get the X coordinate, in double precision.
65    static public double distanceSq (double X1, double Y1, double X2, double Y2)     *
66    {     * @return the x coordinate
67      X2 -= X1;     */
68      Y2 -= Y1;    public abstract double getX();
     return X2*X2 + Y2*Y2;  
   }  
   
   static public double distance (double X1, double Y1, double X2, double Y2)  
   {  
     return Math.sqrt(distance(X1, Y1, X2, Y2));  
   }  
   
   public double distanceSq (double PX, double PY)  
   {  
     return distanceSq (getX(), PX, getY(), PY);  
   }  
   
   public double distance (double PX, double PY)  
   {  
     return distance (getX(), PX, getY(), PY);  
   }  
   
   public double distanceSq (Point2D pt)  
   {  
     return distanceSq (getX(), pt.getX(), getY(), pt.getY());  
   }  
   
   public double distance (Point2D pt)  
   {  
     return distance (getX(), pt.getX(), getY(), pt.getY());  
   }  
69    
70    public int hashCode() { return (int) getX() ^ (int) getY(); }    /**
71       * Get the Y coordinate, in double precision.
72       *
73       * @return the y coordinate
74       */
75      public abstract double getY();
76    
77      /**
78       * Set the location of this point to the new coordinates. There may be a
79       * loss of precision.
80       *
81       * @param x the new x coordinate
82       * @param y the new y coordinate
83       */
84      public abstract void setLocation(double x, double y);
85    
86      /**
87       * Set the location of this point to the new coordinates. There may be a
88       * loss of precision.
89       *
90       * @param p the point to copy
91       * @throws NullPointerException if p is null
92       */
93      public void setLocation(Point2D p)
94      {
95        setLocation(p.getX(), p.getY());
96      }
97    
98      /**
99       * Return the square of the distance between two points.
100       *
101       * @param x1 the x coordinate of point 1
102       * @param y1 the y coordinate of point 1
103       * @param x2 the x coordinate of point 2
104       * @param y2 the y coordinate of point 2
105       * @return (x2 - x1)^2 + (y2 - y1)^2
106       */
107      public static double distanceSq(double x1, double y1, double x2, double y2)
108      {
109        x2 -= x1;
110        y2 -= y1;
111        return x2 * x2 + y2 * y2;
112      }
113    
114      /**
115       * Return the distance between two points.
116       *
117       * @param x1 the x coordinate of point 1
118       * @param y1 the y coordinate of point 1
119       * @param x2 the x coordinate of point 2
120       * @param y2 the y coordinate of point 2
121       * @return the distance from (x1,y1) to (x2,y2)
122       */
123      static public double distance(double x1, double y1, double x2, double y2)
124      {
125        return Math.sqrt(distance(x1, y1, x2, y2));
126      }
127    
128      /**
129       * Return the square of the distance from this point to the given one.
130       *
131       * @param x the x coordinate of the other point
132       * @param y the y coordinate of the other point
133       * @return the square of the distance
134       */
135      public double distanceSq(double x, double y)
136      {
137        return distanceSq(getX(), x, getY(), y);
138      }
139    
140      /**
141       * Return the square of the distance from this point to the given one.
142       *
143       * @param p the other point
144       * @return the square of the distance
145       * @throws NullPointerException if p is null
146       */
147      public double distanceSq(Point2D p)
148      {
149        return distanceSq(getX(), p.getX(), getY(), p.getY());
150      }
151    
152      /**
153       * Return the distance from this point to the given one.
154       *
155       * @param x the x coordinate of the other point
156       * @param y the y coordinate of the other point
157       * @return the distance
158       */
159      public double distance(double x, double y)
160      {
161        return distance(getX(), x, getY(), y);
162      }
163    
164      /**
165       * Return the distance from this point to the given one.
166       *
167       * @param p the other point
168       * @return the distance
169       * @throws NullPointerException if p is null
170       */
171      public double distance(Point2D p)
172      {
173        return distance(getX(), p.getX(), getY(), p.getY());
174      }
175    
176      /**
177       * Create a new point of the same run-time type with the same contents as
178       * this one.
179       *
180       * @return the clone
181       */
182    public Object clone()    public Object clone()
183    {    {
184      try      try
185      {        {
186        return super.clone ();          return super.clone();
187      }        }
188      catch (CloneNotSupportedException _) {return null;}      catch (CloneNotSupportedException e)
189    }        {
190            throw (Error) new InternalError().initCause(e); // Impossible
191    public boolean equals (Object o)        }
192      }
193    
194      /**
195       * Return the hashcode for this point. The formula is not documented, but
196       * appears to be the same as:
197       * <pre>
198       * long l = Double.doubleToLongBits(getY());
199       * l = l * 31 ^ Double.doubleToLongBits(getX());
200       * return (int) ((l >> 32) ^ l);
201       * </pre>
202       *
203       * @return the hashcode
204       */
205      public int hashCode()
206      {
207        // Talk about a fun time reverse engineering this one!
208        long l = java.lang.Double.doubleToLongBits(getY());
209        l = l * 31 ^ java.lang.Double.doubleToLongBits(getX());
210        return (int) ((l >> 32) ^ l);
211      }
212    
213      /**
214       * Compares to points for equality. This returns true if they have the
215       * same coordinates.
216       *
217       * @param o the point to compare
218       * @return true if it is equal
219       */
220      public boolean equals(Object o)
221    {    {
222      if (! (o instanceof Point2D))      if (! (o instanceof Point2D))
223        return false;        return false;
224      Point2D p = (Point2D) o;      Point2D p = (Point2D) o;
225      return getX () == p.getX () && getY () == p.getY ();      return getX() == p.getX() && getY() == p.getY();
226    }    }
227    
228      /**
229       * This class defines a point in <code>double</code> precision.
230       *
231       * @author Eric Blake <ebb9@email.byu.edu>
232       * @since 1.2
233       * @status updated to 1.4
234       */
235    public static class Double extends Point2D    public static class Double extends Point2D
236    {    {
237        /** The X coordinate. */
238      public double x;      public double x;
239    
240        /** The Y coordinate. */
241      public double y;      public double y;
242    
243      public Double ()      /**
244         * Create a new point at (0,0).
245         */
246        public Double()
247      {      {
       x = 0;  
       y = 0;  
248      }      }
249    
250      public Double (double x, double y)      /**
251         * Create a new point at (x,y).
252         *
253         * @param x the x coordinate
254         * @param y the y coordinate
255         */
256        public Double(double x, double y)
257      {      {
258        this.x = x;        this.x = x;
259        this.y = y;        this.y = y;
260      }      }
261    
262      public double getX ()      /**
263         * Return the x coordinate.
264         *
265         * @return the x coordinate
266         */
267        public double getX()
268      {      {
269        return x;        return x;
270      }      }
271    
272      public double getY ()      /**
273         * Return the y coordinate.
274         *
275         * @return the y coordinate
276         */
277        public double getY()
278      {      {
279        return y;        return y;
280      }      }
281    
282      public void setLocation (double x, double y)      /**
283         * Sets the location of this point.
284         *
285         * @param x the new x coordinate
286         * @param y the new y coordinate
287         */
288        public void setLocation(double x, double y)
289      {      {
290        this.x = x;        this.x = x;
291        this.y = y;        this.y = y;
292      }      }
293    
294      public String toString ()      /**
295      {       * Returns a string representation of this object. The format is:
296        return "(" + x + ", " + y + ")";       * <code>"Point2D.Double[" + x + ", " + y + ']'</code>.
297      }       *
298    }       * @return a string representation of this object
299         */
300        public String toString()
301        {
302          return "Point2D.Double[" + x + ", " + y + ']';
303        }
304      } // class Double
305    
306      /**
307       * This class defines a point in <code>float</code> precision.
308       *
309       * @author Eric Blake <ebb9@email.byu.edu>
310       * @since 1.2
311       * @status updated to 1.4
312       */
313    public static class Float extends Point2D    public static class Float extends Point2D
314    {    {
315        /** The X coordinate. */
316      public float x;      public float x;
317    
318        /** The Y coordinate. */
319      public float y;      public float y;
320    
321      public Float ()      /**
322         * Create a new point at (0,0).
323         */
324        public Float()
325      {      {
       x = 0;  
       y = 0;  
326      }      }
327    
328      public Float (float x, float y)      /**
329         * Create a new point at (x,y).
330         *
331         * @param x the x coordinate
332         * @param y the y coordinate
333         */
334        public Float(float x, float y)
335      {      {
336        this.x = x;        this.x = x;
337        this.y = y;        this.y = y;
338      }      }
339    
340      public double getX ()      /**
341         * Return the x coordinate.
342         *
343         * @return the x coordinate
344         */
345        public double getX()
346      {      {
347        return x;        return x;
348      }      }
349    
350      public double getY ()      /**
351         * Return the y coordinate.
352         *
353         * @return the y coordinate
354         */
355        public double getY()
356      {      {
357        return y;        return y;
358      }      }
359    
360      public void setLocation (double x, double y)      /**
361         * Sets the location of this point.
362         *
363         * @param x the new x coordinate
364         * @param y the new y coordinate
365         */
366        public void setLocation(double x, double y)
367      {      {
368        this.x = (float) x;        this.x = (float) x;
369        this.y = (float) y;        this.y = (float) y;
370      }      }
371    
372      public void setLocation (float x, float y)      /**
373         * Sets the location of this point.
374         *
375         * @param x the new x coordinate
376         * @param y the new y coordinate
377         */
378        public void setLocation(float x, float y)
379      {      {
380        this.x = x;        this.x = x;
381        this.y = y;        this.y = y;
382      }      }
383    
384      public String toString ()      /**
385         * Returns a string representation of this object. The format is:
386         * <code>"Point2D.Float[" + x + ", " + y + ']'</code>.
387         *
388         * @return a string representation of this object
389         */
390        public String toString()
391      {      {
392        return "(" + x + ", " + y + ")";        return "Point2D.Float[" + x + ", " + y + ']';
393      }      }
394    }    } // class Float
395  }  } // class Point2D

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26