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