001/* 002 * To change this template, choose Tools | Templates 003 * and open the template in the editor. 004 */ 005package armyc2.c5isr.graphics2d; 006import armyc2.c5isr.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, 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 * @param y 086 * the Y coordinate of the newly constructed 087 * <code>Point2D</code> 088 * @since 1.2 089 */ 090 public Float(float x, float y) { 091 this.x = x; 092 this.y = y; 093 } 094 095 /** 096 * {@inheritDoc} 097 * 098 * @since 1.2 099 */ 100 public double getX() { 101 return (double) x; 102 } 103 104 /** 105 * {@inheritDoc} 106 * 107 * @since 1.2 108 */ 109 public double getY() { 110 return (double) y; 111 } 112 113 /** 114 * {@inheritDoc} 115 * 116 * @since 1.2 117 */ 118 public void setLocation(double x, double y) { 119 this.x = (float) x; 120 this.y = (float) y; 121 } 122 123 /** 124 * Sets the location of this <code>Point2D</code> to the specified 125 * <code>float</code> coordinates. 126 * 127 * @param x 128 * the new X coordinate of this {@code Point2D} 129 * @param y 130 * the new Y coordinate of this {@code Point2D} 131 * @since 1.2 132 */ 133 public void setLocation(float x, float y) { 134 this.x = x; 135 this.y = y; 136 } 137 138 /** 139 * Returns a <code>String</code> that represents the value of this 140 * <code>Point2D</code>. 141 * 142 * @return a string representation of this <code>Point2D</code>. 143 * @since 1.2 144 */ 145 public String toString() { 146 return "Point2D.Float[" + x + ", " + y + "]"; 147 } 148 /* 149 * JDK 1.6 serialVersionUID 150 */ 151 private static final long serialVersionUID = -2870572449815403710L; 152 } 153 154 /** 155 * The <code>Double</code> class defines a point specified in 156 * <code>double</code> precision. 157 * 158 * @since 1.2 159 */ 160 public static class Double extends Point2D implements Serializable { 161 162 /** 163 * The X coordinate of this <code>Point2D</code>. 164 * 165 * @since 1.2 166 * @serial 167 */ 168 public double x; 169 /** 170 * The Y coordinate of this <code>Point2D</code>. 171 * 172 * @since 1.2 173 * @serial 174 */ 175 public double y; 176 177 /** 178 * Constructs and initializes a <code>Point2D</code> with coordinates 179 * (0, 0). 180 * 181 * @since 1.2 182 */ 183 public Double() { 184 } 185 186 /** 187 * Constructs and initializes a <code>Point2D</code> with the specified 188 * coordinates. 189 * 190 * @param x 191 * the X coordinate of the newly constructed 192 * <code>Point2D</code> 193 * @param y 194 * the Y coordinate of the newly constructed 195 * <code>Point2D</code> 196 * @since 1.2 197 */ 198 public Double(double x, double y) { 199 this.x = x; 200 this.y = y; 201 } 202 /** 203 * add the constructor 204 * @param pt 205 */ 206 public Double(POINT2 pt) 207 { 208 this.x=pt.x; 209 this.y=pt.y; 210 } 211 /** 212 * {@inheritDoc} 213 * 214 * @since 1.2 215 */ 216 public double getX() { 217 return x; 218 } 219 220 /** 221 * {@inheritDoc} 222 * 223 * @since 1.2 224 */ 225 public double getY() { 226 return y; 227 } 228 229 /** 230 * {@inheritDoc} 231 * 232 * @since 1.2 233 */ 234 public void setLocation(double x, double y) { 235 this.x = x; 236 this.y = y; 237 } 238 239 /** 240 * Returns a <code>String</code> that represents the value of this 241 * <code>Point2D</code>. 242 * 243 * @return a string representation of this <code>Point2D</code>. 244 * @since 1.2 245 */ 246 public String toString() { 247 return "Point2D.Double[" + x + ", " + y + "]"; 248 } 249 /* 250 * JDK 1.6 serialVersionUID 251 */ 252 private static final long serialVersionUID = 6150783262733311327L; 253 } 254 255 /** 256 * This is an abstract class that cannot be instantiated directly. 257 * Type-specific implementation subclasses are available for instantiation 258 * and provide a number of formats for storing the information necessary to 259 * satisfy the various accessor methods below. 260 * 261 * @see armyc2.c5isr.graphics2d.Point2D.Float 262 * @see armyc2.c5isr.graphics2d.Point2D.Double 263 * @see armyc2.c5isr.graphics2d.Point 264 * @since 1.2 265 */ 266 protected Point2D() { 267 } 268 269 /** 270 * Returns the X coordinate of this <code>Point2D</code> in 271 * <code>double</code> precision. 272 * 273 * @return the X coordinate of this <code>Point2D</code>. 274 * @since 1.2 275 */ 276 public abstract double getX(); 277 278 /** 279 * Returns the Y coordinate of this <code>Point2D</code> in 280 * <code>double</code> precision. 281 * 282 * @return the Y coordinate of this <code>Point2D</code>. 283 * @since 1.2 284 */ 285 public abstract double getY(); 286 287 /** 288 * Sets the location of this <code>Point2D</code> to the specified 289 * <code>double</code> coordinates. 290 * 291 * @param x 292 * the new X coordinate of this {@code Point2D} 293 * @param y 294 * the new Y coordinate of this {@code Point2D} 295 * @since 1.2 296 */ 297 public abstract void setLocation(double x, double y); 298 299 /** 300 * Sets the location of this <code>Point2D</code> to the same coordinates as 301 * the specified <code>Point2D</code> object. 302 * 303 * @param p 304 * the specified <code>Point2D</code> to which to set this 305 * <code>Point2D</code> 306 * @since 1.2 307 */ 308 public void setLocation(Point2D p) { 309 setLocation(p.getX(), p.getY()); 310 } 311 312 /** 313 * Returns the square of the distance between two points. 314 * 315 * @param x1 316 * the X coordinate of the first specified point 317 * @param y1 318 * the Y coordinate of the first specified point 319 * @param x2 320 * the X coordinate of the second specified point 321 * @param y2 322 * the Y coordinate of the second specified point 323 * @return the square of the distance between the two sets of specified 324 * coordinates. 325 * @since 1.2 326 */ 327 public static double distanceSq(double x1, double y1, double x2, double y2) { 328 x1 -= x2; 329 y1 -= y2; 330 return (x1 * x1 + y1 * y1); 331 } 332 333 /** 334 * Returns the distance between two points. 335 * 336 * @param x1 337 * the X coordinate of the first specified point 338 * @param y1 339 * the Y coordinate of the first specified point 340 * @param x2 341 * the X coordinate of the second specified point 342 * @param y2 343 * the Y coordinate of the second specified point 344 * @return the distance between the two sets of specified coordinates. 345 * @since 1.2 346 */ 347 public static double distance(double x1, double y1, double x2, double y2) { 348 x1 -= x2; 349 y1 -= y2; 350 return Math.sqrt(x1 * x1 + y1 * y1); 351 } 352 /** 353 * Determines whether or not two points are equal. Two instances of 354 * <code>Point2D</code> are equal if the values of their <code>x</code> and 355 * <code>y</code> member fields, representing their position in the 356 * coordinate space, are the same. 357 * 358 * @param obj 359 * an object to be compared with this <code>Point2D</code> 360 * @return <code>true</code> if the object to be compared is an instance of 361 * <code>Point2D</code> and has the same values; <code>false</code> 362 * otherwise. 363 * @since 1.2 364 */ 365 public boolean equals(Object obj) { 366 if (obj instanceof Point2D) { 367 Point2D p2d = (Point2D) obj; 368 return (getX() == p2d.getX()) && (getY() == p2d.getY()); 369 } 370 return super.equals(obj); 371 } 372}