1 /* Point2D.java -- generic point in 2-D space
2 Copyright (C) 1999, 2000, 2002, 2004 Free Software Foundation
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package java
.awt
.geom
;
42 * This class implements a generic point in 2D Cartesian space. The storage
43 * representation is left up to the subclass. Point includes two useful
44 * nested classes, for float and double storage respectively.
46 * @author Per Bothner (bothner@cygnus.com)
47 * @author Eric Blake (ebb9@email.byu.edu)
49 * @status updated to 1.4
51 public abstract class Point2D
implements Cloneable
54 * The default constructor.
65 * Get the X coordinate, in double precision.
67 * @return the x coordinate
69 public abstract double getX();
72 * Get the Y coordinate, in double precision.
74 * @return the y coordinate
76 public abstract double getY();
79 * Set the location of this point to the new coordinates. There may be a
82 * @param x the new x coordinate
83 * @param y the new y coordinate
85 public abstract void setLocation(double x
, double y
);
88 * Set the location of this point to the new coordinates. There may be a
91 * @param p the point to copy
92 * @throws NullPointerException if p is null
94 public void setLocation(Point2D p
)
96 setLocation(p
.getX(), p
.getY());
100 * Return the square of the distance between two points.
102 * @param x1 the x coordinate of point 1
103 * @param y1 the y coordinate of point 1
104 * @param x2 the x coordinate of point 2
105 * @param y2 the y coordinate of point 2
106 * @return (x2 - x1)^2 + (y2 - y1)^2
108 public static double distanceSq(double x1
, double y1
, double x2
, double y2
)
112 return x2
* x2
+ y2
* y2
;
116 * Return the distance between two points.
118 * @param x1 the x coordinate of point 1
119 * @param y1 the y coordinate of point 1
120 * @param x2 the x coordinate of point 2
121 * @param y2 the y coordinate of point 2
122 * @return the distance from (x1,y1) to (x2,y2)
124 public static double distance(double x1
, double y1
, double x2
, double y2
)
126 return Math
.sqrt(distanceSq(x1
, y1
, x2
, y2
));
130 * Return the square of the distance from this point to the given one.
132 * @param x the x coordinate of the other point
133 * @param y the y coordinate of the other point
134 * @return the square of the distance
136 public double distanceSq(double x
, double y
)
138 return distanceSq(getX(), x
, getY(), y
);
142 * Return the square of the distance from this point to the given one.
144 * @param p the other point
145 * @return the square of the distance
146 * @throws NullPointerException if p is null
148 public double distanceSq(Point2D p
)
150 return distanceSq(getX(), p
.getX(), getY(), p
.getY());
154 * Return the distance from this point to the given one.
156 * @param x the x coordinate of the other point
157 * @param y the y coordinate of the other point
158 * @return the distance
160 public double distance(double x
, double y
)
162 return distance(getX(), x
, getY(), y
);
166 * Return the distance from this point to the given one.
168 * @param p the other point
169 * @return the distance
170 * @throws NullPointerException if p is null
172 public double distance(Point2D p
)
174 return distance(getX(), p
.getX(), getY(), p
.getY());
178 * Create a new point of the same run-time type with the same contents as
183 public Object
clone()
187 return super.clone();
189 catch (CloneNotSupportedException e
)
191 throw (Error
) new InternalError().initCause(e
); // Impossible
196 * Return the hashcode for this point. The formula is not documented, but
197 * appears to be the same as:
199 * long l = Double.doubleToLongBits(getY());
200 * l = l * 31 ^ Double.doubleToLongBits(getX());
201 * return (int) ((l >> 32) ^ l);
204 * @return the hashcode
206 public int hashCode()
208 // Talk about a fun time reverse engineering this one!
209 long l
= java
.lang
.Double
.doubleToLongBits(getY());
210 l
= l
* 31 ^ java
.lang
.Double
.doubleToLongBits(getX());
211 return (int) ((l
>> 32) ^ l
);
215 * Compares two points for equality. This returns true if they have the
218 * @param o the point to compare
219 * @return true if it is equal
221 public boolean equals(Object o
)
223 if (! (o
instanceof Point2D
))
225 Point2D p
= (Point2D
) o
;
226 return getX() == p
.getX() && getY() == p
.getY();
230 * This class defines a point in <code>double</code> precision.
232 * @author Eric Blake (ebb9@email.byu.edu)
234 * @status updated to 1.4
236 public static class Double
extends Point2D
238 /** The X coordinate. */
241 /** The Y coordinate. */
245 * Create a new point at (0,0).
252 * Create a new point at (x,y).
254 * @param x the x coordinate
255 * @param y the y coordinate
257 public Double(double x
, double y
)
264 * Return the x coordinate.
266 * @return the x coordinate
274 * Return the y coordinate.
276 * @return the y coordinate
284 * Sets the location of this point.
286 * @param x the new x coordinate
287 * @param y the new y coordinate
289 public void setLocation(double x
, double y
)
296 * Returns a string representation of this object. The format is:
297 * <code>"Point2D.Double[" + x + ", " + y + ']'</code>.
299 * @return a string representation of this object
301 public String
toString()
303 return "Point2D.Double[" + x
+ ", " + y
+ ']';
308 * This class defines a point in <code>float</code> precision.
310 * @author Eric Blake (ebb9@email.byu.edu)
312 * @status updated to 1.4
314 public static class Float
extends Point2D
316 /** The X coordinate. */
319 /** The Y coordinate. */
323 * Create a new point at (0,0).
330 * Create a new point at (x,y).
332 * @param x the x coordinate
333 * @param y the y coordinate
335 public Float(float x
, float y
)
342 * Return the x coordinate.
344 * @return the x coordinate
352 * Return the y coordinate.
354 * @return the y coordinate
362 * Sets the location of this point.
364 * @param x the new x coordinate
365 * @param y the new y coordinate
367 public void setLocation(double x
, double y
)
374 * Sets the location of this point.
376 * @param x the new x coordinate
377 * @param y the new y coordinate
379 public void setLocation(float x
, float y
)
386 * Returns a string representation of this object. The format is:
387 * <code>"Point2D.Float[" + x + ", " + y + ']'</code>.
389 * @return a string representation of this object
391 public String
toString()
393 return "Point2D.Float[" + x
+ ", " + y
+ ']';