2 * $RCSfile: Vector2d.java,v $
4 * Copyright 1998-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 double-precision floating
38 * point x,y coordinates.
41 public class Vector2d extends Tuple2d implements java.io.Serializable {
43 // Combatible with 1.1
44 static final long serialVersionUID = 8572646365302599857L;
47 * Constructs and initializes a Vector2d from the specified xy coordinates.
48 * @param x the x coordinate
49 * @param y the y coordinate
51 public Vector2d(double x, double y)
58 * Constructs and initializes a Vector2d from the specified array.
59 * @param v the array of length 2 containing xy in order
61 public Vector2d(double[] v)
68 * Constructs and initializes a Vector2d from the specified Vector2d.
69 * @param v1 the Vector2d containing the initialization x y data
71 public Vector2d(Vector2d v1)
78 * Constructs and initializes a Vector2d from the specified Vector2f.
79 * @param v1 the Vector2f containing the initialization x y data
81 public Vector2d(Vector2f v1)
88 * Constructs and initializes a Vector2d from the specified Tuple2d.
89 * @param t1 the Tuple2d containing the initialization x y data
91 public Vector2d(Tuple2d t1)
98 * Constructs and initializes a Vector2d from the specified Tuple2f.
99 * @param t1 the Tuple2f containing the initialization x y data
101 public Vector2d(Tuple2f t1)
108 * Constructs and initializes a Vector2d to (0,0).
117 * Computes the dot product of the this vector and vector v1.
118 * @param v1 the other vector
120 public final double dot(Vector2d v1)
122 return (this.x*v1.x + this.y*v1.y);
127 * Returns the length of this vector.
128 * @return the length of this vector
130 public final double length()
132 return (double) Math.sqrt(this.x*this.x + this.y*this.y);
136 * Returns the squared length of this vector.
137 * @return the squared length of this vector
139 public final double lengthSquared()
141 return (this.x*this.x + this.y*this.y);
145 * Sets the value of this vector to the normalization of vector v1.
146 * @param v1 the un-normalized vector
148 public final void normalize(Vector2d v1)
152 norm = (double) (1.0/Math.sqrt(v1.x*v1.x + v1.y*v1.y));
158 * Normalizes this vector in place.
160 public final void normalize()
165 (1.0/Math.sqrt(this.x*this.x + this.y*this.y));
172 * Returns the angle in radians between this vector and the vector
173 * parameter; the return value is constrained to the range [0,PI].
174 * @param v1 the other vector
175 * @return the angle in radians in the range [0,PI]
177 public final double angle(Vector2d v1)
179 double vDot = this.dot(v1) / ( this.length()*v1.length() );
180 if( vDot < -1.0) vDot = -1.0;
181 if( vDot > 1.0) vDot = 1.0;
182 return((double) (Math.acos( vDot )));