1. /*
  2. * @(#)Point2D.java 1.14 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt.geom;
  11. /**
  12. * The <code>Point2D</code> class defines a point representing a location
  13. * in (x, y) coordinate space.
  14. * <p>
  15. * This class is only the abstract superclass for all objects that
  16. * store a 2D coordinate.
  17. * The actual storage representation of the coordinates is left to
  18. * the subclass.
  19. *
  20. * @version 1.14, 02/02/00
  21. * @author Jim Graham
  22. */
  23. public abstract class Point2D implements Cloneable {
  24. /**
  25. * The <code>Float</code> class defines a point specified in float
  26. * precision.
  27. */
  28. public static class Float extends Point2D {
  29. /**
  30. * The X coordinate of this <code>Point2D</code>.
  31. * @since 1.2
  32. */
  33. public float x;
  34. /**
  35. * The Y coordinate of this <code>Point2D</code>.
  36. * @since 1.2
  37. */
  38. public float y;
  39. /**
  40. * Constructs and initializes a <code>Point2D</code> with
  41. * coordinates (0, 0).
  42. * @since 1.2
  43. */
  44. public Float() {
  45. }
  46. /**
  47. * Constructs and initializes a <code>Point2D</code> with
  48. * the specified coordinates.
  49. * @param x, y the coordinates to which to set the newly
  50. * constructed <code>Point2D</code>
  51. * @since 1.2
  52. */
  53. public Float(float x, float y) {
  54. this.x = x;
  55. this.y = y;
  56. }
  57. /**
  58. * Returns the X coordinate of this <code>Point2D</code> in
  59. * <code>double</code> precision.
  60. * @return the X coordinate of this <code>Point2D</code>.
  61. * @since 1.2
  62. */
  63. public double getX() {
  64. return (double) x;
  65. }
  66. /**
  67. * Returns the Y coordinate of this <code>Point2D</code> in
  68. * <code>double</code> precision.
  69. * @return the Y coordinate of this <code>Point2D</code>.
  70. * @since 1.2
  71. */
  72. public double getY() {
  73. return (double) y;
  74. }
  75. /**
  76. * Sets the location of this <code>Point2D</code> to the
  77. * specified <code>double</code> coordinates.
  78. * @param x, y the coordinates to which to set this
  79. * <code>Point2D</code>
  80. * @since 1.2
  81. */
  82. public void setLocation(double x, double y) {
  83. this.x = (float) x;
  84. this.y = (float) y;
  85. }
  86. /**
  87. * Sets the location of this <code>Point2D</code> to the
  88. * specified <code>float</code> coordinates.
  89. * @param x, y the coordinates to which to set this
  90. * <code>Point2D</code>
  91. * @since 1.2
  92. */
  93. public void setLocation(float x, float y) {
  94. this.x = x;
  95. this.y = y;
  96. }
  97. /**
  98. * Returns a <code>String</code> that represents the value
  99. * of this <code>Point2D</code>.
  100. * @return a string representation of this <code>Point2D</code>.
  101. * @since 1.2
  102. */
  103. public String toString() {
  104. return "Point2D.Float["+x+", "+y+"]";
  105. }
  106. }
  107. /**
  108. * The <code>Double</code> class defines a point specified in
  109. * <code>double</code> precision.
  110. */
  111. public static class Double extends Point2D {
  112. /**
  113. * The X coordinate of this <code>Point2D</code>.
  114. * @since 1.2
  115. */
  116. public double x;
  117. /**
  118. * The Y coordinate of this <code>Point2D</code>.
  119. * @since 1.2
  120. */
  121. public double y;
  122. /**
  123. * Constructs and initializes a <code>Point2D</code> with
  124. * coordinates (0, 0).
  125. * @since 1.2
  126. */
  127. public Double() {
  128. }
  129. /**
  130. * Constructs and initializes a <code>Point2D</code> with the
  131. * specified coordinates.
  132. * @param x, y the coordinates to which to set the newly
  133. * constructed <code>Point2D</code>
  134. * @since 1.2
  135. */
  136. public Double(double x, double y) {
  137. this.x = x;
  138. this.y = y;
  139. }
  140. /**
  141. * Returns the X coordinate of this <code>Point2D</code>
  142. * in <code>double</code> precision.
  143. * @return the X coordinate of this <code>Point2D</code>.
  144. * @since 1.2
  145. */
  146. public double getX() {
  147. return x;
  148. }
  149. /**
  150. * Returns the Y coordinate of this <code>Point2D</code> in
  151. * <code>double</code> precision.
  152. * @return the Y coordinate of this <code>Point2D</code>.
  153. * @since 1.2
  154. */
  155. public double getY() {
  156. return y;
  157. }
  158. /**
  159. * Sets the location of this <code>Point2D</code> to the
  160. * specified <code>double</code> coordinates.
  161. * @param x, y the coordinates to which to set this
  162. * <code>Point2D</code>
  163. * @since 1.2
  164. */
  165. public void setLocation(double x, double y) {
  166. this.x = x;
  167. this.y = y;
  168. }
  169. /**
  170. * Returns a <code>String</code> that represents the value
  171. * of this <code>Point2D</code>.
  172. * @return a string representation of this <code>Point2D</code>.
  173. * @since 1.2
  174. */
  175. public String toString() {
  176. return "Point2D.Double["+x+", "+y+"]";
  177. }
  178. }
  179. /**
  180. * This is an abstract class that cannot be instantiated directly.
  181. * Type-specific implementation subclasses are available for
  182. * instantiation and provide a number of formats for storing
  183. * the information necessary to satisfy the various accessor
  184. * methods below.
  185. *
  186. * @see java.awt.geom.Point2D.Float
  187. * @see java.awt.geom.Point2D.Double
  188. * @see java.awt.Point
  189. */
  190. protected Point2D() {
  191. }
  192. /**
  193. * Returns the X coordinate of this <code>Point2D</code> in
  194. * <code>double</code> precision.
  195. * @return the X coordinate of this <code>Point2D</code>.
  196. * @since 1.2
  197. */
  198. public abstract double getX();
  199. /**
  200. * Returns the Y coordinate of this <code>Point2D</code> in
  201. * <code>double</code> precision.
  202. * @return the Y coordinate of this <code>Point2D</code>.
  203. * @since 1.2
  204. */
  205. public abstract double getY();
  206. /**
  207. * Sets the location of this <code>Point2D</code> to the
  208. * specified <code>float</code> coordinates.
  209. * @param x, y the coordinates of this <code>Point2D</code>
  210. * @since 1.2
  211. */
  212. public abstract void setLocation(double x, double y);
  213. /**
  214. * Sets the location of this <code>Point2D</code> to the same
  215. * coordinates as the specified <code>Point2D</code> object.
  216. * @param p the specified <code>Point2D</code> the which to set
  217. * this <code>Point2D</code>
  218. * @since 1.2
  219. */
  220. public void setLocation(Point2D p) {
  221. setLocation(p.getX(), p.getY());
  222. }
  223. /**
  224. * Returns the square of the distance between two points.
  225. * @param X1, Y1 the coordinates of the first point
  226. * @param X2, Y2 the coordinates of the second point
  227. * @return the square of the distance between the two
  228. * sets of specified coordinates.
  229. */
  230. public static double distanceSq(double X1, double Y1,
  231. double X2, double Y2) {
  232. X1 -= X2;
  233. Y1 -= Y2;
  234. return (X1 * X1 + Y1 * Y1);
  235. }
  236. /**
  237. * Returns the distance between two points.
  238. * @param X1, Y1 the coordinates of the first point
  239. * @param X2, Y2 the coordinates of the second point
  240. * @return the distance between the two sets of specified
  241. * coordinates.
  242. */
  243. public static double distance(double X1, double Y1,
  244. double X2, double Y2) {
  245. X1 -= X2;
  246. Y1 -= Y2;
  247. return Math.sqrt(X1 * X1 + Y1 * Y1);
  248. }
  249. /**
  250. * Returns the square of the distance from this
  251. * <code>Point2D</code> to a specified point.
  252. * @param PX, PY the coordinates of the other point
  253. * @return the square of the distance between this
  254. * <code>Point2D</code> and the specified point.
  255. */
  256. public double distanceSq(double PX, double PY) {
  257. PX -= getX();
  258. PY -= getY();
  259. return (PX * PX + PY * PY);
  260. }
  261. /**
  262. * Returns the square of the distance from this
  263. * <code>Point2D</code> to a specified <code>Point2D</code>.
  264. * @param pt the specified <code>Point2D</code>
  265. * @return the square of the distance between this
  266. * <code>Point2D</code> to a specified <code>Point2D</code>.
  267. */
  268. public double distanceSq(Point2D pt) {
  269. double PX = pt.getX() - this.getX();
  270. double PY = pt.getY() - this.getY();
  271. return (PX * PX + PY * PY);
  272. }
  273. /**
  274. * Returns the distance from this <code>Point2D</code> to
  275. * a specified point.
  276. * @param PX, PY the coordinates of the specified
  277. * <code>Point2D</code>
  278. * @return the distance between this <code>Point2D</code>
  279. * and a specified point.
  280. */
  281. public double distance(double PX, double PY) {
  282. PX -= getX();
  283. PY -= getY();
  284. return Math.sqrt(PX * PX + PY * PY);
  285. }
  286. /**
  287. * Returns the distance from this <code>Point2D</code> to a
  288. * specified <code>Point2D</code>.
  289. * @param pt the specified <code>Point2D</code>
  290. * @return the distance between this <code>Point2D</code> and
  291. * the specified <code>Point2D</code>.
  292. */
  293. public double distance(Point2D pt) {
  294. double PX = pt.getX() - this.getX();
  295. double PY = pt.getY() - this.getY();
  296. return Math.sqrt(PX * PX + PY * PY);
  297. }
  298. /**
  299. * Creates a new object of the same class and with the
  300. * same contents as this object.
  301. * @return a clone of this instance.
  302. * @exception OutOfMemoryError if there is not enough memory.
  303. * @see java.lang.Cloneable
  304. * @since 1.2
  305. */
  306. public Object clone() {
  307. try {
  308. return super.clone();
  309. } catch (CloneNotSupportedException e) {
  310. // this shouldn't happen, since we are Cloneable
  311. throw new InternalError();
  312. }
  313. }
  314. /**
  315. * Returns the hashcode for this <code>Point2D</code>.
  316. * @return a hash code for this <code>Point2D</code>.
  317. */
  318. public int hashCode() {
  319. long bits = java.lang.Double.doubleToLongBits(getX());
  320. bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
  321. return (((int) bits) ^ ((int) (bits >> 32)));
  322. }
  323. /**
  324. * Determines whether or not two points are equal. Two instances of
  325. * <code>Point2D</code> are equal if the values of their
  326. * <code>x</code> and <code>y</code> member fields, representing
  327. * their position in the coordinate space, are the same.
  328. * @param obj an object to be compared with this <code>Point2D</code>
  329. * @return <code>true</code> if the object to be compared is
  330. * an instance of <code>Point2D</code> and has
  331. * the same values; <code>false</code> otherwise.
  332. * @since 1.2
  333. */
  334. public boolean equals(Object obj) {
  335. if (obj instanceof Point2D) {
  336. Point2D p2d = (Point2D) obj;
  337. return (getX() == p2d.getX()) && (getY() == p2d.getY());
  338. }
  339. return super.equals(obj);
  340. }
  341. }