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