]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Tuple2i.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Tuple2i.java
1 /*
2  * $RCSfile: Tuple2i.java,v $
3  *
4  * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
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.
12  *
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).
18  *
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.
22  *
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
25  * have any questions.
26  *
27  * $Revision: 1.6 $
28  * $Date: 2008/02/28 20:18:51 $
29  * $State: Exp $
30  */
31
32 package javax.vecmath;
33
34 import java.lang.Math;
35
36 /**
37  * A 2-element tuple represented by signed integer x,y
38  * coordinates.
39  *
40  * @since vecmath 1.4
41  */
42 public abstract class Tuple2i implements java.io.Serializable, Cloneable {
43
44     static final long serialVersionUID = -3555701650170169638L;
45
46     /**
47      * The x coordinate.
48      */
49     public int x;
50
51     /**
52      * The y coordinate.
53      */
54     public int y;
55
56
57     /**
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
62      */
63     public Tuple2i(int x, int y) {
64         this.x = x;
65         this.y = y;
66     }
67
68
69     /**
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.
72      */
73     public Tuple2i(int[] t) {
74         this.x = t[0];
75         this.y = t[1];
76     }
77
78
79     /**
80      * Constructs and initializes a Tuple2i from the specified Tuple2i.
81      * @param t1 the Tuple2i containing the initialization x and y
82      * data.
83      */
84     public Tuple2i(Tuple2i t1) {
85         this.x = t1.x;
86         this.y = t1.y;
87     }
88
89
90     /**
91      * Constructs and initializes a Tuple2i to (0,0).
92      */
93     public Tuple2i() {
94         this.x = 0;
95         this.y = 0;
96     }
97
98
99     /**
100      * Sets the value of this tuple to the specified x and y
101      * coordinates.
102      * @param x the x coordinate
103      * @param y the y coordinate
104      */
105     public final void set(int x, int y) {
106         this.x = x;
107         this.y = y;
108     }
109
110
111     /**
112      * Sets the value of this tuple to the specified coordinates in the
113      * array of length 2.
114      * @param t the array of length 2 containing x and y in order.
115      */
116     public final void set(int[] t) {
117         this.x = t[0];
118         this.y = t[1];
119     }
120
121
122     /**
123      * Sets the value of this tuple to the value of tuple t1.
124      * @param t1 the tuple to be copied
125      */
126     public final void set(Tuple2i t1) {
127         this.x = t1.x;
128         this.y = t1.y;
129     }
130
131
132     /**
133      * Copies the values of this tuple into the array t.
134      * @param t is the array
135      */
136     public final void get(int[] t) {
137         t[0] = this.x;
138         t[1] = this.y;
139     }
140
141
142     /**
143      * Copies the values of this tuple into the tuple t.
144      * @param t is the target tuple
145      */
146     public final void get(Tuple2i t) {
147         t.x = this.x;
148         t.y = this.y;
149     }
150
151
152     /**
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
156      */
157     public final void add(Tuple2i t1, Tuple2i t2) {
158         this.x = t1.x + t2.x;
159         this.y = t1.y + t2.y;
160     }
161
162
163     /**
164      * Sets the value of this tuple to the sum of itself and t1.
165      * @param t1 the other tuple
166      */
167     public final void add(Tuple2i t1) {
168         this.x += t1.x;
169         this.y += t1.y;
170     }
171
172
173     /**
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
178      */
179     public final void sub(Tuple2i t1, Tuple2i t2) {
180         this.x = t1.x - t2.x;
181         this.y = t1.y - t2.y;
182     }
183
184
185     /**
186      * Sets the value of this tuple to the difference
187      * of itself and t1 (this = this - t1).
188      * @param t1 the other tuple
189      */
190     public final void sub(Tuple2i t1) {
191         this.x -= t1.x;
192         this.y -= t1.y;
193     }
194
195
196     /**
197      * Sets the value of this tuple to the negation of tuple t1.
198      * @param t1 the source tuple
199      */
200     public final void negate(Tuple2i t1) {
201         this.x = -t1.x;
202         this.y = -t1.y;
203     }
204
205
206     /**
207      * Negates the value of this tuple in place.
208      */
209     public final void negate() {
210         this.x = -this.x;
211         this.y = -this.y;
212     }
213
214
215     /**
216      * Sets the value of this tuple to the scalar multiplication
217      * of tuple t1.
218      * @param s the scalar value
219      * @param t1 the source tuple
220      */
221     public final void scale(int s, Tuple2i t1) {
222         this.x = s*t1.x;
223         this.y = s*t1.y;
224     }
225
226
227     /**
228      * Sets the value of this tuple to the scalar multiplication
229      * of the scale factor with this.
230      * @param s the scalar value
231      */
232     public final void scale(int s) {
233         this.x *= s;
234         this.y *= s;
235     }
236
237
238     /**
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
244      */
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;
248     }
249
250
251     /**
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
256      */
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;
260     }
261
262
263     /**
264      * Returns a string that contains the values of this Tuple2i.
265      * The form is (x,y).
266      * @return the String representation
267      */
268     public String toString() {
269         return "(" + this.x + ", " + this.y + ")";
270     }
271
272
273     /**
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
276      * this Tuple2i.
277      * @param t1  the object with which the comparison is made
278      */
279     public boolean equals(Object t1) {
280         try {
281             Tuple2i t2 = (Tuple2i) t1;
282             return(this.x == t2.x && this.y == t2.y);
283         }
284         catch (NullPointerException e2) {
285             return false;
286         }
287         catch (ClassCastException e1) {
288             return false;
289         }
290     }
291
292
293     /**
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
300      */
301     public int hashCode() {
302         long bits = 1L;
303         bits = 31L * bits + (long)x;
304         bits = 31L * bits + (long)y;
305         return (int) (bits ^ (bits >> 32));
306     }
307
308
309     /**
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
315      */
316     public final void clamp(int min, int max, Tuple2i t) {
317         if( t.x > max ) {
318             x = max;
319         } else if( t.x < min ) {
320             x = min;
321         } else {
322             x = t.x;
323         }
324
325         if( t.y > max ) {
326             y = max;
327         } else if( t.y < min ) {
328             y = min;
329         } else {
330             y = t.y;
331         }
332     }
333
334
335     /**
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
340      */
341     public final void clampMin(int min, Tuple2i t) {
342         if( t.x < min ) {
343             x = min;
344         } else {
345             x = t.x;
346         }
347
348         if( t.y < min ) {
349             y = min;
350         } else {
351             y = t.y;
352         }
353     }
354
355
356     /**
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
361      */
362     public final void clampMax(int max, Tuple2i t) {
363         if( t.x > max ) {
364             x = max;
365         } else {
366             x = t.x;
367         }
368
369         if( t.y > max ) {
370             y = max;
371         } else {
372             y = t.y;
373         }
374     }
375
376
377     /**
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
381      */
382     public final void absolute(Tuple2i t) {
383         x = Math.abs(t.x);
384         y = Math.abs(t.y);
385     }
386
387
388     /**
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
392      */
393     public final void clamp(int min, int max) {
394         if( x > max ) {
395             x = max;
396         } else if( x < min ) {
397             x = min;
398         }
399
400         if( y > max ) {
401             y = max;
402         } else if( y < min ) {
403             y = min;
404         }
405     }
406
407
408     /**
409      *  Clamps the minimum value of this tuple to the min parameter.
410      *  @param min   the lowest value in this tuple after clamping
411      */
412     public final void clampMin(int min) {
413         if (x < min)
414             x=min;
415
416         if (y < min)
417             y = min;
418     }
419
420
421     /**
422      *  Clamps the maximum value of this tuple to the max parameter.
423      *  @param max   the highest value in the tuple after clamping
424      */
425     public final void clampMax(int max) {
426         if (x > max)
427             x = max;
428
429         if (y > max)
430             y = max;
431     }
432
433
434     /**
435      *  Sets each component of this tuple to its absolute value.
436      */
437     public final void absolute() {
438         x = Math.abs(x);
439         y = Math.abs(y);
440     }
441
442     /**
443      * Creates a new object of the same class as this object.
444      *
445      * @return a clone of this instance.
446      * @exception OutOfMemoryError if there is not enough memory.
447      * @see java.lang.Cloneable
448      */
449     public Object clone() {
450         // Since there are no arrays we can just use Object.clone()
451         try {
452             return super.clone();
453         } catch (CloneNotSupportedException e) {
454             // this shouldn't happen, since we are Cloneable
455             throw new InternalError();
456         }
457     }
458
459
460     /**
461          * Get the <i>x</i> coordinate.
462          * 
463          * @return the x coordinate.
464          * 
465          * @since vecmath 1.5
466          */
467         public final int getX() {
468                 return x;
469         }
470
471
472         /**
473          * Set the <i>x</i> coordinate.
474          * 
475          * @param x  value to <i>x</i> coordinate.
476          * 
477          * @since vecmath 1.5
478          */
479         public final void setX(int x) {
480                 this.x = x;
481         }
482
483
484         /**
485          * Get the <i>y</i> coordinate.
486          * 
487          * @return  the <i>y</i> coordinate.
488          * 
489          * @since vecmath 1.5
490          */
491         public final int getY() {
492                 return y;
493         }
494
495
496         /**
497          * Set the <i>y</i> coordinate.
498          * 
499          * @param y value to <i>y</i> coordinate.
500          * 
501          * @since vecmath 1.5
502          */
503         public final void setY(int y) {
504                 this.y = y;
505         }
506 }