-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVector2.h
171 lines (142 loc) · 4.35 KB
/
Vector2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright (C) 1997,1998,1999
Kenji Hiranabe, Eiwa System Management, Inc.
This program is free software.
Implemented by Kenji Hiranabe(hiranabe@esm.co.jp),
conforming to the Java(TM) 3D API specification by Sun Microsystems.
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc.
makes no representations about the suitability of this software for any
purpose. It is provided "AS IS" with NO WARRANTY.
*/
#ifndef VECTOR2_H
#define VECTOR2_H
#include <VmUtil.h>
#include <Tuple2.h>
VM_BEGIN_NS
/**
* A 2 element vector that is represented by x,y coordinates.
* @version specification 1.1, implementation $Revision: 1.3 $, $Date: 1999/10/06 02:52:46 $
* @author Kenji hiranabe
*/
template<class T>
class Vector2 : public Tuple2<T> {
/*
* $Log: Vector2.h,v $
* Revision 1.3 1999/10/06 02:52:46 hiranabe
* Java3D 1.2 and namespace
*
* Revision 1.2 1999/05/26 00:59:37 hiranabe
* support Visual C++
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
*/
public:
/**
* Constructs and initializes a Vector2 from the specified xy coordinates.
* @param x the x coordinate
* @param y the y coordinate
*/
Vector2(T x, T y) : Tuple2<T>(x, y) { }
/**
* Constructs and initializes a Vector2 from the specified array.
* @param v the array of length 2 containing xy in order
*/
Vector2(const T v[]) : Tuple2<T>(v) { }
/**
* Constructs and initializes a Vector2 from the specified Tuple2.
* @param t1 the Tuple2 containing the initialization x y data
*/
Vector2(const Tuple2<T>& t1) : Tuple2<T>(t1) { }
/**
* Constructs and initializes a Vector2 to (0,0).
*/
Vector2(): Tuple2<T>() { }
/**
* Computes the dot product of the this vector and vector v1.
* @param v1 the other vector
*/
T dot(const Vector2& v1) const {
return this->x*v1.x + this->y*v1.y;
}
/**
* Returns the squared length of this vector.
* @return the squared length of this vector
*/
T lengthSquared() const {
return this->x*this->x + this->y*this->y;
}
/**
* Returns the length of this vector.
* @return the length of this vector
*/
T length() const {
return VmUtil<T>::sqrt(lengthSquared());
}
/**
* Normalizes this vector in place.
*/
void normalize() {
T d = length();
// zero-div may occur.
this->x /= d;
this->y /= d;
}
/**
* Sets the value of this vector to the normalization of vector v1.
* @param v1 the un-normalized vector
*/
void normalize(const Vector2& v1) {
this->set(v1);
normalize();
}
/**
* Returns the angle in radians between this vector and
* the vector parameter; the return value is constrained to the
* range [0,PI].
* @param v1 the other vector
* @return the angle in radians in the range [0,PI]
*/
T angle(const Vector2& v1) const {
// stabler than acos
return VmUtil<T>::abs(VmUtil<T>::atan2(this->x*v1.y - this->y*v1.x , dot(v1)));
}
// copy constructor and operator = is made by complier
Vector2& operator=(const Tuple2<T>& t) {
Tuple2<T>::operator=(t);
return *this;
}
};
/*
* 0. value_type typedef added
* 1. copy constructo, oeprator = are delegated to compiler
* 4. typdef value type
* 7. typedefs for <float>, <double>
* removed construction from Vector2f
*/
VM_END_NS
template <class T>
inline
VM_VECMATH_NS::Vector2<T> operator*(T s, const VM_VECMATH_NS::Vector2<T>& t1) {
return operator*(s, (const VM_VECMATH_NS::Tuple2<T>&)t1);
}
#ifdef VM_INCLUDE_IO
template <class T>
inline
VM_IOSTREAM_STD::ostream& operator<<(VM_IOSTREAM_STD::ostream& o, const VM_VECMATH_NS::Vector2<T>& t1) {
return operator<<(o, (const VM_VECMATH_NS::Tuple2<T>&)t1);
}
#endif
VM_BEGIN_NS
typedef Vector2<double> Vector2d;
typedef Vector2<float> Vector2f;
VM_END_NS
#endif /* VECTOR2_H */