1 | /* Point2D.java -- generic point in 2-D space
|
---|
2 | Copyright (C) 1999, 2000, 2002 Free Software Foundation
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.awt.geom;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * This class implements a generic point in 2D Cartesian space. The storage
|
---|
42 | * representation is left up to the subclass. Point includes two useful
|
---|
43 | * nested classes, for float and double storage respectively.
|
---|
44 | *
|
---|
45 | * @author Per Bothner <bothner@cygnus.com>
|
---|
46 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
47 | * @since 1.2
|
---|
48 | * @status updated to 1.4
|
---|
49 | */
|
---|
50 | public abstract class Point2D implements Cloneable
|
---|
51 | {
|
---|
52 | /**
|
---|
53 | * The default constructor.
|
---|
54 | *
|
---|
55 | * @see Point
|
---|
56 | * @see Point2D.Float
|
---|
57 | * @see Point2D.Double
|
---|
58 | */
|
---|
59 | protected Point2D()
|
---|
60 | {
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Get the X coordinate, in double precision.
|
---|
65 | *
|
---|
66 | * @return the x coordinate
|
---|
67 | */
|
---|
68 | public abstract double getX();
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Get the Y coordinate, in double precision.
|
---|
72 | *
|
---|
73 | * @return the y coordinate
|
---|
74 | */
|
---|
75 | public abstract double getY();
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Set the location of this point to the new coordinates. There may be a
|
---|
79 | * loss of precision.
|
---|
80 | *
|
---|
81 | * @param x the new x coordinate
|
---|
82 | * @param y the new y coordinate
|
---|
83 | */
|
---|
84 | public abstract void setLocation(double x, double y);
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Set the location of this point to the new coordinates. There may be a
|
---|
88 | * loss of precision.
|
---|
89 | *
|
---|
90 | * @param p the point to copy
|
---|
91 | * @throws NullPointerException if p is null
|
---|
92 | */
|
---|
93 | public void setLocation(Point2D p)
|
---|
94 | {
|
---|
95 | setLocation(p.getX(), p.getY());
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Return the square of the distance between two points.
|
---|
100 | *
|
---|
101 | * @param x1 the x coordinate of point 1
|
---|
102 | * @param y1 the y coordinate of point 1
|
---|
103 | * @param x2 the x coordinate of point 2
|
---|
104 | * @param y2 the y coordinate of point 2
|
---|
105 | * @return (x2 - x1)^2 + (y2 - y1)^2
|
---|
106 | */
|
---|
107 | public static double distanceSq(double x1, double y1, double x2, double y2)
|
---|
108 | {
|
---|
109 | x2 -= x1;
|
---|
110 | y2 -= y1;
|
---|
111 | return x2 * x2 + y2 * y2;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Return the distance between two points.
|
---|
116 | *
|
---|
117 | * @param x1 the x coordinate of point 1
|
---|
118 | * @param y1 the y coordinate of point 1
|
---|
119 | * @param x2 the x coordinate of point 2
|
---|
120 | * @param y2 the y coordinate of point 2
|
---|
121 | * @return the distance from (x1,y1) to (x2,y2)
|
---|
122 | */
|
---|
123 | static public double distance(double x1, double y1, double x2, double y2)
|
---|
124 | {
|
---|
125 | return Math.sqrt(distanceSq(x1, y1, x2, y2));
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Return the square of the distance from this point to the given one.
|
---|
130 | *
|
---|
131 | * @param x the x coordinate of the other point
|
---|
132 | * @param y the y coordinate of the other point
|
---|
133 | * @return the square of the distance
|
---|
134 | */
|
---|
135 | public double distanceSq(double x, double y)
|
---|
136 | {
|
---|
137 | return distanceSq(getX(), x, getY(), y);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Return the square of the distance from this point to the given one.
|
---|
142 | *
|
---|
143 | * @param p the other point
|
---|
144 | * @return the square of the distance
|
---|
145 | * @throws NullPointerException if p is null
|
---|
146 | */
|
---|
147 | public double distanceSq(Point2D p)
|
---|
148 | {
|
---|
149 | return distanceSq(getX(), p.getX(), getY(), p.getY());
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Return the distance from this point to the given one.
|
---|
154 | *
|
---|
155 | * @param x the x coordinate of the other point
|
---|
156 | * @param y the y coordinate of the other point
|
---|
157 | * @return the distance
|
---|
158 | */
|
---|
159 | public double distance(double x, double y)
|
---|
160 | {
|
---|
161 | return distance(getX(), x, getY(), y);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Return the distance from this point to the given one.
|
---|
166 | *
|
---|
167 | * @param p the other point
|
---|
168 | * @return the distance
|
---|
169 | * @throws NullPointerException if p is null
|
---|
170 | */
|
---|
171 | public double distance(Point2D p)
|
---|
172 | {
|
---|
173 | return distance(getX(), p.getX(), getY(), p.getY());
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Create a new point of the same run-time type with the same contents as
|
---|
178 | * this one.
|
---|
179 | *
|
---|
180 | * @return the clone
|
---|
181 | */
|
---|
182 | public Object clone()
|
---|
183 | {
|
---|
184 | try
|
---|
185 | {
|
---|
186 | return super.clone();
|
---|
187 | }
|
---|
188 | catch (CloneNotSupportedException e)
|
---|
189 | {
|
---|
190 | throw (Error) new InternalError().initCause(e); // Impossible
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Return the hashcode for this point. The formula is not documented, but
|
---|
196 | * appears to be the same as:
|
---|
197 | * <pre>
|
---|
198 | * long l = Double.doubleToLongBits(getY());
|
---|
199 | * l = l * 31 ^ Double.doubleToLongBits(getX());
|
---|
200 | * return (int) ((l >> 32) ^ l);
|
---|
201 | * </pre>
|
---|
202 | *
|
---|
203 | * @return the hashcode
|
---|
204 | */
|
---|
205 | public int hashCode()
|
---|
206 | {
|
---|
207 | // Talk about a fun time reverse engineering this one!
|
---|
208 | long l = java.lang.Double.doubleToLongBits(getY());
|
---|
209 | l = l * 31 ^ java.lang.Double.doubleToLongBits(getX());
|
---|
210 | return (int) ((l >> 32) ^ l);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Compares two points for equality. This returns true if they have the
|
---|
215 | * same coordinates.
|
---|
216 | *
|
---|
217 | * @param o the point to compare
|
---|
218 | * @return true if it is equal
|
---|
219 | */
|
---|
220 | public boolean equals(Object o)
|
---|
221 | {
|
---|
222 | if (! (o instanceof Point2D))
|
---|
223 | return false;
|
---|
224 | Point2D p = (Point2D) o;
|
---|
225 | return getX() == p.getX() && getY() == p.getY();
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * This class defines a point in <code>double</code> precision.
|
---|
230 | *
|
---|
231 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
232 | * @since 1.2
|
---|
233 | * @status updated to 1.4
|
---|
234 | */
|
---|
235 | public static class Double extends Point2D
|
---|
236 | {
|
---|
237 | /** The X coordinate. */
|
---|
238 | public double x;
|
---|
239 |
|
---|
240 | /** The Y coordinate. */
|
---|
241 | public double y;
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Create a new point at (0,0).
|
---|
245 | */
|
---|
246 | public Double()
|
---|
247 | {
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Create a new point at (x,y).
|
---|
252 | *
|
---|
253 | * @param x the x coordinate
|
---|
254 | * @param y the y coordinate
|
---|
255 | */
|
---|
256 | public Double(double x, double y)
|
---|
257 | {
|
---|
258 | this.x = x;
|
---|
259 | this.y = y;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Return the x coordinate.
|
---|
264 | *
|
---|
265 | * @return the x coordinate
|
---|
266 | */
|
---|
267 | public double getX()
|
---|
268 | {
|
---|
269 | return x;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Return the y coordinate.
|
---|
274 | *
|
---|
275 | * @return the y coordinate
|
---|
276 | */
|
---|
277 | public double getY()
|
---|
278 | {
|
---|
279 | return y;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Sets the location of this point.
|
---|
284 | *
|
---|
285 | * @param x the new x coordinate
|
---|
286 | * @param y the new y coordinate
|
---|
287 | */
|
---|
288 | public void setLocation(double x, double y)
|
---|
289 | {
|
---|
290 | this.x = x;
|
---|
291 | this.y = y;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Returns a string representation of this object. The format is:
|
---|
296 | * <code>"Point2D.Double[" + x + ", " + y + ']'</code>.
|
---|
297 | *
|
---|
298 | * @return a string representation of this object
|
---|
299 | */
|
---|
300 | public String toString()
|
---|
301 | {
|
---|
302 | return "Point2D.Double[" + x + ", " + y + ']';
|
---|
303 | }
|
---|
304 | } // class Double
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * This class defines a point in <code>float</code> precision.
|
---|
308 | *
|
---|
309 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
310 | * @since 1.2
|
---|
311 | * @status updated to 1.4
|
---|
312 | */
|
---|
313 | public static class Float extends Point2D
|
---|
314 | {
|
---|
315 | /** The X coordinate. */
|
---|
316 | public float x;
|
---|
317 |
|
---|
318 | /** The Y coordinate. */
|
---|
319 | public float y;
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Create a new point at (0,0).
|
---|
323 | */
|
---|
324 | public Float()
|
---|
325 | {
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Create a new point at (x,y).
|
---|
330 | *
|
---|
331 | * @param x the x coordinate
|
---|
332 | * @param y the y coordinate
|
---|
333 | */
|
---|
334 | public Float(float x, float y)
|
---|
335 | {
|
---|
336 | this.x = x;
|
---|
337 | this.y = y;
|
---|
338 | }
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Return the x coordinate.
|
---|
342 | *
|
---|
343 | * @return the x coordinate
|
---|
344 | */
|
---|
345 | public double getX()
|
---|
346 | {
|
---|
347 | return x;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Return the y coordinate.
|
---|
352 | *
|
---|
353 | * @return the y coordinate
|
---|
354 | */
|
---|
355 | public double getY()
|
---|
356 | {
|
---|
357 | return y;
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Sets the location of this point.
|
---|
362 | *
|
---|
363 | * @param x the new x coordinate
|
---|
364 | * @param y the new y coordinate
|
---|
365 | */
|
---|
366 | public void setLocation(double x, double y)
|
---|
367 | {
|
---|
368 | this.x = (float) x;
|
---|
369 | this.y = (float) y;
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Sets the location of this point.
|
---|
374 | *
|
---|
375 | * @param x the new x coordinate
|
---|
376 | * @param y the new y coordinate
|
---|
377 | */
|
---|
378 | public void setLocation(float x, float y)
|
---|
379 | {
|
---|
380 | this.x = x;
|
---|
381 | this.y = y;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Returns a string representation of this object. The format is:
|
---|
386 | * <code>"Point2D.Float[" + x + ", " + y + ']'</code>.
|
---|
387 | *
|
---|
388 | * @return a string representation of this object
|
---|
389 | */
|
---|
390 | public String toString()
|
---|
391 | {
|
---|
392 | return "Point2D.Float[" + x + ", " + y + ']';
|
---|
393 | }
|
---|
394 | } // class Float
|
---|
395 | } // class Point2D
|
---|