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