2 * $RCSfile: Vector2f.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 2-element vector that is represented by single-precision floating
38 * point x,y coordinates.
41 public class Vector2f extends Tuple2f implements java.io.Serializable {
43 // Combatible with 1.1
44 static final long serialVersionUID = -2168194326883512320L;
47 * Constructs and initializes a Vector2f from the specified xy coordinates.
48 * @param x the x coordinate
49 * @param y the y coordinate
51 public Vector2f(float x, float y)
58 * Constructs and initializes a Vector2f from the specified array.
59 * @param v the array of length 2 containing xy in order
61 public Vector2f(float[] v)
68 * Constructs and initializes a Vector2f from the specified Vector2f.
69 * @param v1 the Vector2f containing the initialization x y data
71 public Vector2f(Vector2f v1)
78 * Constructs and initializes a Vector2f from the specified Vector2d.
79 * @param v1 the Vector2d containing the initialization x y data
81 public Vector2f(Vector2d v1)
88 * Constructs and initializes a Vector2f from the specified Tuple2f.
89 * @param t1 the Tuple2f containing the initialization x y data
91 public Vector2f(Tuple2f t1)
98 * Constructs and initializes a Vector2f from the specified Tuple2d.
99 * @param t1 the Tuple2d containing the initialization x y data
101 public Vector2f(Tuple2d t1)
109 * Constructs and initializes a Vector2f to (0,0).
118 * Computes the dot product of the this vector and vector v1.
119 * @param v1 the other vector
121 public final float dot(Vector2f v1)
123 return (this.x*v1.x + this.y*v1.y);
128 * Returns the length of this vector.
129 * @return the length of this vector
131 public final float length()
133 return (float) Math.sqrt(this.x*this.x + this.y*this.y);
137 * Returns the squared length of this vector.
138 * @return the squared length of this vector
140 public final float lengthSquared()
142 return (this.x*this.x + this.y*this.y);
146 * Sets the value of this vector to the normalization of vector v1.
147 * @param v1 the un-normalized vector
149 public final void normalize(Vector2f v1)
153 norm = (float) (1.0/Math.sqrt(v1.x*v1.x + v1.y*v1.y));
159 * Normalizes this vector in place.
161 public final void normalize()
166 (1.0/Math.sqrt(this.x*this.x + this.y*this.y));
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]
178 public final float angle(Vector2f v1)
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 )));