1 7 8 package java.awt.geom; 9 10 22 public abstract class Point2D implements Cloneable { 23 27 public static class Float extends Point2D { 28 32 public float x; 33 34 38 public float y; 39 40 45 public Float() { 46 } 47 48 55 public Float(float x, float y) { 56 this.x = x; 57 this.y = y; 58 } 59 60 66 public double getX() { 67 return (double) x; 68 } 69 70 76 public double getY() { 77 return (double) y; 78 } 79 80 87 public void setLocation(double x, double y) { 88 this.x = (float) x; 89 this.y = (float) y; 90 } 91 92 99 public void setLocation(float x, float y) { 100 this.x = x; 101 this.y = y; 102 } 103 104 110 public String toString() { 111 return "Point2D.Float["+x+", "+y+"]"; 112 } 113 } 114 115 119 public static class Double extends Point2D { 120 124 public double x; 125 126 130 public double y; 131 132 137 public Double() { 138 } 139 140 147 public Double(double x, double y) { 148 this.x = x; 149 this.y = y; 150 } 151 152 158 public double getX() { 159 return x; 160 } 161 162 168 public double getY() { 169 return y; 170 } 171 172 179 public void setLocation(double x, double y) { 180 this.x = x; 181 this.y = y; 182 } 183 184 190 public String toString() { 191 return "Point2D.Double["+x+", "+y+"]"; 192 } 193 } 194 195 206 protected Point2D() { 207 } 208 209 215 public abstract double getX(); 216 217 223 public abstract double getY(); 224 225 231 public abstract void setLocation(double x, double y); 232 233 240 public void setLocation(Point2D p) { 241 setLocation(p.getX(), p.getY()); 242 } 243 244 251 public static double distanceSq(double X1, double Y1, 252 double X2, double Y2) { 253 X1 -= X2; 254 Y1 -= Y2; 255 return (X1 * X1 + Y1 * Y1); 256 } 257 258 265 public static double distance(double X1, double Y1, 266 double X2, double Y2) { 267 X1 -= X2; 268 Y1 -= Y2; 269 return Math.sqrt(X1 * X1 + Y1 * Y1); 270 } 271 272 279 public double distanceSq(double PX, double PY) { 280 PX -= getX(); 281 PY -= getY(); 282 return (PX * PX + PY * PY); 283 } 284 285 292 public double distanceSq(Point2D pt) { 293 double PX = pt.getX() - this.getX(); 294 double PY = pt.getY() - this.getY(); 295 return (PX * PX + PY * PY); 296 } 297 298 306 public double distance(double PX, double PY) { 307 PX -= getX(); 308 PY -= getY(); 309 return Math.sqrt(PX * PX + PY * PY); 310 } 311 312 319 public double distance(Point2D pt) { 320 double PX = pt.getX() - this.getX(); 321 double PY = pt.getY() - this.getY(); 322 return Math.sqrt(PX * PX + PY * PY); 323 } 324 325 333 public Object clone() { 334 try { 335 return super.clone(); 336 } catch (CloneNotSupportedException e) { 337 throw new InternalError (); 339 } 340 } 341 342 346 public int hashCode() { 347 long bits = java.lang.Double.doubleToLongBits(getX()); 348 bits ^= java.lang.Double.doubleToLongBits(getY()) * 31; 349 return (((int) bits) ^ ((int) (bits >> 32))); 350 } 351 352 363 public boolean equals(Object obj) { 364 if (obj instanceof Point2D ) { 365 Point2D p2d = (Point2D ) obj; 366 return (getX() == p2d.getX()) && (getY() == p2d.getY()); 367 } 368 return super.equals(obj); 369 } 370 } 371 | Popular Tags |