Point2D.java 9.4 KB

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