]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Tuple2f.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Tuple2f.java
1 /*
2  * $RCSfile: Tuple2f.java,v $
3  *
4  * Copyright 1997-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.8 $
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 generic 2-element tuple that is represented by single-precision  
38  * floating point x,y coordinates.
39  *
40  */
41 public abstract class Tuple2f implements java.io.Serializable, Cloneable {
42
43     static final long serialVersionUID = 9011180388985266884L;
44
45     /**
46      * The x coordinate.
47      */
48     public      float   x;
49
50     /**
51      * The y coordinate.
52      */
53     public      float   y;
54
55
56     /**
57      * Constructs and initializes a Tuple2f from the specified xy coordinates.
58      * @param x the x coordinate
59      * @param y the y coordinate
60      */
61     public Tuple2f(float x, float y)
62     {
63         this.x = x;
64         this.y = y;
65     }
66
67
68     /**
69      * Constructs and initializes a Tuple2f from the specified array.
70      * @param t the array of length 2 containing xy in order
71      */
72     public Tuple2f(float[] t)
73     {
74         this.x = t[0];
75         this.y = t[1];
76     }
77
78
79     /**
80      * Constructs and initializes a Tuple2f from the specified Tuple2f.
81      * @param t1 the Tuple2f containing the initialization x y data
82      */
83     public Tuple2f(Tuple2f t1)
84     {
85         this.x = t1.x;
86         this.y = t1.y;
87     }
88
89
90     /**
91      * Constructs and initializes a Tuple2f from the specified Tuple2d.
92      * @param t1 the Tuple2d containing the initialization x y data
93      */
94     public Tuple2f(Tuple2d t1)
95     {
96         this.x = (float) t1.x;
97         this.y = (float) t1.y;
98     }
99
100
101     /**
102      * Constructs and initializes a Tuple2f to (0,0).
103      */
104     public Tuple2f()
105     {
106         this.x = (float) 0.0;
107         this.y = (float) 0.0;
108     }
109
110
111     /**
112      * Sets the value of this tuple to the specified xy coordinates.
113      * @param x the x coordinate
114      * @param y the y coordinate
115      */
116     public final void set(float x, float y)
117     {
118         this.x = x;
119         this.y = y;
120     }
121
122
123     /**
124      * Sets the value of this tuple from the 2 values specified in 
125      * the array.
126      * @param t the array of length 2 containing xy in order
127      */
128     public final void set(float[] t)
129     {
130         this.x = t[0];
131         this.y = t[1];
132     }
133
134
135     /**
136      * Sets the value of this tuple to the value of the Tuple2f argument.
137      * @param t1 the tuple to be copied
138      */
139     public final void set(Tuple2f t1)
140     {
141         this.x = t1.x;
142         this.y = t1.y;
143     }
144  
145
146     /**
147      * Sets the value of this tuple to the value of the Tuple2d argument.
148      * @param t1 the tuple to be copied
149      */
150     public final void set(Tuple2d t1)
151     {
152         this.x = (float) t1.x;
153         this.y = (float) t1.y;
154     }
155
156
157    /**
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
160     */
161    public final void get(float[] t)
162     {
163         t[0] = this.x;
164         t[1] = this.y;
165     }
166
167
168     /**
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
172      */
173     public final void add(Tuple2f t1, Tuple2f t2)
174     {
175         this.x = t1.x + t2.x;
176         this.y = t1.y + t2.y;
177     }
178
179
180     /**
181      * Sets the value of this tuple to the vector sum of itself and tuple t1.
182      * @param t1 the other tuple
183      */  
184     public final void add(Tuple2f t1)
185     {
186         this.x += t1.x;
187         this.y += t1.y;
188     }
189
190
191     /**
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
196      */  
197     public final void sub(Tuple2f t1, Tuple2f t2)
198     {
199         this.x = t1.x - t2.x;
200         this.y = t1.y - t2.y;
201     }  
202
203
204     /**
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
208      */  
209     public final void sub(Tuple2f t1)
210     {
211         this.x -= t1.x;
212         this.y -= t1.y;
213     }
214
215
216     /**
217      * Sets the value of this tuple to the negation of tuple t1.
218      * @param t1 the source tuple
219      */
220     public final void negate(Tuple2f t1)
221     {
222         this.x = -t1.x;
223         this.y = -t1.y;
224     }
225
226
227     /**
228      * Negates the value of this vector in place.
229      */
230     public final void negate()
231     {
232         this.x = -this.x;
233         this.y = -this.y;
234     }
235
236
237     /**
238      * Sets the value of this tuple to the scalar multiplication
239      * of tuple t1.
240      * @param s the scalar value
241      * @param t1 the source tuple
242      */
243     public final void scale(float s, Tuple2f t1)
244     {
245         this.x = s*t1.x;
246         this.y = s*t1.y;
247     }
248
249
250     /**
251      * Sets the value of this tuple to the scalar multiplication
252      * of itself.
253      * @param s the scalar value
254      */
255     public final void scale(float s)
256     {
257         this.x *= s;
258         this.y *= s;
259     }
260
261
262     /**
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
268      */  
269     public final void scaleAdd(float s, Tuple2f t1, Tuple2f t2)
270     {
271         this.x = s*t1.x + t2.x; 
272         this.y = s*t1.y + t2.y; 
273     } 
274  
275
276     /**
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
281      */
282     public final void scaleAdd(float s, Tuple2f t1)
283     {
284         this.x = s*this.x + t1.x;
285         this.y = s*this.y + t1.y;
286     }
287
288
289
290     /**
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
297      */  
298     public int hashCode() {
299         long bits = 1L;
300         bits = 31L * bits + (long)VecMathUtil.floatToIntBits(x);
301         bits = 31L * bits + (long)VecMathUtil.floatToIntBits(y);
302         return (int) (bits ^ (bits >> 32));
303     }
304
305
306    /**   
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
311      */  
312     public boolean equals(Tuple2f t1)
313     {
314         try {
315            return(this.x == t1.x && this.y == t1.y);
316         }
317         catch (NullPointerException e2) {return false;}
318
319     }
320
321    /**   
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
324      * this Tuple2f.
325      * @param t1  the object with which the comparison is made
326      * @return  true or false
327      */  
328     public boolean equals(Object t1)
329     {
330         try {
331            Tuple2f t2 = (Tuple2f) t1;
332            return(this.x == t2.x && this.y == t2.y);
333         }
334         catch (NullPointerException e2) {return false;}
335         catch (ClassCastException   e1) {return false;}
336
337     }
338
339    /**
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
347      */
348     public boolean epsilonEquals(Tuple2f t1, float epsilon)
349     {
350        float diff;
351
352        diff = x - t1.x;
353        if(Float.isNaN(diff)) return false;
354        if((diff<0?-diff:diff) > epsilon) return false;
355
356        diff = y - t1.y;
357        if(Float.isNaN(diff)) return false;
358        if((diff<0?-diff:diff) > epsilon) return false;
359
360        return true;
361     }
362
363    /**
364      * Returns a string that contains the values of this Tuple2f.
365      * The form is (x,y).
366      * @return the String representation
367      */  
368    public String toString()
369    {
370         return("(" + this.x + ", " + this.y + ")");
371    }
372
373
374   /**
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
380     */
381    public final void clamp(float min, float max, Tuple2f t)
382    {
383         if( t.x > max ) { 
384           x = max;
385         } else if( t.x < min ){
386           x = min;
387         } else {
388           x = t.x;
389         }
390
391         if( t.y > max ) { 
392           y = max;
393         } else if( t.y < min ){
394           y = min;
395         } else {
396           y = t.y;
397         }
398
399    }
400
401
402   /** 
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
407     */   
408    public final void clampMin(float min, Tuple2f t) 
409    { 
410         if( t.x < min ) { 
411           x = min;
412         } else {
413           x = t.x;
414         }
415
416         if( t.y < min ) { 
417           y = min;
418         } else {
419           y = t.y;
420         }
421
422    } 
423
424
425   /**  
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
430     */    
431    public final void clampMax(float max, Tuple2f t)  
432    {  
433         if( t.x > max ) { 
434           x = max;
435         } else { 
436           x = t.x;
437         }
438  
439         if( t.y > max ) {
440           y = max;
441         } else {
442           y = t.y;
443         }
444
445    } 
446
447
448   /**  
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
452     */    
453   public final void absolute(Tuple2f t)
454   {
455        x = Math.abs(t.x);
456        y = Math.abs(t.y);
457   } 
458
459
460
461   /**
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
465     */
466    public final void clamp(float min, float max)
467    {
468         if( x > max ) {
469           x = max;
470         } else if( x < min ){
471           x = min;
472         }
473  
474         if( y > max ) {
475           y = max;
476         } else if( y < min ){
477           y = min;
478         }
479
480    }
481
482  
483   /**
484     *  Clamps the minimum value of this tuple to the min parameter.
485     *  @param min   the lowest value in this tuple after clamping
486     */
487    public final void clampMin(float min)
488    { 
489       if( x < min ) x=min;
490       if( y < min ) y=min;
491    } 
492  
493  
494   /**
495     *  Clamps the maximum value of this tuple to the max parameter.
496     *  @param max   the highest value in the tuple after clamping
497     */
498    public final void clampMax(float max)
499    { 
500       if( x > max ) x=max;
501       if( y > max ) y=max;
502    }
503
504
505   /**
506     *  Sets each component of this tuple to its absolute value.
507     */
508   public final void absolute()
509   {
510      x = Math.abs(x);
511      y = Math.abs(y);
512   }
513
514
515   /** 
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
521     */
522   public final void interpolate(Tuple2f t1, Tuple2f t2, float alpha)
523   {
524            this.x = (1-alpha)*t1.x + alpha*t2.x;
525            this.y = (1-alpha)*t1.y + alpha*t2.y;
526  
527   }
528
529
530   /**  
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  
535     */   
536   public final void interpolate(Tuple2f t1, float alpha) 
537   { 
538
539      this.x = (1-alpha)*this.x + alpha*t1.x;
540      this.y = (1-alpha)*this.y + alpha*t1.y;
541
542   } 
543
544     /**
545      * Creates a new object of the same class as this object.
546      *
547      * @return a clone of this instance.
548      * @exception OutOfMemoryError if there is not enough memory.
549      * @see java.lang.Cloneable
550      * @since vecmath 1.3
551      */
552     public Object clone() {
553         // Since there are no arrays we can just use Object.clone()
554         try {
555             return super.clone();
556         } catch (CloneNotSupportedException e) {
557             // this shouldn't happen, since we are Cloneable
558             throw new InternalError();
559         }
560     }
561
562
563     /**
564          * Get the <i>x</i> coordinate.
565          * 
566          * @return  the <i>x</i> coordinate.
567          * 
568          * @since vecmath 1.5
569          */
570         public final float getX() {
571                 return x;
572         }
573
574
575         /**
576          * Set the <i>x</i> coordinate.
577          * 
578          * @param x  value to <i>x</i> coordinate.
579          * 
580          * @since vecmath 1.5
581          */
582         public final void setX(float x) {
583                 this.x = x;
584         }
585
586
587         /**
588          * Get the <i>y</i> coordinate.
589          * 
590          * @return  the <i>y</i> coordinate.
591          * 
592          * @since vecmath 1.5
593          */
594         public final float getY() {
595                 return y;
596         }
597
598
599         /**
600          * Set the <i>y</i> coordinate.
601          * 
602          * @param y value to <i>y</i> coordinate.
603          * 
604          * @since vecmath 1.5
605          */
606         public final void setY(float y) {
607                 this.y = y;
608         }
609 }