Back to index

nux  3.0.0
Vector2.h
Go to the documentation of this file.
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 
00047     inline bool operator == (const Vec2<T>&) const;
00048     inline bool operator != (const Vec2<T>&) const;
00049     inline Vec2<T> operator+ (const Vec2<T>&) const;
00050     inline Vec2<T> operator* (const Vec2<T>&) const;
00051     inline Vec2<T> operator- (const Vec2<T>&) const;
00052     inline Vec2<T> operator- () const;
00053 
00054     inline Vec2<T>& operator*= (const Vec2<T>&);
00055     inline Vec2<T>& operator+= (const Vec2<T>&);
00056     inline Vec2<T>& operator-= (const Vec2<T>&);
00057 
00058     inline Vec2<T> operator/ (const T &) const;
00059     inline Vec2<T> operator* (const T &) const;
00060     inline T &operator [] (int i);
00061     inline const T &operator [] (int i) const;
00062 
00063     inline T Length() const;
00064     inline T DotProduct (const Vec2 &) const;
00065     inline T CrossProduct (const Vec2 &) const;
00066     inline void Normalize();
00067 
00068     template <typename U> friend Vec2<U> operator* (const U &, const Vec2<U>&);
00069 
00070     friend class Matrix2;
00071 
00072     T x, y;
00073   };
00074 
00075   template <typename T>
00076   Vec2<T>::Vec2()
00077   {
00078     x = 0;
00079     y = 0;
00080   }
00081 
00082   template <typename T>
00083   Vec2<T>::~Vec2()
00084   {
00085 
00086   }
00087 
00088 //template <typename T>
00089 //Vec2<T>::Vec2(const T& fx)
00090 //{
00091 //    x = fx;
00092 //    y = fx;
00093 //}
00094 
00095   template <typename T>
00096   Vec2<T>::Vec2 (const T &fx, const T &fy)
00097   {
00098     x = fx;
00099     y = fy;
00100   }
00101 
00102 //Vec2<T>::Vec2(double fx, double fy)
00103 //{
00104 //    x = float(fx);
00105 //    y = float(fy);
00106 //}
00107 //
00108 //Vec2<T>::Vec2(int fx, int fy)
00109 //{
00110 //    x = float(fx);
00111 //    y = float(fy);
00112 //}
00113 
00114   template <typename T>
00115   Vec2<T>::Vec2 (const Vec2<T>& v)
00116   {
00117     x = v.x;
00118     y = v.y;
00119   }
00120 
00121   template <typename T>
00122   Vec2<T>& Vec2<T>::operator = (const Vec2<T>& v)
00123   {
00124     x = v.x;
00125     y = v.y;
00126     return *this;
00127   }
00128 
00129   template <typename T>
00130   bool Vec2<T>::operator == (const Vec2<T>& v) const
00131   {
00132     if ( (x == v.x) &&
00133          (y == v.y) )
00134     {
00135       return true;
00136     }
00137 
00138     return false;
00139   }
00140 
00141   template <typename T>
00142   bool Vec2<T>::operator != (const Vec2<T>& v) const
00143   {
00144     return ! (*this == v);
00145   }
00146 
00147   template <typename T>
00148   Vec2<T> Vec2<T>::operator+ (const Vec2<T>& v) const
00149   {
00150     return Vec2<T> (x + v.x, y + v.y);
00151   }
00152 
00153   template <typename T>
00154   Vec2<T> Vec2<T>::operator* (const Vec2<T>& v) const
00155   {
00156     return Vec2<T> (x * v.x, y * v.y);
00157   }
00158 
00159   template <typename T>
00160   Vec2<T> Vec2<T>::operator- (const Vec2<T>& v) const
00161   {
00162     return Vec2<T> (x - v.x, y - v.y);
00163   }
00164 
00165   template <typename T>
00166   Vec2<T> Vec2<T>::operator- () const
00167   {
00168     return Vec2<T> (-x, -y);
00169   }
00170 
00171   template <typename T>
00172   Vec2<T>& Vec2<T>::operator*= (const Vec2<T>& v)
00173   {
00174     x *= v.x;
00175     y *= v.y;
00176     return *this;
00177   }
00178 
00179   template <typename T>
00180   Vec2<T>& Vec2<T>::operator+= (const Vec2<T>& v)
00181   {
00182     x += v.x;
00183     y += v.y;
00184     return *this;
00185   }
00186 
00187   template <typename T>
00188   Vec2<T>& Vec2<T>::operator-= (const Vec2<T>& v)
00189   {
00190     x -= v.x;
00191     y -= v.y;
00192     return *this;
00193   }
00194 
00195   template <typename T>
00196   Vec2<T> Vec2<T>::operator/ (const T &f) const
00197   {
00198     if (f == 0)
00199     {
00200       throw DivisionByZeroException();
00201     }
00202 
00203     return Vec2 (x / f, y / f);
00204   }
00205 
00206   template <typename T>
00207   Vec2<T> Vec2<T>::operator* (const T &f) const
00208   {
00209     return Vec2<T> (x * f, y * f);
00210   }
00211 
00213   template <typename T>
00214   T &Vec2<T>::operator [] (int i)
00215   {
00216     assert (i >= 0);
00217     assert (i <= 1);
00218     return * (&x + i);
00219   }
00220 
00222   template <typename T>
00223   const T &Vec2<T>::operator [] (int i) const
00224   {
00225     assert (i >= 0);
00226     assert (i <= 1);
00227     return * (&x + i);
00228   }
00229 
00230   template <typename T>
00231   T Vec2<T>::Length() const
00232   {
00233     return (T) sqrt (x * x + y * y);
00234   }
00235 
00236   template <typename T>
00237   T Vec2<T>::DotProduct (const Vec2<T>& v) const
00238   {
00239     return x * v.x + y * v.y;
00240   }
00241 
00242   template <typename T>
00243   T Vec2<T>::CrossProduct (const Vec2<T>& v) const
00244   {
00245     T val;
00246     val = x * v.y - y * v.x;
00247     return val;
00248   }
00249 
00250   template <typename T>
00251   void Vec2<T>::Normalize()
00252   {
00253     T l;
00254 
00255     l = Length();
00256 
00257     if (l == 0)
00258     {
00259       throw DivisionByZeroException();
00260     }
00261 
00262     x = x / l;
00263     y = y / l;
00264   }
00265 
00266   template <typename T>
00267   T DotProduct (const Vec2<T>& lhs, const Vec2<T>& rhs)
00268   {
00269     return lhs.x * rhs.x + lhs.y * rhs.y;
00270   }
00271 
00272   template <typename T>
00273   T CrossProduct (const Vec2<T>& lhs, const Vec2<T>& rhs)
00274   {
00275     return    lhs.x * rhs.y - lhs.y * rhs.x;
00276   }
00277 
00278   template <typename U>
00279   Vec2<U> operator* (const U &f, const Vec2<U>& v)
00280   {
00281     return v * f;
00282   }
00283 
00284   typedef Vec2<float> Vector2;
00285   typedef Vec2<float> Vertex2;
00286 
00287 }
00288 
00289 #endif // VECTOR2_H