Source for java.awt.geom.Point2D

   1: /* Point2D.java -- generic point in 2-D space
   2:    Copyright (C) 1999, 2000, 2002, 2004, 2006,  Free Software Foundation
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt.geom;
  40: 
  41: /**
  42:  * This class implements a generic point in 2D Cartesian space. The storage
  43:  * representation is left up to the subclass. Point includes two useful
  44:  * nested classes, for float and double storage respectively.
  45:  *
  46:  * @author Per Bothner (bothner@cygnus.com)
  47:  * @author Eric Blake (ebb9@email.byu.edu)
  48:  * @since 1.2
  49:  * @status updated to 1.4
  50:  */
  51: public abstract class Point2D implements Cloneable
  52: {
  53:   /**
  54:    * The default constructor.
  55:    *
  56:    * @see java.awt.Point
  57:    * @see Point2D.Float
  58:    * @see Point2D.Double
  59:    */
  60:   protected Point2D()
  61:   {
  62:   }
  63: 
  64:   /**
  65:    * Get the X coordinate, in double precision.
  66:    *
  67:    * @return the x coordinate
  68:    */
  69:   public abstract double getX();
  70: 
  71:   /**
  72:    * Get the Y coordinate, in double precision.
  73:    *
  74:    * @return the y coordinate
  75:    */
  76:   public abstract double getY();
  77: 
  78:   /**
  79:    * Set the location of this point to the new coordinates. There may be a
  80:    * loss of precision.
  81:    *
  82:    * @param x the new x coordinate
  83:    * @param y the new y coordinate
  84:    */
  85:   public abstract void setLocation(double x, double y);
  86: 
  87:   /**
  88:    * Set the location of this point to the new coordinates. There may be a
  89:    * loss of precision.
  90:    *
  91:    * @param p the point to copy
  92:    * @throws NullPointerException if p is null
  93:    */
  94:   public void setLocation(Point2D p)
  95:   {
  96:     setLocation(p.getX(), p.getY());
  97:   }
  98: 
  99:   /**
 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