2 * $RCSfile: Tuple2f.java,v $
4 * Copyright 1997-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:51 $
32 package javax.vecmath;
34 import java.lang.Math;
37 * A generic 2-element tuple that is represented by single-precision
38 * floating point x,y coordinates.
41 public abstract class Tuple2f implements java.io.Serializable, Cloneable {
43 static final long serialVersionUID = 9011180388985266884L;
57 * Constructs and initializes a Tuple2f from the specified xy coordinates.
58 * @param x the x coordinate
59 * @param y the y coordinate
61 public Tuple2f(float x, float y)
69 * Constructs and initializes a Tuple2f from the specified array.
70 * @param t the array of length 2 containing xy in order
72 public Tuple2f(float[] t)
80 * Constructs and initializes a Tuple2f from the specified Tuple2f.
81 * @param t1 the Tuple2f containing the initialization x y data
83 public Tuple2f(Tuple2f t1)
91 * Constructs and initializes a Tuple2f from the specified Tuple2d.
92 * @param t1 the Tuple2d containing the initialization x y data
94 public Tuple2f(Tuple2d t1)
96 this.x = (float) t1.x;
97 this.y = (float) t1.y;
102 * Constructs and initializes a Tuple2f to (0,0).
106 this.x = (float) 0.0;
107 this.y = (float) 0.0;
112 * Sets the value of this tuple to the specified xy coordinates.
113 * @param x the x coordinate
114 * @param y the y coordinate
116 public final void set(float x, float y)
124 * Sets the value of this tuple from the 2 values specified in
126 * @param t the array of length 2 containing xy in order
128 public final void set(float[] t)
136 * Sets the value of this tuple to the value of the Tuple2f argument.
137 * @param t1 the tuple to be copied
139 public final void set(Tuple2f t1)
147 * Sets the value of this tuple to the value of the Tuple2d argument.
148 * @param t1 the tuple to be copied
150 public final void set(Tuple2d t1)
152 this.x = (float) t1.x;
153 this.y = (float) t1.y;
158 * Copies the value of the elements of this tuple into the array t.
159 * @param t the array that will contain the values of the vector
161 public final void get(float[] t)
169 * Sets the value of this tuple to the vector sum of tuples t1 and t2.
170 * @param t1 the first tuple
171 * @param t2 the second tuple
173 public final void add(Tuple2f t1, Tuple2f t2)
175 this.x = t1.x + t2.x;
176 this.y = t1.y + t2.y;
181 * Sets the value of this tuple to the vector sum of itself and tuple t1.
182 * @param t1 the other tuple
184 public final void add(Tuple2f t1)
192 * Sets the value of this tuple to the vector difference of
193 * tuple t1 and t2 (this = t1 - t2).
194 * @param t1 the first tuple
195 * @param t2 the second tuple
197 public final void sub(Tuple2f t1, Tuple2f t2)
199 this.x = t1.x - t2.x;
200 this.y = t1.y - t2.y;
205 * Sets the value of this tuple to the vector difference of
206 * itself and tuple t1 (this = this - t1).
207 * @param t1 the other tuple
209 public final void sub(Tuple2f t1)
217 * Sets the value of this tuple to the negation of tuple t1.
218 * @param t1 the source tuple
220 public final void negate(Tuple2f t1)
228 * Negates the value of this vector in place.
230 public final void negate()
238 * Sets the value of this tuple to the scalar multiplication
240 * @param s the scalar value
241 * @param t1 the source tuple
243 public final void scale(float s, Tuple2f t1)
251 * Sets the value of this tuple to the scalar multiplication
253 * @param s the scalar value
255 public final void scale(float s)
263 * Sets the value of this tuple to the scalar multiplication
264 * of tuple t1 and then adds tuple t2 (this = s*t1 + t2).
265 * @param s the scalar value
266 * @param t1 the tuple to be multipled
267 * @param t2 the tuple to be added
269 public final void scaleAdd(float s, Tuple2f t1, Tuple2f t2)
271 this.x = s*t1.x + t2.x;
272 this.y = s*t1.y + t2.y;
277 * Sets the value of this tuple to the scalar multiplication
278 * of itself and then adds tuple t1 (this = s*this + t1).
279 * @param s the scalar value
280 * @param t1 the tuple to be added
282 public final void scaleAdd(float s, Tuple2f t1)
284 this.x = s*this.x + t1.x;
285 this.y = s*this.y + t1.y;
291 * Returns a hash code value based on the data values in this
292 * object. Two different Tuple2f objects with identical data values
293 * (i.e., Tuple2f.equals returns true) will return the same hash
294 * code value. Two objects with different data members may return the
295 * same hash value, although this is not likely.
296 * @return the integer hash code value
298 public int hashCode() {
300 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(x);
301 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(y);
302 return (int) (bits ^ (bits >> 32));
307 * Returns true if all of the data members of Tuple2f t1 are
308 * equal to the corresponding data members in this Tuple2f.
309 * @param t1 the vector with which the comparison is made
310 * @return true or false
312 public boolean equals(Tuple2f t1)
315 return(this.x == t1.x && this.y == t1.y);
317 catch (NullPointerException e2) {return false;}
322 * Returns true if the Object t1 is of type Tuple2f and all of the
323 * data members of t1 are equal to the corresponding data members in
325 * @param t1 the object with which the comparison is made
326 * @return true or false
328 public boolean equals(Object t1)
331 Tuple2f t2 = (Tuple2f) t1;
332 return(this.x == t2.x && this.y == t2.y);
334 catch (NullPointerException e2) {return false;}
335 catch (ClassCastException e1) {return false;}
340 * Returns true if the L-infinite distance between this tuple
341 * and tuple t1 is less than or equal to the epsilon parameter,
342 * otherwise returns false. The L-infinite
343 * distance is equal to MAX[abs(x1-x2), abs(y1-y2)].
344 * @param t1 the tuple to be compared to this tuple
345 * @param epsilon the threshold value
346 * @return true or false
348 public boolean epsilonEquals(Tuple2f t1, float epsilon)
353 if(Float.isNaN(diff)) return false;
354 if((diff<0?-diff:diff) > epsilon) return false;
357 if(Float.isNaN(diff)) return false;
358 if((diff<0?-diff:diff) > epsilon) return false;
364 * Returns a string that contains the values of this Tuple2f.
366 * @return the String representation
368 public String toString()
370 return("(" + this.x + ", " + this.y + ")");
375 * Clamps the tuple parameter to the range [low, high] and
376 * places the values into this tuple.
377 * @param min the lowest value in the tuple after clamping
378 * @param max the highest value in the tuple after clamping
379 * @param t the source tuple, which will not be modified
381 public final void clamp(float min, float max, Tuple2f t)
385 } else if( t.x < min ){
393 } else if( t.y < min ){
403 * Clamps the minimum value of the tuple parameter to the min
404 * parameter and places the values into this tuple.
405 * @param min the lowest value in the tuple after clamping
406 * @param t the source tuple, which will not be modified
408 public final void clampMin(float min, Tuple2f t)
426 * Clamps the maximum value of the tuple parameter to the max
427 * parameter and places the values into this tuple.
428 * @param max the highest value in the tuple after clamping
429 * @param t the source tuple, which will not be modified
431 public final void clampMax(float max, Tuple2f t)
449 * Sets each component of the tuple parameter to its absolute
450 * value and places the modified values into this tuple.
451 * @param t the source tuple, which will not be modified
453 public final void absolute(Tuple2f t)
462 * Clamps this tuple to the range [low, high].
463 * @param min the lowest value in this tuple after clamping
464 * @param max the highest value in this tuple after clamping
466 public final void clamp(float min, float max)
470 } else if( x < min ){
476 } else if( y < min ){
484 * Clamps the minimum value of this tuple to the min parameter.
485 * @param min the lowest value in this tuple after clamping
487 public final void clampMin(float min)
495 * Clamps the maximum value of this tuple to the max parameter.
496 * @param max the highest value in the tuple after clamping
498 public final void clampMax(float max)
506 * Sets each component of this tuple to its absolute value.
508 public final void absolute()
516 * Linearly interpolates between tuples t1 and t2 and places the
517 * result into this tuple: this = (1-alpha)*t1 + alpha*t2.
518 * @param t1 the first tuple
519 * @param t2 the second tuple
520 * @param alpha the alpha interpolation parameter
522 public final void interpolate(Tuple2f t1, Tuple2f t2, float alpha)
524 this.x = (1-alpha)*t1.x + alpha*t2.x;
525 this.y = (1-alpha)*t1.y + alpha*t2.y;
531 * Linearly interpolates between this tuple and tuple t1 and
532 * places the result into this tuple: this = (1-alpha)*this + alpha*t1.
533 * @param t1 the first tuple
534 * @param alpha the alpha interpolation parameter
536 public final void interpolate(Tuple2f t1, float alpha)
539 this.x = (1-alpha)*this.x + alpha*t1.x;
540 this.y = (1-alpha)*this.y + alpha*t1.y;
545 * Creates a new object of the same class as this object.
547 * @return a clone of this instance.
548 * @exception OutOfMemoryError if there is not enough memory.
549 * @see java.lang.Cloneable
552 public Object clone() {
553 // Since there are no arrays we can just use Object.clone()
555 return super.clone();
556 } catch (CloneNotSupportedException e) {
557 // this shouldn't happen, since we are Cloneable
558 throw new InternalError();
564 * Get the <i>x</i> coordinate.
566 * @return the <i>x</i> coordinate.
570 public final float getX() {
576 * Set the <i>x</i> coordinate.
578 * @param x value to <i>x</i> coordinate.
582 public final void setX(float x) {
588 * Get the <i>y</i> coordinate.
590 * @return the <i>y</i> coordinate.
594 public final float getY() {
600 * Set the <i>y</i> coordinate.
602 * @param y value to <i>y</i> coordinate.
606 public final void setY(float y) {