KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > geom > Point2D


1 /*
2  * @(#)Point2D.java 1.18 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.geom;
9
10 /**
11  * The <code>Point2D</code> class defines a point representing a location
12  * in (x,&nbsp;y) coordinate space.
13  * <p>
14  * This class is only the abstract superclass for all objects that
15  * store a 2D coordinate.
16  * The actual storage representation of the coordinates is left to
17  * the subclass.
18  *
19  * @version 1.18, 12/19/03
20  * @author Jim Graham
21  */

22 public abstract class Point2D implements Cloneable JavaDoc {
23     /**
24      * The <code>Float</code> class defines a point specified in float
25      * precision.
26      */

27     public static class Float extends Point2D JavaDoc {
28     /**
29      * The X coordinate of this <code>Point2D</code>.
30      * @since 1.2
31      */

32     public float x;
33
34     /**
35      * The Y coordinate of this <code>Point2D</code>.
36      * @since 1.2
37      */

38     public float y;
39
40     /**
41      * Constructs and initializes a <code>Point2D</code> with
42          * coordinates (0,&nbsp;0).
43      * @since 1.2
44      */

45     public Float() {
46     }
47
48     /**
49      * Constructs and initializes a <code>Point2D</code> with
50          * the specified coordinates.
51          * @param x,&nbsp;y the coordinates to which to set the newly
52          * constructed <code>Point2D</code>
53      * @since 1.2
54      */

55     public Float(float x, float y) {
56         this.x = x;
57         this.y = y;
58     }
59
60     /**
61      * Returns the X coordinate of this <code>Point2D</code> in
62          * <code>double</code> precision.
63          * @return the X coordinate of this <code>Point2D</code>.
64      * @since 1.2
65      */

66     public double getX() {
67         return (double) x;
68     }
69
70     /**
71      * Returns the Y coordinate of this <code>Point2D</code> in
72          * <code>double</code> precision.
73          * @return the Y coordinate of this <code>Point2D</code>.
74      * @since 1.2
75      */

76     public double getY() {
77         return (double) y;
78     }
79
80     /**
81      * Sets the location of this <code>Point2D</code> to the
82          * specified <code>double</code> coordinates.
83          * @param x,&nbsp;y the coordinates to which to set this
84          * <code>Point2D</code>
85      * @since 1.2
86      */

87     public void setLocation(double x, double y) {
88         this.x = (float) x;
89         this.y = (float) y;
90     }
91
92     /**
93      * Sets the location of this <code>Point2D</code> to the
94          * specified <code>float</code> coordinates.
95          * @param x,&nbsp;y the coordinates to which to set this
96          * <code>Point2D</code>
97      * @since 1.2
98      */

99     public void setLocation(float x, float y) {
100         this.x = x;
101         this.y = y;
102     }
103
104     /**
105      * Returns a <code>String</code> that represents the value
106          * of this <code>Point2D</code>.
107          * @return a string representation of this <code>Point2D</code>.
108      * @since 1.2
109      */

110     public String JavaDoc toString() {
111         return "Point2D.Float["+x+", "+y+"]";
112     }
113     }
114
115     /**
116      * The <code>Double</code> class defines a point specified in
117      * <code>double</code> precision.
118      */

119     public static class Double extends Point2D JavaDoc {
120     /**
121      * The X coordinate of this <code>Point2D</code>.
122      * @since 1.2
123      */

124     public double x;
125
126     /**
127      * The Y coordinate of this <code>Point2D</code>.
128      * @since 1.2
129      */

130     public double y;
131
132     /**
133      * Constructs and initializes a <code>Point2D</code> with
134          * coordinates (0,&nbsp;0).
135      * @since 1.2
136      */

137     public Double() {
138     }
139
140     /**
141      * Constructs and initializes a <code>Point2D</code> with the
142          * specified coordinates.
143          * @param x,&nbsp;y the coordinates to which to set the newly
144          * constructed <code>Point2D</code>
145      * @since 1.2
146      */

147     public Double(double x, double y) {
148         this.x = x;
149         this.y = y;
150     }
151
152     /**
153      * Returns the X coordinate of this <code>Point2D</code>
154          * in <code>double</code> precision.
155          * @return the X coordinate of this <code>Point2D</code>.
156      * @since 1.2
157      */

158     public double getX() {
159         return x;
160     }
161
162     /**
163      * Returns the Y coordinate of this <code>Point2D</code> in
164          * <code>double</code> precision.
165          * @return the Y coordinate of this <code>Point2D</code>.
166      * @since 1.2
167      */

168     public double getY() {
169         return y;
170     }
171
172     /**
173      * Sets the location of this <code>Point2D</code> to the
174          * specified <code>double</code> coordinates.
175          * @param x,&nbsp;y the coordinates to which to set this
176          * <code>Point2D</code>
177      * @since 1.2
178      */

179     public void setLocation(double x, double y) {
180         this.x = x;
181         this.y = y;
182     }
183
184     /**
185      * Returns a <code>String</code> that represents the value
186          * of this <code>Point2D</code>.
187          * @return a string representation of this <code>Point2D</code>.
188      * @since 1.2
189      */

190     public String JavaDoc toString() {
191         return "Point2D.Double["+x+", "+y+"]";
192     }
193     }
194
195     /**
196      * This is an abstract class that cannot be instantiated directly.
197      * Type-specific implementation subclasses are available for
198      * instantiation and provide a number of formats for storing
199      * the information necessary to satisfy the various accessor
200      * methods below.
201      *
202      * @see java.awt.geom.Point2D.Float
203      * @see java.awt.geom.Point2D.Double
204      * @see java.awt.Point
205      */

206     protected Point2D() {
207     }
208
209     /**
210      * Returns the X coordinate of this <code>Point2D</code> in
211      * <code>double</code> precision.
212      * @return the X coordinate of this <code>Point2D</code>.
213      * @since 1.2
214      */

215     public abstract double getX();
216
217     /**
218      * Returns the Y coordinate of this <code>Point2D</code> in
219      * <code>double</code> precision.
220      * @return the Y coordinate of this <code>Point2D</code>.
221      * @since 1.2
222      */

223     public abstract double getY();
224
225     /**
226      * Sets the location of this <code>Point2D</code> to the
227      * specified <code>double</code> coordinates.
228      * @param x,&nbsp;y the coordinates of this <code>Point2D</code>
229      * @since 1.2
230      */

231     public abstract void setLocation(double x, double y);
232
233     /**
234      * Sets the location of this <code>Point2D</code> to the same
235      * coordinates as the specified <code>Point2D</code> object.
236      * @param p the specified <code>Point2D</code> the which to set
237      * this <code>Point2D</code>
238      * @since 1.2
239      */

240     public void setLocation(Point2D JavaDoc p) {
241     setLocation(p.getX(), p.getY());
242     }
243
244     /**
245      * Returns the square of the distance between two points.
246      * @param X1,&nbsp;Y1 the coordinates of the first point
247      * @param X2,&nbsp;Y2 the coordinates of the second point
248      * @return the square of the distance between the two
249      * sets of specified coordinates.
250      */

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     /**
259      * Returns the distance between two points.
260      * @param X1,&nbsp;Y1 the coordinates of the first point
261      * @param X2,&nbsp;Y2 the coordinates of the second point
262      * @return the distance between the two sets of specified
263      * coordinates.
264      */

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     /**
273      * Returns the square of the distance from this
274      * <code>Point2D</code> to a specified point.
275      * @param PX,&nbsp;PY the coordinates of the other point
276      * @return the square of the distance between this
277      * <code>Point2D</code> and the specified point.
278      */

279     public double distanceSq(double PX, double PY) {
280     PX -= getX();
281     PY -= getY();
282     return (PX * PX + PY * PY);
283     }
284
285     /**
286      * Returns the square of the distance from this
287      * <code>Point2D</code> to a specified <code>Point2D</code>.
288      * @param pt the specified <code>Point2D</code>
289      * @return the square of the distance between this
290      * <code>Point2D</code> to a specified <code>Point2D</code>.
291      */

292     public double distanceSq(Point2D JavaDoc pt) {
293     double PX = pt.getX() - this.getX();
294     double PY = pt.getY() - this.getY();
295     return (PX * PX + PY * PY);
296     }
297
298     /**
299      * Returns the distance from this <code>Point2D</code> to
300      * a specified point.
301      * @param PX,&nbsp;PY the coordinates of the specified
302      * <code>Point2D</code>
303      * @return the distance between this <code>Point2D</code>
304      * and a specified point.
305      */

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     /**
313      * Returns the distance from this <code>Point2D</code> to a
314      * specified <code>Point2D</code>.
315      * @param pt the specified <code>Point2D</code>
316      * @return the distance between this <code>Point2D</code> and
317      * the specified <code>Point2D</code>.
318      */

319     public double distance(Point2D JavaDoc 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     /**
326      * Creates a new object of the same class and with the
327      * same contents as this object.
328      * @return a clone of this instance.
329      * @exception OutOfMemoryError if there is not enough memory.
330      * @see java.lang.Cloneable
331      * @since 1.2
332      */

333     public Object JavaDoc clone() {
334     try {
335         return super.clone();
336     } catch (CloneNotSupportedException JavaDoc e) {
337         // this shouldn't happen, since we are Cloneable
338
throw new InternalError JavaDoc();
339     }
340     }
341
342     /**
343      * Returns the hashcode for this <code>Point2D</code>.
344      * @return a hash code for this <code>Point2D</code>.
345      */

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     /**
353      * Determines whether or not two points are equal. Two instances of
354      * <code>Point2D</code> are equal if the values of their
355      * <code>x</code> and <code>y</code> member fields, representing
356      * their position in the coordinate space, are the same.
357      * @param obj an object to be compared with this <code>Point2D</code>
358      * @return <code>true</code> if the object to be compared is
359      * an instance of <code>Point2D</code> and has
360      * the same values; <code>false</code> otherwise.
361      * @since 1.2
362      */

363     public boolean equals(Object JavaDoc obj) {
364     if (obj instanceof Point2D JavaDoc) {
365         Point2D JavaDoc p2d = (Point2D JavaDoc) obj;
366         return (getX() == p2d.getX()) && (getY() == p2d.getY());
367     }
368     return super.equals(obj);
369     }
370 }
371
Popular Tags