2 * $RCSfile: Vector4f.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 4-element vector represented by single-precision floating point x,y,z,w
41 public class Vector4f extends Tuple4f implements java.io.Serializable {
43 // Compatible with 1.1
44 static final long serialVersionUID = 8749319902347760659L;
47 * Constructs and initializes a Vector4f from the specified xyzw coordinates.
48 * @param x the x coordinate
49 * @param y the y coordinate
50 * @param z the z coordinate
51 * @param w the w coordinate
53 public Vector4f(float x, float y, float z, float w)
60 * Constructs and initializes a Vector4f from the array of length 4.
61 * @param v the array of length 4 containing xyzw in order
63 public Vector4f(float[] v)
70 * Constructs and initializes a Vector4f from the specified Vector4f.
71 * @param v1 the Vector4f containing the initialization x y z w data
73 public Vector4f(Vector4f v1)
80 * Constructs and initializes a Vector4f from the specified Vector4d.
81 * @param v1 the Vector4d containing the initialization x y z w data
83 public Vector4f(Vector4d v1)
90 * Constructs and initializes a Vector4f from the specified Tuple4f.
91 * @param t1 the Tuple4f containing the initialization x y z w data
93 public Vector4f(Tuple4f t1)
100 * Constructs and initializes a Vector4f from the specified Tuple4d.
101 * @param t1 the Tuple4d containing the initialization x y z w data
103 public Vector4f(Tuple4d t1)
110 * Constructs and initializes a Vector4f from the specified Tuple3f.
111 * The x,y,z components of this vector are set to the corresponding
112 * components of tuple t1. The w component of this vector
114 * @param t1 the tuple to be copied
118 public Vector4f(Tuple3f t1) {
119 super(t1.x, t1.y, t1.z, 0.0f);
124 * Constructs and initializes a Vector4f to (0,0,0,0).
133 * Sets the x,y,z components of this vector to the corresponding
134 * components of tuple t1. The w component of this vector
136 * @param t1 the tuple to be copied
140 public final void set(Tuple3f t1) {
149 * Returns the length of this vector.
150 * @return the length of this vector as a float
152 public final float length()
155 (float) Math.sqrt(this.x*this.x + this.y*this.y +
156 this.z*this.z + this.w*this.w);
160 * Returns the squared length of this vector
161 * @return the squared length of this vector as a float
163 public final float lengthSquared()
165 return (this.x*this.x + this.y*this.y +
166 this.z*this.z + this.w*this.w);
170 * returns the dot product of this vector and v1
171 * @param v1 the other vector
172 * @return the dot product of this vector and v1
174 public final float dot(Vector4f v1)
176 return (this.x*v1.x + this.y*v1.y + this.z*v1.z + this.w*v1.w);
181 * Sets the value of this vector to the normalization of vector v1.
182 * @param v1 the un-normalized vector
184 public final void normalize(Vector4f v1)
188 norm = (float) (1.0/Math.sqrt(v1.x*v1.x + v1.y*v1.y +
189 v1.z*v1.z + v1.w*v1.w));
198 * Normalizes this vector in place.
200 public final void normalize()
204 norm = (float) (1.0/Math.sqrt(this.x*this.x + this.y*this.y +
205 this.z*this.z + this.w*this.w));
214 * Returns the (4-space) angle in radians between this vector and
215 * the vector parameter; the return value is constrained to the
217 * @param v1 the other vector
218 * @return the angle in radians in the range [0,PI]
220 public final float angle(Vector4f v1)
222 double vDot = this.dot(v1) / ( this.length()*v1.length() );
223 if( vDot < -1.0) vDot = -1.0;
224 if( vDot > 1.0) vDot = 1.0;
225 return((float) (Math.acos( vDot )));