1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
/* * @(#)Point2D.java 1.21 06/02/24 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt.geom; import java.io.Serializable; /** * The <code>Point2D</code> class defines a point representing a location * in {@code (x,y)} coordinate space. * <p> * This class is only the abstract superclass for all objects that * store a 2D coordinate. * The actual storage representation of the coordinates is left to * the subclass. * * @version 1.21, 02/24/06 * @author Jim Graham * @since 1.2 */ public abstract class Point2D implements Cloneable { /** * The <code>Float</code> class defines a point specified in float * precision. * @since 1.2 */ public static class Float extends Point2D implements Serializable { /** * The X coordinate of this <code>Point2D</code>. * @since 1.2 * @serial */ public float x; /** * The Y coordinate of this <code>Point2D</code>. * @since 1.2 * @serial */ public float y; /** * Constructs and initializes a <code>Point2D</code> with * coordinates (0, 0). * @since 1.2 */ public Float() { } /** * Constructs and initializes a <code>Point2D</code> with * the specified coordinates. * * @param x the X coordinate of the newly * constructed <code>Point2D</code> * @param y the Y coordinate of the newly * constructed <code>Point2D</code> * @since 1.2 */ public Float(float x, float y) { this.x = x; this.y = y; } /** * {@inheritDoc} * @since 1.2 */ public double getX() { return (double) x; } /** * {@inheritDoc} * @since 1.2 */ public double getY() { return (double) y; } /** * {@inheritDoc} * @since 1.2 */ public void setLocation(double x, double y) { this.x = (float) x; this.y = (float) y; } /** * Sets the location of this <code>Point2D</code> to the * specified <code>float</code> coordinates. * * @param x the new X coordinate of this {@code Point2D} * @param y the new Y coordinate of this {@code Point2D} * @since 1.2 */ public void setLocation(float x, float y) { this.x = x; this.y = y; } /** * Returns a <code>String</code> that represents the value * of this <code>Point2D</code>. * @return a string representation of this <code>Point2D</code>. * @since 1.2 */ public String toString() { return "Point2D.Float["+x+", "+y+"]"; } /* * JDK 1.6 serialVersionUID */ private static final long serialVersionUID = -2870572449815403710L; } /** * The <code>Double</code> class defines a point specified in * <code>double</code> precision. * @since 1.2 */ public static class Double extends Point2D implements Serializable { /** * The X coordinate of this <code>Point2D</code>. * @since 1.2 * @serial */ public double x; /** * The Y coordinate of this <code>Point2D</code>. * @since 1.2 * @serial */ public double y; /** * Constructs and initializes a <code>Point2D</code> with * coordinates (0, 0). * @since 1.2 */ public Double() { } /** * Constructs and initializes a <code>Point2D</code> with the * specified coordinates. * * @param x the X coordinate of the newly * constructed <code>Point2D</code> * @param y the Y coordinate of the newly * constructed <code>Point2D</code> * @since 1.2 */ public Double(double x, double y) { this.x = x; this.y = y; } /** * {@inheritDoc} * @since 1.2 */ public double getX() { return x; } /** * {@inheritDoc} * @since 1.2 */ public double getY() { return y; } /** * {@inheritDoc} * @since 1.2 */ public void setLocation(double x, double y) { this.x = x; this.y = y; } /** * Returns a <code>String</code> that represents the value * of this <code>Point2D</code>. * @return a string representation of this <code>Point2D</code>. * @since 1.2 */ public String toString() { return "Point2D.Double["+x+", "+y+"]"; } /* * JDK 1.6 serialVersionUID */ private static final long serialVersionUID = 6150783262733311327L; } /** * This is an abstract class that cannot be instantiated directly. * Type-specific implementation subclasses are available for * instantiation and provide a number of formats for storing * the information necessary to satisfy the various accessor * methods below. * * @see java.awt.geom.Point2D.Float * @see java.awt.geom.Point2D.Double * @see java.awt.Point * @since 1.2 */ protected Point2D() { } /** * Returns the X coordinate of this <code>Point2D</code> in * <code>double</code> precision. * @return the X coordinate of this <code>Point2D</code>. * @since 1.2 */ public abstract double getX(); /** * Returns the Y coordinate of this <code>Point2D</code> in * <code>double</code> precision. * @return the Y coordinate of this <code>Point2D</code>. * @since 1.2 */ public abstract double getY(); /** * Sets the location of this <code>Point2D</code> to the * specified <code>double</code> coordinates. * * @param x the new X coordinate of this {@code Point2D} * @param y the new Y coordinate of this {@code Point2D} * @since 1.2 */ public abstract void setLocation(double x, double y); /** * Sets the location of this <code>Point2D</code> to the same * coordinates as the specified <code>Point2D</code> object. * @param p the specified <code>Point2D</code> to which to set * this <code>Point2D</code> * @since 1.2 */ public void setLocation(Point2D p) { setLocation(p.getX(), p.getY()); } /** * Returns the square of the distance between two points. * * @param x1 the X coordinate of the first specified point * @param y1 the Y coordinate of the first specified point * @param x2 the X coordinate of the second specified point * @param y2 the Y coordinate of the second specified point * @return the square of the distance between the two * sets of specified coordinates. * @since 1.2 */ public static double distanceSq(double x1, double y1, double x2, double y2) { x1 -= x2; y1 -= y2; return (x1 * x1 + y1 * y1); } /** * Returns the distance between two points. * * @param x1 the X coordinate of the first specified point * @param y1 the Y coordinate of the first specified point * @param x2 the X coordinate of the second specified point * @param y2 the Y coordinate of the second specified point * @return the distance between the two sets of specified * coordinates. * @since 1.2 */ public static double distance(double x1, double y1, double x2, double y2) { x1 -= x2; y1 -= y2; return Math.sqrt(x1 * x1 + y1 * y1); } /** * Returns the square of the distance from this * <code>Point2D</code> to a specified point. * * @param px the X coordinate of the specified point to be measured * against this <code>Point2D</code> * @param py the Y coordinate of the specified point to be measured * against this <code>Point2D</code> * @return the square of the distance between this * <code>Point2D</code> and the specified point. * @since 1.2 */ public double distanceSq(double px, double py) { px -= getX(); py -= getY(); return (px * px + py * py); } /** * Returns the square of the distance from this * <code>Point2D</code> to a specified <code>Point2D</code>. * * @param pt the specified point to be measured * against this <code>Point2D</code> * @return the square of the distance between this * <code>Point2D</code> to a specified <code>Point2D</code>. * @since 1.2 */ public double distanceSq(Point2D pt) { double px = pt.getX() - this.getX(); double py = pt.getY() - this.getY(); return (px * px + py * py); } /** * Returns the distance from this <code>Point2D</code> to * a specified point. * * @param px the X coordinate of the specified point to be measured * against this <code>Point2D</code> * @param py the Y coordinate of the specified point to be measured * against this <code>Point2D</code> * @return the distance between this <code>Point2D</code> * and a specified point. * @since 1.2 */ public double distance(double px, double py) { px -= getX(); py -= getY(); return Math.sqrt(px * px + py * py); } /** * Returns the distance from this <code>Point2D</code> to a * specified <code>Point2D</code>. * * @param pt the specified point to be measured * against this <code>Point2D</code> * @return the distance between this <code>Point2D</code> and * the specified <code>Point2D</code>. * @since 1.2 */ public double distance(Point2D pt) { double px = pt.getX() - this.getX(); double py = pt.getY() - this.getY(); return Math.sqrt(px * px + py * py); } /** * Creates a new object of the same class and with the * same contents as this object. * @return a clone of this instance. * @exception OutOfMemoryError if there is not enough memory. * @see java.lang.Cloneable * @since 1.2 */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } /** * Returns the hashcode for this <code>Point2D</code>. * @return a hash code for this <code>Point2D</code>. */ public int hashCode() { long bits = java.lang.Double.doubleToLongBits(getX()); bits ^= java.lang.Double.doubleToLongBits(getY()) * 31; return (((int) bits) ^ ((int) (bits >> 32))); } /** * Determines whether or not two points are equal. Two instances of * <code>Point2D</code> are equal if the values of their * <code>x</code> and <code>y</code> member fields, representing * their position in the coordinate space, are the same. * @param obj an object to be compared with this <code>Point2D</code> * @return <code>true</code> if the object to be compared is * an instance of <code>Point2D</code> and has * the same values; <code>false</code> otherwise. * @since 1.2 */ public boolean equals(Object obj) { if (obj instanceof Point2D) { Point2D p2d = (Point2D) obj; return (getX() == p2d.getX()) && (getY() == p2d.getY()); } return super.equals(obj); } }