vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gm_vec2.h
Go to the documentation of this file.
1 // gmVector2.h - 2 element vector class
2 //
3 // libgm++: Graphics Math Library
4 // Ferdi Scheepers and Stephen F May
5 // 15 June 1994
6 // ----------------------------------
7 // modified: October 2002
8 // Pavel Kolcarek, kolcarek@fi.muni.cz
9 //
10 
11 #ifndef GM_VECTOR2_H
12 #define GM_VECTOR2_H
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <gm_utils.h>
18 
19 using namespace std;
20 
21 class gmVector2 {
22 
23 protected:
24  double v_[2];
25 
26 public:
27  gmVector2();
28  gmVector2(const gmVector2&);
29  gmVector2(double, double);
30 
31  // array access
32 
33  double& operator [](int);
34  const double& operator [](int) const;
35 
36  // assignment
37 
38  gmVector2& assign(double, double);
39  gmVector2& operator =(const gmVector2&);
40 
41  // math operators
42 
43  gmVector2& operator +=(const gmVector2&);
44  gmVector2& operator -=(const gmVector2&);
45  gmVector2& operator *=(double);
46  gmVector2& operator /=(double);
47 
48  gmVector2 operator +(const gmVector2&) const;
49  gmVector2 operator -(const gmVector2&) const;
50  gmVector2 operator -() const;
51  gmVector2 operator *(double) const;
52  gmVector2 operator /(double) const;
53 
54 friend gmVector2 operator *(double, const gmVector2&);
55 
56  bool operator ==(const gmVector2&) const;
57  bool operator !=(const gmVector2&) const;
58 
59  // operations
60 
61  gmVector2& clamp(double, double);
62  double length() const;
63  double lengthSquared() const;
64  gmVector2& normalize();
65 
66  void copyTo(float [2]) const;
67  void copyTo(double [2]) const;
68 
69 friend double distance(const gmVector2&, const gmVector2&);
70 friend double distanceSquared(const gmVector2&, const gmVector2&);
71 friend double dot(const gmVector2&, const gmVector2&);
72 friend gmVector2 lerp(double, const gmVector2&, const gmVector2&);
73 
74  // output
75 
76 friend ostream& operator << ( ostream &, const gmVector2 & );
77 };
78 
79 // CONSTRUCTORS
80 
82 {
83  v_[0] = v_[1] = 0;
84 }
85 
87 {
88  v_[0] = v.v_[0]; v_[1] = v.v_[1];
89 }
90 
91 inline gmVector2::gmVector2(double x, double y)
92 {
93  v_[0] = x; v_[1] = y;
94 }
95 
96 // ARRAY ACCESS
97 
98 inline double& gmVector2::operator [](int i)
99 {
100  assert(i == 0 || i == 1);
101  return v_[i];
102 }
103 
104 inline const double& gmVector2::operator [](int i) const
105 {
106  assert(i == 0 || i == 1);
107  return v_[i];
108 }
109 
110 // ASSIGNMENT
111 
112 inline gmVector2& gmVector2::assign(double x, double y)
113 {
114  v_[0] = x; v_[1] = y;
115  return *this;
116 }
117 
119 {
120  v_[0] = v[0]; v_[1] = v[1];
121  return *this;
122 }
123 
124 // MATH OPERATORS
125 
127 {
128  v_[0] += v[0]; v_[1] += v[1];
129  return *this;
130 }
131 
133 {
134  v_[0] -= v[0]; v_[1] -= v[1];
135  return *this;
136 }
137 
139 {
140  v_[0] *= c; v_[1] *= c;
141  return *this;
142 }
143 
145 {
146  assert(!gmIsZero(c));
147  v_[0] /= c; v_[1] /= c;
148  return *this;
149 }
150 
152 {
153  return gmVector2(v_[0] + v[0], v_[1] + v[1]);
154 }
155 
157 {
158  return gmVector2(v_[0] - v[0], v_[1] - v[1]);
159 }
160 
162 {
163  return gmVector2(-v_[0], -v_[1]);
164 }
165 
166 inline gmVector2 gmVector2::operator *(double c) const
167 {
168  return gmVector2(v_[0] * c, v_[1] * c);
169 }
170 
171 inline gmVector2 gmVector2::operator /(double c) const
172 {
173  assert(!gmIsZero(c));
174  return gmVector2(v_[0] / c, v_[1] / c);
175 }
176 
177 inline gmVector2 operator *(double c, const gmVector2& v)
178 {
179  return gmVector2(c * v[0], c * v[1]);
180 }
181 
182 inline bool gmVector2::operator ==(const gmVector2& v) const
183 {
184  return (gmFuzEQ(v_[0], v[0]) && gmFuzEQ(v_[1], v[1]));
185 }
186 
187 inline bool gmVector2::operator !=(const gmVector2& v) const
188 {
189  return (!(*this == v));
190 }
191 
192 // OPERATIONS
193 
194 inline gmVector2& gmVector2::clamp(double lo, double hi)
195 {
196  gmClamp(v_[0], lo, hi); gmClamp(v_[1], lo, hi);
197  return *this;
198 }
199 
200 inline double gmVector2::length() const
201 {
202  return sqrt(gmSqr(v_[0]) + gmSqr(v_[1]));
203 }
204 
205 inline double gmVector2::lengthSquared() const
206 {
207  return gmSqr(v_[0]) + gmSqr(v_[1]);
208 }
209 
211 {
212  double len = length();
213  assert(!gmIsZero(len));
214  *this /= len;
215  return *this;
216 }
217 
218 inline void gmVector2::copyTo(float f[2]) const
219 {
220  f[0] = v_[0]; f[1] = v_[1];
221 }
222 
223 inline void gmVector2::copyTo(double f[2]) const
224 {
225  f[0] = v_[0]; f[1] = v_[1];
226 }
227 
228 inline double distance(const gmVector2& v1, const gmVector2& v2)
229 {
230  return sqrt(gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]));
231 }
232 
233 inline double distanceSquared(const gmVector2& v1, const gmVector2& v2)
234 {
235  return gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]);
236 }
237 
238 inline double dot(const gmVector2& v1, const gmVector2& v2)
239 {
240  return v1[0] * v2[0] + v1[1] * v2[1];
241 }
242 
243 inline gmVector2 lerp(double f, const gmVector2& v1, const gmVector2& v2)
244 {
245  return v1 + ((v2 - v1) * f);
246 }
247 
248 // OUTPUT
249 
250 inline ostream& operator<< ( ostream& os, const gmVector2& v)
251 {
252  os << "< " << v[0] << " " << v[1] << " >";
253  return os;
254 }
255 
256 #endif
257 
258