001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.geom;
027
028 import java.io.Serializable;
029
030 /**
031 * The <code>Point2D</code> class defines a point representing a location
032 * in {@code (x,y)} coordinate space.
033 * <p>
034 * This class is only the abstract superclass for all objects that
035 * store a 2D coordinate.
036 * The actual storage representation of the coordinates is left to
037 * the subclass.
038 *
039 * @version 1.27, 05/05/07
040 * @author Jim Graham
041 * @since 1.2
042 */
043 public abstract class Point2D implements Cloneable {
044
045 /**
046 * The <code>Float</code> class defines a point specified in float
047 * precision.
048 * @since 1.2
049 */
050 public static class Float extends Point2D implements Serializable {
051 /**
052 * The X coordinate of this <code>Point2D</code>.
053 * @since 1.2
054 * @serial
055 */
056 public float x;
057
058 /**
059 * The Y coordinate of this <code>Point2D</code>.
060 * @since 1.2
061 * @serial
062 */
063 public float y;
064
065 /**
066 * Constructs and initializes a <code>Point2D</code> with
067 * coordinates (0, 0).
068 * @since 1.2
069 */
070 public Float() {
071 }
072
073 /**
074 * Constructs and initializes a <code>Point2D</code> with
075 * the specified coordinates.
076 *
077 * @param x the X coordinate of the newly
078 * constructed <code>Point2D</code>
079 * @param y the Y coordinate of the newly
080 * constructed <code>Point2D</code>
081 * @since 1.2
082 */
083 public Float(float x, float y) {
084 this .x = x;
085 this .y = y;
086 }
087
088 /**
089 * {@inheritDoc}
090 * @since 1.2
091 */
092 public double getX() {
093 return (double) x;
094 }
095
096 /**
097 * {@inheritDoc}
098 * @since 1.2
099 */
100 public double getY() {
101 return (double) y;
102 }
103
104 /**
105 * {@inheritDoc}
106 * @since 1.2
107 */
108 public void setLocation(double x, double y) {
109 this .x = (float) x;
110 this .y = (float) y;
111 }
112
113 /**
114 * Sets the location of this <code>Point2D</code> to the
115 * specified <code>float</code> coordinates.
116 *
117 * @param x the new X coordinate of this {@code Point2D}
118 * @param y the new Y coordinate of this {@code Point2D}
119 * @since 1.2
120 */
121 public void setLocation(float x, float y) {
122 this .x = x;
123 this .y = y;
124 }
125
126 /**
127 * Returns a <code>String</code> that represents the value
128 * of this <code>Point2D</code>.
129 * @return a string representation of this <code>Point2D</code>.
130 * @since 1.2
131 */
132 public String toString() {
133 return "Point2D.Float[" + x + ", " + y + "]";
134 }
135
136 /*
137 * JDK 1.6 serialVersionUID
138 */
139 private static final long serialVersionUID = -2870572449815403710L;
140 }
141
142 /**
143 * The <code>Double</code> class defines a point specified in
144 * <code>double</code> precision.
145 * @since 1.2
146 */
147 public static class Double extends Point2D implements Serializable {
148 /**
149 * The X coordinate of this <code>Point2D</code>.
150 * @since 1.2
151 * @serial
152 */
153 public double x;
154
155 /**
156 * The Y coordinate of this <code>Point2D</code>.
157 * @since 1.2
158 * @serial
159 */
160 public double y;
161
162 /**
163 * Constructs and initializes a <code>Point2D</code> with
164 * coordinates (0, 0).
165 * @since 1.2
166 */
167 public Double() {
168 }
169
170 /**
171 * Constructs and initializes a <code>Point2D</code> with the
172 * specified coordinates.
173 *
174 * @param x the X coordinate of the newly
175 * constructed <code>Point2D</code>
176 * @param y the Y coordinate of the newly
177 * constructed <code>Point2D</code>
178 * @since 1.2
179 */
180 public Double(double x, double y) {
181 this .x = x;
182 this .y = y;
183 }
184
185 /**
186 * {@inheritDoc}
187 * @since 1.2
188 */
189 public double getX() {
190 return x;
191 }
192
193 /**
194 * {@inheritDoc}
195 * @since 1.2
196 */
197 public double getY() {
198 return y;
199 }
200
201 /**
202 * {@inheritDoc}
203 * @since 1.2
204 */
205 public void setLocation(double x, double y) {
206 this .x = x;
207 this .y = y;
208 }
209
210 /**
211 * Returns a <code>String</code> that represents the value
212 * of this <code>Point2D</code>.
213 * @return a string representation of this <code>Point2D</code>.
214 * @since 1.2
215 */
216 public String toString() {
217 return "Point2D.Double[" + x + ", " + y + "]";
218 }
219
220 /*
221 * JDK 1.6 serialVersionUID
222 */
223 private static final long serialVersionUID = 6150783262733311327L;
224 }
225
226 /**
227 * This is an abstract class that cannot be instantiated directly.
228 * Type-specific implementation subclasses are available for
229 * instantiation and provide a number of formats for storing
230 * the information necessary to satisfy the various accessor
231 * methods below.
232 *
233 * @see java.awt.geom.Point2D.Float
234 * @see java.awt.geom.Point2D.Double
235 * @see java.awt.Point
236 * @since 1.2
237 */
238 protected Point2D() {
239 }
240
241 /**
242 * Returns the X coordinate of this <code>Point2D</code> in
243 * <code>double</code> precision.
244 * @return the X coordinate of this <code>Point2D</code>.
245 * @since 1.2
246 */
247 public abstract double getX();
248
249 /**
250 * Returns the Y coordinate of this <code>Point2D</code> in
251 * <code>double</code> precision.
252 * @return the Y coordinate of this <code>Point2D</code>.
253 * @since 1.2
254 */
255 public abstract double getY();
256
257 /**
258 * Sets the location of this <code>Point2D</code> to the
259 * specified <code>double</code> coordinates.
260 *
261 * @param x the new X coordinate of this {@code Point2D}
262 * @param y the new Y coordinate of this {@code Point2D}
263 * @since 1.2
264 */
265 public abstract void setLocation(double x, double y);
266
267 /**
268 * Sets the location of this <code>Point2D</code> to the same
269 * coordinates as the specified <code>Point2D</code> object.
270 * @param p the specified <code>Point2D</code> to which to set
271 * this <code>Point2D</code>
272 * @since 1.2
273 */
274 public void setLocation(Point2D p) {
275 setLocation(p.getX(), p.getY());
276 }
277
278 /**
279 * Returns the square of the distance between two points.
280 *
281 * @param x1 the X coordinate of the first specified point
282 * @param y1 the Y coordinate of the first specified point
283 * @param x2 the X coordinate of the second specified point
284 * @param y2 the Y coordinate of the second specified point
285 * @return the square of the distance between the two
286 * sets of specified coordinates.
287 * @since 1.2
288 */
289 public static double distanceSq(double x1, double y1, double x2,
290 double y2) {
291 x1 -= x2;
292 y1 -= y2;
293 return (x1 * x1 + y1 * y1);
294 }
295
296 /**
297 * Returns the distance between two points.
298 *
299 * @param x1 the X coordinate of the first specified point
300 * @param y1 the Y coordinate of the first specified point
301 * @param x2 the X coordinate of the second specified point
302 * @param y2 the Y coordinate of the second specified point
303 * @return the distance between the two sets of specified
304 * coordinates.
305 * @since 1.2
306 */
307 public static double distance(double x1, double y1, double x2,
308 double y2) {
309 x1 -= x2;
310 y1 -= y2;
311 return Math.sqrt(x1 * x1 + y1 * y1);
312 }
313
314 /**
315 * Returns the square of the distance from this
316 * <code>Point2D</code> to a specified point.
317 *
318 * @param px the X coordinate of the specified point to be measured
319 * against this <code>Point2D</code>
320 * @param py the Y coordinate of the specified point to be measured
321 * against this <code>Point2D</code>
322 * @return the square of the distance between this
323 * <code>Point2D</code> and the specified point.
324 * @since 1.2
325 */
326 public double distanceSq(double px, double py) {
327 px -= getX();
328 py -= getY();
329 return (px * px + py * py);
330 }
331
332 /**
333 * Returns the square of the distance from this
334 * <code>Point2D</code> to a specified <code>Point2D</code>.
335 *
336 * @param pt the specified point to be measured
337 * against this <code>Point2D</code>
338 * @return the square of the distance between this
339 * <code>Point2D</code> to a specified <code>Point2D</code>.
340 * @since 1.2
341 */
342 public double distanceSq(Point2D pt) {
343 double px = pt.getX() - this .getX();
344 double py = pt.getY() - this .getY();
345 return (px * px + py * py);
346 }
347
348 /**
349 * Returns the distance from this <code>Point2D</code> to
350 * a specified point.
351 *
352 * @param px the X coordinate of the specified point to be measured
353 * against this <code>Point2D</code>
354 * @param py the Y coordinate of the specified point to be measured
355 * against this <code>Point2D</code>
356 * @return the distance between this <code>Point2D</code>
357 * and a specified point.
358 * @since 1.2
359 */
360 public double distance(double px, double py) {
361 px -= getX();
362 py -= getY();
363 return Math.sqrt(px * px + py * py);
364 }
365
366 /**
367 * Returns the distance from this <code>Point2D</code> to a
368 * specified <code>Point2D</code>.
369 *
370 * @param pt the specified point to be measured
371 * against this <code>Point2D</code>
372 * @return the distance between this <code>Point2D</code> and
373 * the specified <code>Point2D</code>.
374 * @since 1.2
375 */
376 public double distance(Point2D pt) {
377 double px = pt.getX() - this .getX();
378 double py = pt.getY() - this .getY();
379 return Math.sqrt(px * px + py * py);
380 }
381
382 /**
383 * Creates a new object of the same class and with the
384 * same contents as this object.
385 * @return a clone of this instance.
386 * @exception OutOfMemoryError if there is not enough memory.
387 * @see java.lang.Cloneable
388 * @since 1.2
389 */
390 public Object clone() {
391 try {
392 return super .clone();
393 } catch (CloneNotSupportedException e) {
394 // this shouldn't happen, since we are Cloneable
395 throw new InternalError();
396 }
397 }
398
399 /**
400 * Returns the hashcode for this <code>Point2D</code>.
401 * @return a hash code for this <code>Point2D</code>.
402 */
403 public int hashCode() {
404 long bits = java.lang.Double.doubleToLongBits(getX());
405 bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
406 return (((int) bits) ^ ((int) (bits >> 32)));
407 }
408
409 /**
410 * Determines whether or not two points are equal. Two instances of
411 * <code>Point2D</code> are equal if the values of their
412 * <code>x</code> and <code>y</code> member fields, representing
413 * their position in the coordinate space, are the same.
414 * @param obj an object to be compared with this <code>Point2D</code>
415 * @return <code>true</code> if the object to be compared is
416 * an instance of <code>Point2D</code> and has
417 * the same values; <code>false</code> otherwise.
418 * @since 1.2
419 */
420 public boolean equals(Object obj) {
421 if (obj instanceof Point2D) {
422 Point2D p2d = (Point2D) obj;
423 return (getX() == p2d.getX()) && (getY() == p2d.getY());
424 }
425 return super.equals(obj);
426 }
427 }
|