]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Vector2f.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Vector2f.java
1 /*
2  * $RCSfile: Vector2f.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.5 $
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 vector that is represented by single-precision floating 
38  * point x,y coordinates.
39  *
40  */
41 public class Vector2f extends Tuple2f implements java.io.Serializable {
42
43     // Combatible with 1.1
44     static final long serialVersionUID = -2168194326883512320L;
45
46     /**
47      * Constructs and initializes a Vector2f from the specified xy coordinates.
48      * @param x the x coordinate
49      * @param y the y coordinate
50      */
51     public Vector2f(float x, float y)
52     {
53       super(x,y);
54     }
55
56
57     /**
58      * Constructs and initializes a Vector2f from the specified array.
59      * @param v the array of length 2 containing xy in order
60      */
61     public Vector2f(float[] v)
62     {
63       super(v);
64     }
65
66
67     /**
68      * Constructs and initializes a Vector2f from the specified Vector2f.
69      * @param v1 the Vector2f containing the initialization x y data
70      */
71     public Vector2f(Vector2f v1)
72     {
73        super(v1);
74     }
75
76
77     /**
78      * Constructs and initializes a Vector2f from the specified Vector2d.
79      * @param v1 the Vector2d containing the initialization x y data
80      */
81     public Vector2f(Vector2d v1)
82     {
83        super(v1);
84     }
85
86
87     /**
88      * Constructs and initializes a Vector2f from the specified Tuple2f.
89      * @param t1 the Tuple2f containing the initialization x y data
90      */  
91     public Vector2f(Tuple2f t1)
92     {
93        super(t1);
94     }
95
96
97     /**
98      * Constructs and initializes a Vector2f from the specified Tuple2d.
99      * @param t1 the Tuple2d containing the initialization x y data
100      */  
101     public Vector2f(Tuple2d t1)
102     {
103        super(t1);
104     }
105
106
107
108     /**
109      * Constructs and initializes a Vector2f to (0,0).
110      */
111     public Vector2f()
112     {
113         super();
114     }
115
116
117   /**
118    * Computes the dot product of the this vector and vector v1.
119    * @param v1 the other vector
120    */
121   public final float dot(Vector2f v1)
122     {
123       return (this.x*v1.x + this.y*v1.y);
124     }
125
126
127     /**  
128      * Returns the length of this vector.
129      * @return the length of this vector
130      */  
131     public final float length()
132     {
133         return (float) Math.sqrt(this.x*this.x + this.y*this.y);
134     }
135
136     /**  
137      * Returns the squared length of this vector.
138      * @return the squared length of this vector
139      */  
140     public final float lengthSquared()
141     {
142         return (this.x*this.x + this.y*this.y);
143     }
144
145     /**
146      * Sets the value of this vector to the normalization of vector v1.
147      * @param v1 the un-normalized vector
148      */  
149     public final void normalize(Vector2f v1)
150     {
151         float norm;
152
153         norm = (float) (1.0/Math.sqrt(v1.x*v1.x + v1.y*v1.y));
154         this.x = v1.x*norm;
155         this.y = v1.y*norm;
156     }
157
158     /**
159      * Normalizes this vector in place.
160      */  
161     public final void normalize()
162     {
163         float norm;
164
165         norm = (float)
166                (1.0/Math.sqrt(this.x*this.x + this.y*this.y));
167         this.x *= norm;
168         this.y *= norm;
169     }
170
171
172   /**
173     *   Returns the angle in radians between this vector and the vector
174     *   parameter; the return value is constrained to the range [0,PI].
175     *   @param v1    the other vector
176     *   @return   the angle in radians in the range [0,PI]
177     */
178    public final float angle(Vector2f v1)
179    {
180       double vDot = this.dot(v1) / ( this.length()*v1.length() );
181       if( vDot < -1.0) vDot = -1.0;
182       if( vDot >  1.0) vDot =  1.0;
183       return((float) (Math.acos( vDot )));
184    }
185
186
187 }