2 * $RCSfile: Tuple2d.java,v $
4 * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation. Sun designates this
10 * particular file as subject to the "Classpath" exception as provided
11 * by Sun in the LICENSE file that accompanied this code.
13 * This code is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * version 2 for more details (a copy is included in the LICENSE file that
17 * accompanied this code).
19 * You should have received a copy of the GNU General Public License version
20 * 2 along with this work; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24 * CA 95054 USA or visit www.sun.com if you need additional information or
28 * $Date: 2008/02/28 20:18:50 $
32 package javax.vecmath;
34 import java.lang.Math;
37 * A generic 2-element tuple that is represented by double-precision
38 * floating point x,y coordinates.
41 public abstract class Tuple2d implements java.io.Serializable, Cloneable {
43 static final long serialVersionUID = 6205762482756093838L;
57 * Constructs and initializes a Tuple2d from the specified xy coordinates.
58 * @param x the x coordinate
59 * @param y the y coordinate
61 public Tuple2d(double x, double y)
69 * Constructs and initializes a Tuple2d from the specified array.
70 * @param t the array of length 2 containing xy in order
72 public Tuple2d(double[] t)
80 * Constructs and initializes a Tuple2d from the specified Tuple2d.
81 * @param t1 the Tuple2d containing the initialization x y data
83 public Tuple2d(Tuple2d t1)
91 * Constructs and initializes a Tuple2d from the specified Tuple2f.
92 * @param t1 the Tuple2f containing the initialization x y data
94 public Tuple2d(Tuple2f t1)
96 this.x = (double) t1.x;
97 this.y = (double) t1.y;
101 * Constructs and initializes a Tuple2d to (0,0).
111 * Sets the value of this tuple to the specified xy coordinates.
112 * @param x the x coordinate
113 * @param y the y coordinate
115 public final void set(double x, double y)
123 * Sets the value of this tuple from the 2 values specified in
125 * @param t the array of length 2 containing xy in order
127 public final void set(double[] t)
135 * Sets the value of this tuple to the value of the Tuple2d argument.
136 * @param t1 the tuple to be copied
138 public final void set(Tuple2d t1)
146 * Sets the value of this tuple to the value of Tuple2f t1.
147 * @param t1 the tuple to be copied
149 public final void set(Tuple2f t1)
151 this.x = (double) t1.x;
152 this.y = (double) t1.y;
156 * Copies the value of the elements of this tuple into the array t.
157 * @param t the array that will contain the values of the vector
159 public final void get(double[] t)
167 * Sets the value of this tuple to the vector sum of tuples t1 and t2.
168 * @param t1 the first tuple
169 * @param t2 the second tuple
171 public final void add(Tuple2d t1, Tuple2d t2)
173 this.x = t1.x + t2.x;
174 this.y = t1.y + t2.y;
179 * Sets the value of this tuple to the vector sum of itself and tuple t1.
180 * @param t1 the other tuple
182 public final void add(Tuple2d t1)
190 * Sets the value of this tuple to the vector difference of
191 * tuple t1 and t2 (this = t1 - t2).
192 * @param t1 the first tuple
193 * @param t2 the second tuple
195 public final void sub(Tuple2d t1, Tuple2d t2)
197 this.x = t1.x - t2.x;
198 this.y = t1.y - t2.y;
203 * Sets the value of this tuple to the vector difference of
204 * itself and tuple t1 (this = this - t1).
205 * @param t1 the other vector
207 public final void sub(Tuple2d t1)
215 * Sets the value of this tuple to the negation of tuple t1.
216 * @param t1 the source vector
218 public final void negate(Tuple2d t1)
226 * Negates the value of this vector in place.
228 public final void negate()
236 * Sets the value of this tuple to the scalar multiplication
238 * @param s the scalar value
239 * @param t1 the source tuple
241 public final void scale(double s, Tuple2d t1)
249 * Sets the value of this tuple to the scalar multiplication
251 * @param s the scalar value
253 public final void scale(double s)
261 * Sets the value of this tuple to the scalar multiplication
262 * of tuple t1 and then adds tuple t2 (this = s*t1 + t2).
263 * @param s the scalar value
264 * @param t1 the tuple to be multipled
265 * @param t2 the tuple to be added
267 public final void scaleAdd(double s, Tuple2d t1, Tuple2d t2)
269 this.x = s*t1.x + t2.x;
270 this.y = s*t1.y + t2.y;
275 * Sets the value of this tuple to the scalar multiplication
276 * of itself and then adds tuple t1 (this = s*this + t1).
277 * @param s the scalar value
278 * @param t1 the tuple to be added
280 public final void scaleAdd(double s, Tuple2d t1)
282 this.x = s*this.x + t1.x;
283 this.y = s*this.y + t1.y;
289 * Returns a hash code value based on the data values in this
290 * object. Two different Tuple2d objects with identical data values
291 * (i.e., Tuple2d.equals returns true) will return the same hash
292 * code value. Two objects with different data members may return the
293 * same hash value, although this is not likely.
294 * @return the integer hash code value
296 public int hashCode() {
298 bits = 31L * bits + VecMathUtil.doubleToLongBits(x);
299 bits = 31L * bits + VecMathUtil.doubleToLongBits(y);
300 return (int) (bits ^ (bits >> 32));
305 * Returns true if all of the data members of Tuple2d t1 are
306 * equal to the corresponding data members in this Tuple2d.
307 * @param t1 the vector with which the comparison is made
308 * @return true or false
310 public boolean equals(Tuple2d t1)
313 return(this.x == t1.x && this.y == t1.y);
315 catch (NullPointerException e2) {return false;}
320 * Returns true if the Object t1 is of type Tuple2d and all of the
321 * data members of t1 are equal to the corresponding data members in
323 * @param t1 the object with which the comparison is made
324 * @return true or false
326 public boolean equals(Object t1)
329 Tuple2d t2 = (Tuple2d) t1;
330 return(this.x == t2.x && this.y == t2.y);
332 catch (NullPointerException e2) {return false;}
333 catch (ClassCastException e1) {return false;}
338 * Returns true if the L-infinite distance between this tuple
339 * and tuple t1 is less than or equal to the epsilon parameter,
340 * otherwise returns false. The L-infinite
341 * distance is equal to MAX[abs(x1-x2), abs(y1-y2)].
342 * @param t1 the tuple to be compared to this tuple
343 * @param epsilon the threshold value
344 * @return true or false
346 public boolean epsilonEquals(Tuple2d t1, double epsilon)
351 if(Double.isNaN(diff)) return false;
352 if((diff<0?-diff:diff) > epsilon) return false;
355 if(Double.isNaN(diff)) return false;
356 if((diff<0?-diff:diff) > epsilon) return false;
362 * Returns a string that contains the values of this Tuple2d.
364 * @return the String representation
366 public String toString()
368 return("(" + this.x + ", " + this.y + ")");
373 * Clamps the tuple parameter to the range [low, high] and
374 * places the values into this tuple.
375 * @param min the lowest value in the tuple after clamping
376 * @param max the highest value in the tuple after clamping
377 * @param t the source tuple, which will not be modified
379 public final void clamp(double min, double max, Tuple2d t)
383 } else if( t.x < min ){
391 } else if( t.y < min ){
401 * Clamps the minimum value of the tuple parameter to the min
402 * parameter and places the values into this tuple.
403 * @param min the lowest value in the tuple after clamping
404 * @param t the source tuple, which will not be modified
406 public final void clampMin(double min, Tuple2d t)
424 * Clamps the maximum value of the tuple parameter to the max
425 * parameter and places the values into this tuple.
426 * @param max the highest value in the tuple after clamping
427 * @param t the source tuple, which will not be modified
429 public final void clampMax(double max, Tuple2d t)
447 * Sets each component of the tuple parameter to its absolute
448 * value and places the modified values into this tuple.
449 * @param t the source tuple, which will not be modified
451 public final void absolute(Tuple2d t)
460 * Clamps this tuple to the range [low, high].
461 * @param min the lowest value in this tuple after clamping
462 * @param max the highest value in this tuple after clamping
464 public final void clamp(double min, double max)
468 } else if( x < min ){
474 } else if( y < min ){
482 * Clamps the minimum value of this tuple to the min parameter.
483 * @param min the lowest value in this tuple after clamping
485 public final void clampMin(double min)
493 * Clamps the maximum value of this tuple to the max parameter.
494 * @param max the highest value in the tuple after clamping
496 public final void clampMax(double max)
504 * Sets each component of this tuple to its absolute value.
506 public final void absolute()
514 * Linearly interpolates between tuples t1 and t2 and places the
515 * result into this tuple: this = (1-alpha)*t1 + alpha*t2.
516 * @param t1 the first tuple
517 * @param t2 the second tuple
518 * @param alpha the alpha interpolation parameter
520 public final void interpolate(Tuple2d t1, Tuple2d t2, double alpha)
522 this.x = (1-alpha)*t1.x + alpha*t2.x;
523 this.y = (1-alpha)*t1.y + alpha*t2.y;
528 * Linearly interpolates between this tuple and tuple t1 and
529 * places the result into this tuple: this = (1-alpha)*this + alpha*t1.
530 * @param t1 the first tuple
531 * @param alpha the alpha interpolation parameter
533 public final void interpolate(Tuple2d t1, double alpha)
535 this.x = (1-alpha)*this.x + alpha*t1.x;
536 this.y = (1-alpha)*this.y + alpha*t1.y;
541 * Creates a new object of the same class as this object.
543 * @return a clone of this instance.
544 * @exception OutOfMemoryError if there is not enough memory.
545 * @see java.lang.Cloneable
548 public Object clone() {
549 // Since there are no arrays we can just use Object.clone()
551 return super.clone();
552 } catch (CloneNotSupportedException e) {
553 // this shouldn't happen, since we are Cloneable
554 throw new InternalError();
560 * Get the <i>x</i> coordinate.
562 * @return the <i>x</i> coordinate.
566 public final double getX() {
572 * Set the <i>x</i> coordinate.
574 * @param x value to <i>x</i> coordinate.
578 public final void setX(double x) {
584 * Get the <i>y</i> coordinate.
586 * @return the <i>y</i> coordinate.
590 public final double getY() {
596 * Set the <i>y</i> coordinate.
598 * @param y value to <i>y</i> coordinate.
602 public final void setY(double y) {