nux-0.9.48
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #ifndef VECTOR2_H 00024 #define VECTOR2_H 00025 00026 #include "../Exception.h" 00027 00028 #include "Vector4.h" 00029 #include "Vector3.h" 00030 00031 namespace nux 00032 { 00033 class Matrix2; 00034 template<typename T> class Vec2; 00035 00036 template <typename T> 00037 class Vec2 00038 { 00039 public: 00040 inline Vec2(); 00041 inline ~Vec2(); 00042 inline Vec2 (const T &, const T &); 00043 inline Vec2 (const Vec2 &); 00044 00045 inline Vec2<T>& operator = (const Vec2<T>&); 00046 inline Vec2<T>& operator = (const Vec4<T>&); 00047 inline Vec2<T>& operator = (const Vec3<T>&); 00048 00049 inline t_bool operator == (const Vec2<T>&) const; 00050 inline t_bool operator != (const Vec2<T>&) const; 00051 inline Vec2<T> operator+ (const Vec2<T>&) const; 00052 inline Vec2<T> operator* (const Vec2<T>&) const; 00053 inline Vec2<T> operator- (const Vec2<T>&) const; 00054 inline Vec2<T> operator- () const; 00055 00056 inline Vec2<T>& operator*= (const Vec2<T>&); 00057 inline Vec2<T>& operator+= (const Vec2<T>&); 00058 inline Vec2<T>& operator-= (const Vec2<T>&); 00059 00060 inline Vec2<T> operator/ (const T &) const; 00061 inline Vec2<T> operator* (const T &) const; 00062 inline T &operator [] (int i); 00063 inline const T &operator [] (int i) const; 00064 00065 inline T Length() const; 00066 inline T DotProduct (const Vec2 &) const; 00067 inline T CrossProduct (const Vec2 &) const; 00068 inline void Normalize(); 00069 00070 template <typename U> friend Vec2<U> operator* (const U &, const Vec2<U>&); 00071 00072 friend class Matrix2; 00073 00074 T x, y; 00075 }; 00076 00077 template <typename T> 00078 Vec2<T>::Vec2() 00079 { 00080 x = 0; 00081 y = 0; 00082 } 00083 00084 template <typename T> 00085 Vec2<T>::~Vec2() 00086 { 00087 00088 } 00089 00090 //template <typename T> 00091 //Vec2<T>::Vec2(const T& fx) 00092 //{ 00093 // x = fx; 00094 // y = fx; 00095 //} 00096 00097 template <typename T> 00098 Vec2<T>::Vec2 (const T &fx, const T &fy) 00099 { 00100 x = fx; 00101 y = fy; 00102 } 00103 00104 //Vec2<T>::Vec2(t_double fx, t_double fy) 00105 //{ 00106 // x = t_float(fx); 00107 // y = t_float(fy); 00108 //} 00109 // 00110 //Vec2<T>::Vec2(t_int fx, t_int fy) 00111 //{ 00112 // x = t_float(fx); 00113 // y = t_float(fy); 00114 //} 00115 00116 template <typename T> 00117 Vec2<T>::Vec2 (const Vec2<T>& v) 00118 { 00119 x = v.x; 00120 y = v.y; 00121 } 00122 00123 template <typename T> 00124 Vec2<T>& Vec2<T>::operator = (const Vec2<T>& v) 00125 { 00126 x = v.x; 00127 y = v.y; 00128 return *this; 00129 } 00130 00131 template <typename T> 00132 Vec2<T>& Vec2<T>::operator = (const Vec3<T>& v) 00133 { 00134 x = v.x; 00135 y = v.y; 00136 return *this; 00137 } 00138 00139 template <typename T> 00140 Vec2<T>& Vec2<T>::operator = (const Vec4<T>& v) 00141 { 00142 x = v.x; 00143 y = v.y; 00144 return *this; 00145 } 00146 00147 template <typename T> 00148 t_bool Vec2<T>::operator == (const Vec2<T>& v) const 00149 { 00150 if ( (x == v.x) && 00151 (y == v.y) ) 00152 { 00153 return true; 00154 } 00155 00156 return false; 00157 } 00158 00159 template <typename T> 00160 t_bool Vec2<T>::operator != (const Vec2<T>& v) const 00161 { 00162 return ! (*this == v); 00163 } 00164 00165 template <typename T> 00166 Vec2<T> Vec2<T>::operator+ (const Vec2<T>& v) const 00167 { 00168 return Vec2<T> (x + v.x, y + v.y); 00169 } 00170 00171 template <typename T> 00172 Vec2<T> Vec2<T>::operator* (const Vec2<T>& v) const 00173 { 00174 return Vec2<T> (x * v.x, y * v.y); 00175 } 00176 00177 template <typename T> 00178 Vec2<T> Vec2<T>::operator- (const Vec2<T>& v) const 00179 { 00180 return Vec2<T> (x - v.x, y - v.y); 00181 } 00182 00183 template <typename T> 00184 Vec2<T> Vec2<T>::operator- () const 00185 { 00186 return Vec2<T> (-x, -y); 00187 } 00188 00189 template <typename T> 00190 Vec2<T>& Vec2<T>::operator*= (const Vec2<T>& v) 00191 { 00192 x *= v.x; 00193 y *= v.y; 00194 return *this; 00195 } 00196 00197 template <typename T> 00198 Vec2<T>& Vec2<T>::operator+= (const Vec2<T>& v) 00199 { 00200 x += v.x; 00201 y += v.y; 00202 return *this; 00203 } 00204 00205 template <typename T> 00206 Vec2<T>& Vec2<T>::operator-= (const Vec2<T>& v) 00207 { 00208 x -= v.x; 00209 y -= v.y; 00210 return *this; 00211 } 00212 00213 template <typename T> 00214 Vec2<T> Vec2<T>::operator/ (const T &f) const 00215 { 00216 if (f == 0) 00217 { 00218 throw DivisionByZeroException(); 00219 } 00220 00221 return Vec2 (x / f, y / f); 00222 } 00223 00224 template <typename T> 00225 Vec2<T> Vec2<T>::operator* (const T &f) const 00226 { 00227 return Vec2<T> (x * f, y * f); 00228 } 00229 00231 template <typename T> 00232 T &Vec2<T>::operator [] (int i) 00233 { 00234 assert (i >= 0); 00235 assert (i <= 1); 00236 return * (&x + i); 00237 } 00238 00240 template <typename T> 00241 const T &Vec2<T>::operator [] (int i) const 00242 { 00243 assert (i >= 0); 00244 assert (i <= 1); 00245 return * (&x + i); 00246 } 00247 00248 template <typename T> 00249 T Vec2<T>::Length() const 00250 { 00251 return (T) sqrt (x * x + y * y); 00252 } 00253 00254 template <typename T> 00255 T Vec2<T>::DotProduct (const Vec2<T>& v) const 00256 { 00257 return x * v.x + y * v.y; 00258 } 00259 00260 template <typename T> 00261 T Vec2<T>::CrossProduct (const Vec2<T>& v) const 00262 { 00263 T val; 00264 val = x * v.y - y * v.x; 00265 return val; 00266 } 00267 00268 template <typename T> 00269 void Vec2<T>::Normalize() 00270 { 00271 T l; 00272 00273 l = Length(); 00274 00275 if (l == 0) 00276 { 00277 throw DivisionByZeroException(); 00278 } 00279 00280 x = x / l; 00281 y = y / l; 00282 } 00283 00284 template <typename T> 00285 T DotProduct (const Vec2<T>& lhs, const Vec2<T>& rhs) 00286 { 00287 return lhs.x * rhs.x + lhs.y * rhs.y; 00288 } 00289 00290 template <typename T> 00291 T CrossProduct (const Vec2<T>& lhs, const Vec2<T>& rhs) 00292 { 00293 return lhs.x * rhs.y - lhs.y * rhs.x; 00294 } 00295 00296 template <typename U> 00297 Vec2<U> operator* (const U &f, const Vec2<U>& v) 00298 { 00299 return v * f; 00300 } 00301 00302 typedef Vec2<float> Vector2; 00303 typedef Vec2<float> Vertex2; 00304 00305 } 00306 00307 #endif // VECTOR2_H