001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package armyc2.c2sd.graphics2d;
006import armyc2.c2sd.JavaLineArray.POINT2;
007/*
008 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
009 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
010 *
011 * This code is free software; you can redistribute it and/or modify it
012 * under the terms of the GNU General Public License version 2 only, as
013 * published by the Free Software Foundation. Sun designates this
014 * particular file as subject to the "Classpath" exception as provided
015 * by Sun in the LICENSE file that accompanied this code.
016 *
017 * This code is distributed in the hope that it will be useful, but WITHOUT
018 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
019 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
020 * version 2 for more details (a copy is included in the LICENSE file that
021 * accompanied this code).
022 *
023 * You should have received a copy of the GNU General Public License version
024 * 2 along with this work; if not, write to the Free Software Foundation,
025 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
026 *
027 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
028 * CA 95054 USA or visit www.sun.com if you need additional information or
029 * have any questions.
030 */
031import java.io.Serializable;
032
033/**
034 * The <code>Point2D</code> class defines a point representing a location in
035 * {@code (x,y)} coordinate space.
036 * <p>
037 * This class is only the abstract superclass for all objects that store a 2D
038 * coordinate. The actual storage representation of the coordinates is left to
039 * the subclass.
040 *
041 * @author Jim Graham
042 * @since 1.2
043 */
044public abstract class Point2D implements Cloneable{
045
046    /**
047     * The <code>Float</code> class defines a point specified in float
048     * precision.
049     *
050     * @since 1.2
051     */
052    public static class Float extends Point2D implements Serializable {
053
054        /**
055         * The X coordinate of this <code>Point2D</code>.
056         *
057         * @since 1.2
058         * @serial
059         */
060        public float x;
061        /**
062         * The Y coordinate of this <code>Point2D</code>.
063         *
064         * @since 1.2
065         * @serial
066         */
067        public float y;
068
069        /**
070         * Constructs and initializes a <code>Point2D</code> with coordinates
071         * (0,&nbsp;0).
072         *
073         * @since 1.2
074         */
075        public Float() {
076        }
077
078        /**
079         * Constructs and initializes a <code>Point2D</code> with the specified
080         * coordinates.
081         *
082         * @param x
083         * the X coordinate of the newly constructed
084         * <code>Point2D</code>
085        Point2D.java : » App » android-app-examples » org » loon » framework » android » gam... Page 1 of 6
086        http://www.java2s.com/Open-Source/Android/App/android-app-examples/org/loon/frame... 2/25/2013
087         * @param y
088         * the Y coordinate of the newly constructed
089         * <code>Point2D</code>
090         * @since 1.2
091         */
092        public Float(float x, float y) {
093            this.x = x;
094            this.y = y;
095        }
096
097        /**
098         * {@inheritDoc}
099         *
100         * @since 1.2
101         */
102        public double getX() {
103            return (double) x;
104        }
105
106        /**
107         * {@inheritDoc}
108         *
109         * @since 1.2
110         */
111        public double getY() {
112            return (double) y;
113        }
114
115        /**
116         * {@inheritDoc}
117         *
118         * @since 1.2
119         */
120        public void setLocation(double x, double y) {
121            this.x = (float) x;
122            this.y = (float) y;
123        }
124
125        /**
126         * Sets the location of this <code>Point2D</code> to the specified
127         * <code>float</code> coordinates.
128         *
129         * @param x
130         * the new X coordinate of this {@code Point2D}
131         * @param y
132         * the new Y coordinate of this {@code Point2D}
133         * @since 1.2
134         */
135        public void setLocation(float x, float y) {
136            this.x = x;
137            this.y = y;
138        }
139
140        /**
141         * Returns a <code>String</code> that represents the value of this
142         * <code>Point2D</code>.
143         *
144         * @return a string representation of this <code>Point2D</code>.
145         * @since 1.2
146         */
147        public String toString() {
148            return "Point2D.Float[" + x + ", " + y + "]";
149        }
150        /*
151         * JDK 1.6 serialVersionUID
152         */
153        private static final long serialVersionUID = -2870572449815403710L;
154    }
155
156    /**
157     * The <code>Double</code> class defines a point specified in
158     * <code>double</code> precision.
159     *
160     * @since 1.2
161     */
162    public static class Double extends Point2D implements Serializable {
163
164        /**
165         * The X coordinate of this <code>Point2D</code>.
166         *
167         * @since 1.2
168         * @serial
169         */
170        public double x;
171        /**
172         * The Y coordinate of this <code>Point2D</code>.
173         *
174         * @since 1.2
175         * @serial
176         */
177        public double y;
178
179        /**
180         * Constructs and initializes a <code>Point2D</code> with coordinates
181         * (0,&nbsp;0).
182         *
183         * @since 1.2
184         */
185        public Double() {
186        }
187
188        /**
189         * Constructs and initializes a <code>Point2D</code> with the specified
190         * coordinates.
191         *
192         * @param x
193         * the X coordinate of the newly constructed
194         * <code>Point2D</code>
195         * @param y
196         * the Y coordinate of the newly constructed
197         * <code>Point2D</code>
198         * @since 1.2
199         */
200        public Double(double x, double y) {
201            this.x = x;
202            this.y = y;
203        }
204        /**
205         * add the constructor
206         * @param pt 
207         */
208        public Double(POINT2 pt)
209        {
210            this.x=pt.x;
211            this.y=pt.y;
212        }
213        /**
214         * {@inheritDoc}
215         *
216         * @since 1.2
217         */
218        public double getX() {
219            return x;
220        }
221
222        /**
223         * {@inheritDoc}
224         *
225         * @since 1.2
226         */
227        public double getY() {
228            return y;
229        }
230
231        /**
232         * {@inheritDoc}
233         *
234         * @since 1.2
235         */
236        public void setLocation(double x, double y) {
237            this.x = x;
238            this.y = y;
239        }
240
241        /**
242         * Returns a <code>String</code> that represents the value of this
243         * <code>Point2D</code>.
244         *
245         * @return a string representation of this <code>Point2D</code>.
246         * @since 1.2
247         */
248        public String toString() {
249            return "Point2D.Double[" + x + ", " + y + "]";
250        }
251        /*
252         * JDK 1.6 serialVersionUID
253         */
254        private static final long serialVersionUID = 6150783262733311327L;
255    }
256
257    /**
258     * This is an abstract class that cannot be instantiated directly.
259     * Type-specific implementation subclasses are available for instantiation
260     * and provide a number of formats for storing the information necessary to
261     * satisfy the various accessor methods below.
262     *
263     * @see and.awt.geom.Point2D.Float
264     * @see and.awt.geom.Point2D.Double
265     * @see and.awt.Point
266     * @since 1.2
267     */
268    protected Point2D() {
269    }
270
271    /**
272     * Returns the X coordinate of this <code>Point2D</code> in
273     * <code>double</code> precision.
274     *
275    Point2D.java : » App » android-app-examples » org » loon » framework » android » gam... Page 3 of 6
276    http://www.java2s.com/Open-Source/Android/App/android-app-examples/org/loon/frame... 2/25/2013
277     * @return the X coordinate of this <code>Point2D</code>.
278     * @since 1.2
279     */
280    public abstract double getX();
281
282    /**
283     * Returns the Y coordinate of this <code>Point2D</code> in
284     * <code>double</code> precision.
285     *
286     * @return the Y coordinate of this <code>Point2D</code>.
287     * @since 1.2
288     */
289    public abstract double getY();
290
291    /**
292     * Sets the location of this <code>Point2D</code> to the specified
293     * <code>double</code> coordinates.
294     *
295     * @param x
296     * the new X coordinate of this {@code Point2D}
297     * @param y
298     * the new Y coordinate of this {@code Point2D}
299     * @since 1.2
300     */
301    public abstract void setLocation(double x, double y);
302
303    /**
304     * Sets the location of this <code>Point2D</code> to the same coordinates as
305     * the specified <code>Point2D</code> object.
306     *
307     * @param p
308     * the specified <code>Point2D</code> to which to set this
309     * <code>Point2D</code>
310     * @since 1.2
311     */
312    public void setLocation(Point2D p) {
313        setLocation(p.getX(), p.getY());
314    }
315
316    /**
317     * Returns the square of the distance between two points.
318     *
319     * @param x1
320     * the X coordinate of the first specified point
321     * @param y1
322     * the Y coordinate of the first specified point
323     * @param x2
324     * the X coordinate of the second specified point
325     * @param y2
326     * the Y coordinate of the second specified point
327     * @return the square of the distance between the two sets of specified
328     * coordinates.
329     * @since 1.2
330     */
331    public static double distanceSq(double x1, double y1, double x2, double y2) {
332        x1 -= x2;
333        y1 -= y2;
334        return (x1 * x1 + y1 * y1);
335    }
336
337    /**
338     * Returns the distance between two points.
339     *
340     * @param x1
341     * the X coordinate of the first specified point
342     * @param y1
343     * the Y coordinate of the first specified point
344     * @param x2
345     * the X coordinate of the second specified point
346     * @param y2
347     * the Y coordinate of the second specified point
348     * @return the distance between the two sets of specified coordinates.
349     * @since 1.2
350     */
351    public static double distance(double x1, double y1, double x2, double y2) {
352        x1 -= x2;
353        y1 -= y2;
354        return Math.sqrt(x1 * x1 + y1 * y1);
355    }
356    /**
357     * Determines whether or not two points are equal. Two instances of
358     * <code>Point2D</code> are equal if the values of their <code>x</code> and
359     * <code>y</code> member fields, representing their position in the
360     * coordinate space, are the same.
361     *
362     * @param obj
363     * an object to be compared with this <code>Point2D</code>
364     * @return <code>true</code> if the object to be compared is an instance of
365     * <code>Point2D</code> and has the same values; <code>false</code>
366     * otherwise.
367     * @since 1.2
368     */
369    public boolean equals(Object obj) {
370        if (obj instanceof Point2D) {
371            Point2D p2d = (Point2D) obj;
372            return (getX() == p2d.getX()) && (getY() == p2d.getY());
373        }
374        return super.equals(obj);
375    }
376}