001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt.geom;
021:
022: import org.apache.harmony.misc.HashCode;
023:
024: public abstract class Point2D implements Cloneable {
025:
026: public static class Float extends Point2D {
027:
028: public float x;
029: public float y;
030:
031: public Float() {
032: }
033:
034: public Float(float x, float y) {
035: this .x = x;
036: this .y = y;
037: }
038:
039: @Override
040: public double getX() {
041: return x;
042: }
043:
044: @Override
045: public double getY() {
046: return y;
047: }
048:
049: public void setLocation(float x, float y) {
050: this .x = x;
051: this .y = y;
052: }
053:
054: @Override
055: public void setLocation(double x, double y) {
056: this .x = (float) x;
057: this .y = (float) y;
058: }
059:
060: @Override
061: public String toString() {
062: return getClass().getName() + "[x=" + x + ",y=" + y + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
063: }
064: }
065:
066: public static class Double extends Point2D {
067:
068: public double x;
069: public double y;
070:
071: public Double() {
072: }
073:
074: public Double(double x, double y) {
075: this .x = x;
076: this .y = y;
077: }
078:
079: @Override
080: public double getX() {
081: return x;
082: }
083:
084: @Override
085: public double getY() {
086: return y;
087: }
088:
089: @Override
090: public void setLocation(double x, double y) {
091: this .x = x;
092: this .y = y;
093: }
094:
095: @Override
096: public String toString() {
097: return getClass().getName() + "[x=" + x + ",y=" + y + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
098: }
099: }
100:
101: protected Point2D() {
102: }
103:
104: public abstract double getX();
105:
106: public abstract double getY();
107:
108: public abstract void setLocation(double x, double y);
109:
110: public void setLocation(Point2D p) {
111: setLocation(p.getX(), p.getY());
112: }
113:
114: public static double distanceSq(double x1, double y1, double x2,
115: double y2) {
116: x2 -= x1;
117: y2 -= y1;
118: return x2 * x2 + y2 * y2;
119: }
120:
121: public double distanceSq(double px, double py) {
122: return Point2D.distanceSq(getX(), getY(), px, py);
123: }
124:
125: public double distanceSq(Point2D p) {
126: return Point2D.distanceSq(getX(), getY(), p.getX(), p.getY());
127: }
128:
129: public static double distance(double x1, double y1, double x2,
130: double y2) {
131: return Math.sqrt(distanceSq(x1, y1, x2, y2));
132: }
133:
134: public double distance(double px, double py) {
135: return Math.sqrt(distanceSq(px, py));
136: }
137:
138: public double distance(Point2D p) {
139: return Math.sqrt(distanceSq(p));
140: }
141:
142: @Override
143: public Object clone() {
144: try {
145: return super .clone();
146: } catch (CloneNotSupportedException e) {
147: throw new InternalError();
148: }
149: }
150:
151: @Override
152: public int hashCode() {
153: HashCode hash = new HashCode();
154: hash.append(getX());
155: hash.append(getY());
156: return hash.hashCode();
157: }
158:
159: @Override
160: public boolean equals(Object obj) {
161: if (obj == this ) {
162: return true;
163: }
164: if (obj instanceof Point2D) {
165: Point2D p = (Point2D) obj;
166: return getX() == p.getX() && getY() == p.getY();
167: }
168: return false;
169: }
170: }
|