2 * $RCSfile: Tuple2i.java,v $
4 * Copyright 2005-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 2-element tuple represented by signed integer x,y
42 public abstract class Tuple2i implements java.io.Serializable, Cloneable {
44 static final long serialVersionUID = -3555701650170169638L;
58 * Constructs and initializes a Tuple2i from the specified
59 * x and y coordinates.
60 * @param x the x coordinate
61 * @param y the y coordinate
63 public Tuple2i(int x, int y) {
70 * Constructs and initializes a Tuple2i from the array of length 2.
71 * @param t the array of length 2 containing x and y in order.
73 public Tuple2i(int[] t) {
80 * Constructs and initializes a Tuple2i from the specified Tuple2i.
81 * @param t1 the Tuple2i containing the initialization x and y
84 public Tuple2i(Tuple2i t1) {
91 * Constructs and initializes a Tuple2i to (0,0).
100 * Sets the value of this tuple to the specified x and y
102 * @param x the x coordinate
103 * @param y the y coordinate
105 public final void set(int x, int y) {
112 * Sets the value of this tuple to the specified coordinates in the
114 * @param t the array of length 2 containing x and y in order.
116 public final void set(int[] t) {
123 * Sets the value of this tuple to the value of tuple t1.
124 * @param t1 the tuple to be copied
126 public final void set(Tuple2i t1) {
133 * Copies the values of this tuple into the array t.
134 * @param t is the array
136 public final void get(int[] t) {
143 * Copies the values of this tuple into the tuple t.
144 * @param t is the target tuple
146 public final void get(Tuple2i t) {
153 * Sets the value of this tuple to the sum of tuples t1 and t2.
154 * @param t1 the first tuple
155 * @param t2 the second tuple
157 public final void add(Tuple2i t1, Tuple2i t2) {
158 this.x = t1.x + t2.x;
159 this.y = t1.y + t2.y;
164 * Sets the value of this tuple to the sum of itself and t1.
165 * @param t1 the other tuple
167 public final void add(Tuple2i t1) {
174 * Sets the value of this tuple to the difference
175 * of tuples t1 and t2 (this = t1 - t2).
176 * @param t1 the first tuple
177 * @param t2 the second tuple
179 public final void sub(Tuple2i t1, Tuple2i t2) {
180 this.x = t1.x - t2.x;
181 this.y = t1.y - t2.y;
186 * Sets the value of this tuple to the difference
187 * of itself and t1 (this = this - t1).
188 * @param t1 the other tuple
190 public final void sub(Tuple2i t1) {
197 * Sets the value of this tuple to the negation of tuple t1.
198 * @param t1 the source tuple
200 public final void negate(Tuple2i t1) {
207 * Negates the value of this tuple in place.
209 public final void negate() {
216 * Sets the value of this tuple to the scalar multiplication
218 * @param s the scalar value
219 * @param t1 the source tuple
221 public final void scale(int s, Tuple2i t1) {
228 * Sets the value of this tuple to the scalar multiplication
229 * of the scale factor with this.
230 * @param s the scalar value
232 public final void scale(int s) {
239 * Sets the value of this tuple to the scalar multiplication
240 * of tuple t1 plus tuple t2 (this = s*t1 + t2).
241 * @param s the scalar value
242 * @param t1 the tuple to be multipled
243 * @param t2 the tuple to be added
245 public final void scaleAdd(int s, Tuple2i t1, Tuple2i t2) {
246 this.x = s*t1.x + t2.x;
247 this.y = s*t1.y + t2.y;
252 * Sets the value of this tuple to the scalar multiplication
253 * of itself and then adds tuple t1 (this = s*this + t1).
254 * @param s the scalar value
255 * @param t1 the tuple to be added
257 public final void scaleAdd(int s, Tuple2i t1) {
258 this.x = s*this.x + t1.x;
259 this.y = s*this.y + t1.y;
264 * Returns a string that contains the values of this Tuple2i.
266 * @return the String representation
268 public String toString() {
269 return "(" + this.x + ", " + this.y + ")";
274 * Returns true if the Object t1 is of type Tuple2i and all of the
275 * data members of t1 are equal to the corresponding data members in
277 * @param t1 the object with which the comparison is made
279 public boolean equals(Object t1) {
281 Tuple2i t2 = (Tuple2i) t1;
282 return(this.x == t2.x && this.y == t2.y);
284 catch (NullPointerException e2) {
287 catch (ClassCastException e1) {
294 * Returns a hash code value based on the data values in this
295 * object. Two different Tuple2i objects with identical data values
296 * (i.e., Tuple2i.equals returns true) will return the same hash
297 * code value. Two objects with different data members may return the
298 * same hash value, although this is not likely.
299 * @return the integer hash code value
301 public int hashCode() {
303 bits = 31L * bits + (long)x;
304 bits = 31L * bits + (long)y;
305 return (int) (bits ^ (bits >> 32));
310 * Clamps the tuple parameter to the range [low, high] and
311 * places the values into this tuple.
312 * @param min the lowest value in the tuple after clamping
313 * @param max the highest value in the tuple after clamping
314 * @param t the source tuple, which will not be modified
316 public final void clamp(int min, int max, Tuple2i t) {
319 } else if( t.x < min ) {
327 } else if( t.y < min ) {
336 * Clamps the minimum value of the tuple parameter to the min
337 * parameter and places the values into this tuple.
338 * @param min the lowest value in the tuple after clamping
339 * @param t the source tuple, which will not be modified
341 public final void clampMin(int min, Tuple2i t) {
357 * Clamps the maximum value of the tuple parameter to the max
358 * parameter and places the values into this tuple.
359 * @param max the highest value in the tuple after clamping
360 * @param t the source tuple, which will not be modified
362 public final void clampMax(int max, Tuple2i t) {
378 * Sets each component of the tuple parameter to its absolute
379 * value and places the modified values into this tuple.
380 * @param t the source tuple, which will not be modified
382 public final void absolute(Tuple2i t) {
389 * Clamps this tuple to the range [low, high].
390 * @param min the lowest value in this tuple after clamping
391 * @param max the highest value in this tuple after clamping
393 public final void clamp(int min, int max) {
396 } else if( x < min ) {
402 } else if( y < min ) {
409 * Clamps the minimum value of this tuple to the min parameter.
410 * @param min the lowest value in this tuple after clamping
412 public final void clampMin(int min) {
422 * Clamps the maximum value of this tuple to the max parameter.
423 * @param max the highest value in the tuple after clamping
425 public final void clampMax(int max) {
435 * Sets each component of this tuple to its absolute value.
437 public final void absolute() {
443 * Creates a new object of the same class as this object.
445 * @return a clone of this instance.
446 * @exception OutOfMemoryError if there is not enough memory.
447 * @see java.lang.Cloneable
449 public Object clone() {
450 // Since there are no arrays we can just use Object.clone()
452 return super.clone();
453 } catch (CloneNotSupportedException e) {
454 // this shouldn't happen, since we are Cloneable
455 throw new InternalError();
461 * Get the <i>x</i> coordinate.
463 * @return the x coordinate.
467 public final int getX() {
473 * Set the <i>x</i> coordinate.
475 * @param x value to <i>x</i> coordinate.
479 public final void setX(int x) {
485 * Get the <i>y</i> coordinate.
487 * @return the <i>y</i> coordinate.
491 public final int getY() {
497 * Set the <i>y</i> coordinate.
499 * @param y value to <i>y</i> coordinate.
503 public final void setY(int y) {