001: /*
002: * Copyright (C) 2005, 2006 data2c GmbH (www.data2c.com)
003: *
004: * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com
005: *
006: * J2ME version of java.awt.geom.Point2D.
007: */
008:
009: //#ifndef j2se
010: package org.awt.geom;
011:
012: /**
013: * The <code>Point2D</code> class defines a point representing a location
014: * in (x, y) coordinate space.
015: * <p>
016: * This class is quite similar to java.awt.Point2D.
017: * Only the abstract superclass for all objects that
018: * store a 2D coordinate.
019: * The actual storage representation of the coordinates is left to
020: * the subclass.
021: */
022:
023: public abstract class Point2D //implements Cloneable
024: {
025: /**
026: * The <code>Float</code> class defines a point specified in float
027: * precision.
028: */
029: public static class Float extends Point2D {
030: /**
031: * The X coordinate of this <code>Point2D</code>.
032: */
033: public float x;
034:
035: /**
036: * The Y coordinate of this <code>Point2D</code>.
037: */
038: public float y;
039:
040: /**
041: * Constructs and initializes a <code>Point2D</code> with
042: * coordinates (0, 0).
043: */
044: public Float() {
045: }
046:
047: /**
048: * Constructs and initializes a <code>Point2D</code> with
049: * the specified coordinates.
050: * @param x, y the coordinates to which to set the newly
051: * constructed <code>Point2D</code>
052: */
053: public Float(float x, float y) {
054: this .x = x;
055: this .y = y;
056: }
057:
058: //#ifdef notdef
059: public Object clone() /*throws CloneNotSupportedException*/{
060: return new Float(x, y);
061: }
062:
063: //#endif
064: /**
065: * Returns the X coordinate of this <code>Point2D</code> in
066: * <code>double</code> precision.
067: * @return the X coordinate of this <code>Point2D</code>.
068: */
069: public double getX() {
070: return (double) x;
071: }
072:
073: /**
074: * Returns the Y coordinate of this <code>Point2D</code> in
075: * <code>double</code> precision.
076: * @return the Y coordinate of this <code>Point2D</code>.
077: */
078: public double getY() {
079: return (double) y;
080: }
081:
082: /**
083: * Sets the location of this <code>Point2D</code> to the
084: * specified <code>double</code> coordinates.
085: * @param x, y the coordinates to which to set this
086: * <code>Point2D</code>
087: */
088: public void setLocation(double x, double y) {
089: this .x = (float) x;
090: this .y = (float) y;
091: }
092:
093: /**
094: * Sets the location of this <code>Point2D</code> to the
095: * specified <code>float</code> coordinates.
096: * @param x, y the coordinates to which to set this
097: * <code>Point2D</code>
098: */
099: public void setLocation(float x, float y) {
100: this .x = x;
101: this .y = y;
102: }
103:
104: /**
105: * Returns a <code>String</code> that represents the value
106: * of this <code>Point2D</code>.
107: * @return a string representation of this <code>Point2D</code>.
108: */
109: public String toString() {
110: return "Point2D.Float[" + x + ", " + y + "]";
111: }
112: }
113:
114: /**
115: * The <code>Double</code> class defines a point specified in
116: * <code>double</code> precision.
117: */
118: public static class Double extends Point2D {
119: /**
120: * The X coordinate of this <code>Point2D</code>.
121: */
122: public double x;
123:
124: /**
125: * The Y coordinate of this <code>Point2D</code>.
126: */
127: public double y;
128:
129: /**
130: * Constructs and initializes a <code>Point2D</code> with
131: * coordinates (0, 0).
132: */
133: public Double() {
134: }
135:
136: /**
137: * Constructs and initializes a <code>Point2D</code> with the
138: * specified coordinates.
139: * @param x, y the coordinates to which to set the newly
140: * constructed <code>Point2D</code>
141: */
142: public Double(double x, double y) {
143: this .x = x;
144: this .y = y;
145: }
146:
147: //#ifdef notdef
148: public Object clone() // throws CloneNotSupportedException
149: {
150: return new Double(x, y);
151: }
152:
153: //#endif
154: /**
155: * Returns the X coordinate of this <code>Point2D</code>
156: * in <code>double</code> precision.
157: * @return the X coordinate of this <code>Point2D</code>.
158: */
159: public double getX() {
160: return x;
161: }
162:
163: /**
164: * Returns the Y coordinate of this <code>Point2D</code> in
165: * <code>double</code> precision.
166: * @return the Y coordinate of this <code>Point2D</code>.
167: */
168: public double getY() {
169: return y;
170: }
171:
172: /**
173: * Sets the location of this <code>Point2D</code> to the
174: * specified <code>double</code> coordinates.
175: * @param x, y the coordinates to which to set this
176: * <code>Point2D</code>
177: */
178: public void setLocation(double x, double y) {
179: this .x = x;
180: this .y = y;
181: }
182:
183: /**
184: * Returns a <code>String</code> that represents the value
185: * of this <code>Point2D</code>.
186: * @return a string representation of this <code>Point2D</code>.
187: */
188: public String toString() {
189: return "Point2D.Double[" + x + ", " + y + "]";
190: }
191: }
192:
193: /**
194: * This is an abstract class that cannot be instantiated directly.
195: * Type-specific implementation subclasses are available for
196: * instantiation and provide a number of formats for storing
197: * the information necessary to satisfy the various accessor
198: * methods below.
199: *
200: * @see java.awt.geom.Point2D.Float
201: * @see java.awt.geom.Point2D.Double
202: * @see java.awt.Point
203: */
204: protected Point2D() {
205: }
206:
207: /**
208: * Returns the X coordinate of this <code>Point2D</code> in
209: * <code>double</code> precision.
210: * @return the X coordinate of this <code>Point2D</code>.
211: */
212: public abstract double getX();
213:
214: /**
215: * Returns the Y coordinate of this <code>Point2D</code> in
216: * <code>double</code> precision.
217: * @return the Y coordinate of this <code>Point2D</code>.
218: */
219: public abstract double getY();
220:
221: /**
222: * Sets the location of this <code>Point2D</code> to the
223: * specified <code>double</code> coordinates.
224: * @param x, y the coordinates of this <code>Point2D</code>
225: */
226: public abstract void setLocation(double x, double y);
227:
228: /**
229: * Sets the location of this <code>Point2D</code> to the same
230: * coordinates as the specified <code>Point2D</code> object.
231: * @param p the specified <code>Point2D</code> the which to set
232: * this <code>Point2D</code>
233: */
234: public void setLocation(Point2D p) {
235: setLocation(p.getX(), p.getY());
236: }
237:
238: /**
239: * Returns the square of the distance between two points.
240: * @param X1, Y1 the coordinates of the first point
241: * @param X2, Y2 the coordinates of the second point
242: * @return the square of the distance between the two
243: * sets of specified coordinates.
244: */
245: public static double distanceSq(double X1, double Y1, double X2,
246: double Y2) {
247: X1 -= X2;
248: Y1 -= Y2;
249: return (X1 * X1 + Y1 * Y1);
250: }
251:
252: /**
253: * Returns the distance between two points.
254: * @param X1, Y1 the coordinates of the first point
255: * @param X2, Y2 the coordinates of the second point
256: * @return the distance between the two sets of specified
257: * coordinates.
258: */
259: public static double distance(double X1, double Y1, double X2,
260: double Y2) {
261: X1 -= X2;
262: Y1 -= Y2;
263: return Math.sqrt(X1 * X1 + Y1 * Y1);
264: }
265:
266: /**
267: * Returns the square of the distance from this
268: * <code>Point2D</code> to a specified point.
269: * @param PX, PY the coordinates of the other point
270: * @return the square of the distance between this
271: * <code>Point2D</code> and the specified point.
272: */
273: public double distanceSq(double PX, double PY) {
274: PX -= getX();
275: PY -= getY();
276: return (PX * PX + PY * PY);
277: }
278:
279: /**
280: * Returns the square of the distance from this
281: * <code>Point2D</code> to a specified <code>Point2D</code>.
282: * @param pt the specified <code>Point2D</code>
283: * @return the square of the distance between this
284: * <code>Point2D</code> to a specified <code>Point2D</code>.
285: */
286: public double distanceSq(Point2D pt) {
287: double PX = pt.getX() - this .getX();
288: double PY = pt.getY() - this .getY();
289: return (PX * PX + PY * PY);
290: }
291:
292: /**
293: * Returns the distance from this <code>Point2D</code> to
294: * a specified point.
295: * @param PX, PY the coordinates of the specified
296: * <code>Point2D</code>
297: * @return the distance between this <code>Point2D</code>
298: * and a specified point.
299: */
300: public double distance(double PX, double PY) {
301: PX -= getX();
302: PY -= getY();
303: return Math.sqrt(PX * PX + PY * PY);
304: }
305:
306: /**
307: * Returns the distance from this <code>Point2D</code> to a
308: * specified <code>Point2D</code>.
309: * @param pt the specified <code>Point2D</code>
310: * @return the distance between this <code>Point2D</code> and
311: * the specified <code>Point2D</code>.
312: */
313: public double distance(Point2D pt) {
314: double PX = pt.getX() - this .getX();
315: double PY = pt.getY() - this .getY();
316: return Math.sqrt(PX * PX + PY * PY);
317: }
318:
319: /* *
320: * Creates a new object of the same class and with the
321: * same contents as this object.
322: * @return a clone of this instance.
323: * @exception OutOfMemoryError if there is not enough memory.
324: * @see java.lang.Cloneable
325: */
326: //#ifdef j2se
327: public Object clone() {
328: try {
329: return super .clone();
330: } catch (CloneNotSupportedException e) {
331: // this shouldn't happen, since we are Cloneable
332: throw new InternalError();
333: }
334: }
335:
336: //#else
337: //public abstract Object clone();
338: //#endif
339:
340: /**
341: * Returns the hashcode for this <code>Point2D</code>.
342: * @return a hash code for this <code>Point2D</code>.
343: */
344: public int hashCode() {
345: long bits = java.lang.Double.doubleToLongBits(getX());
346: bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
347: return (((int) bits) ^ ((int) (bits >> 32)));
348: }
349:
350: /**
351: * Determines whether or not two points are equal. Two instances of
352: * <code>Point2D</code> are equal if the values of their
353: * <code>x</code> and <code>y</code> member fields, representing
354: * their position in the coordinate space, are the same.
355: * @param obj an object to be compared with this <code>Point2D</code>
356: * @return <code>true</code> if the object to be compared is
357: * an instance of <code>Point2D</code> and has
358: * the same values; <code>false</code> otherwise.
359: */
360: public boolean equals(Object obj) {
361: if (obj instanceof Point2D) {
362: Point2D p2d = (Point2D) obj;
363: return (getX() == p2d.getX()) && (getY() == p2d.getY());
364: }
365: return super .equals(obj);
366: }
367:
368: }
369: //#endif
|