fs/mth/fsVec2.h

Go to the documentation of this file.
00001 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00002 /*                          _______      ______    ______          __      */
00003 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~    / ____(_)___ / ___ /)  / ____/___  ____/ /__    */
00004 /*      [fsVec2]          / /_  / / __ \\__ \|/  / /   / __ \/ __  / _ \   */
00005 /*       rev. 3          / __/ / / /_/ /__/ /   / /___/ /_/ / /_/ /  __/   */
00006 /*    20th Jan 2007     /_/   /_/ ,___/____/    \____/\____/\__,_/\___/    */
00007 /*     [x] stable              /_/ (c) 2006-7 Filip STOKLAS (FipS)         */
00008 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~       http://HOLE.4FipS.com/fips_code.php          */
00009 /*                                                                         */
00010 /* This code is free for personal and commercial use. You may redistribute */
00011 /* it by any means. If you use the code for your own projects please give  */
00012 /* me credit. Please send a bug report. Don't alter or remove this header! */
00013 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00014 #ifndef FS_MTH_FSVEC2_H
00015 #define FS_MTH_FSVEC2_H
00016 //---------------------------------------------------------------------------
00017 #if defined(FS_INCLUDE_USERDEFS) // inject user definition file on request
00018 #   include <fs/fsUserDefs.h>
00019 #endif
00020 //---------------------------------------------------------------------------
00021 namespace fs { namespace mth {
00022 //---------------------------------------------------------------------------
00024 template <typename T>
00025 class Vec2_T
00026 {
00027  public:
00028  
00029     // --- CONSTRUCTION ---
00030     
00031     Vec2_T();
00032     Vec2_T(T x, T y);
00033     Vec2_T(const Vec2_T &RHS);
00034     
00035     // --- ASSIGMENT ---
00036     
00037     Vec2_T & operator = (const Vec2_T &RHS);
00038     
00039     // ---
00040     
00041     bool operator == (const Vec2_T &RHS) const;
00042     bool operator != (const Vec2_T &RHS) const;
00043                  
00044     // --- OPERATIONS ---
00045     
00046     Vec2_T operator - () const;
00047     Vec2_T operator + (const Vec2_T &RHS) const;
00048     Vec2_T & operator += (const Vec2_T &RHS);
00049     Vec2_T operator - (const Vec2_T &RHS) const;
00050     Vec2_T & operator -= (const Vec2_T &RHS);
00051     T operator * (const Vec2_T &RHS) const; // dot
00052     Vec2_T operator * (T t) const;
00053     // friend Vec2_T operator * (T t, const Vec2_T &RHS);
00054     Vec2_T & operator *= (T t);
00055     
00057     T Len() const;
00059     T Len2() const;
00061     T Norm();
00062  
00063  public: // direct access is allowed
00064  
00065     T x, y;
00066 };
00067 //---------------------------------------------------------------------------
00068 template <typename T>
00069 inline Vec2_T<T>::Vec2_T() // does nothing
00070 {
00071 }
00072 //---------------------------------------------------------------------------
00073 template <typename T>
00074 inline Vec2_T<T>::Vec2_T(T x, T y):
00075 x(x),
00076 y(y)
00077 {
00078 }
00079 //---------------------------------------------------------------------------
00080 template <typename T>
00081 inline Vec2_T<T>::Vec2_T(const Vec2_T &RHS):
00082 x(RHS.x),
00083 y(RHS.y)
00084 {
00085 }
00086 //---------------------------------------------------------------------------
00087 template <typename T>
00088 inline Vec2_T<T> & Vec2_T<T>::operator = (const Vec2_T &RHS)
00089 {
00090     x = RHS.x;
00091     y = RHS.y;
00092     return *this;
00093 }
00094 //---------------------------------------------------------------------------
00095 template <typename T>
00096 inline bool Vec2_T<T>::operator == (const Vec2_T &RHS) const
00097 {
00098     return x == RHS.x && y == RHS.y;
00099 }
00100 //---------------------------------------------------------------------------
00101 template <typename T>
00102 inline bool Vec2_T<T>::operator != (const Vec2_T &RHS) const
00103 {
00104     return x != RHS.x || y != RHS.y;
00105 }
00106 //---------------------------------------------------------------------------
00107 template <typename T>
00108 inline Vec2_T<T> Vec2_T<T>::operator - () const
00109 {
00110     return Vec2_T(-x, -y);
00111 }
00112 //---------------------------------------------------------------------------
00113 template <typename T>
00114 inline Vec2_T<T> Vec2_T<T>::operator + (const Vec2_T &RHS) const
00115 {
00116     return Vec2_T(x + RHS.x, y + RHS.y);
00117 }
00118 //---------------------------------------------------------------------------
00119 template <typename T>
00120 inline Vec2_T<T> & Vec2_T<T>::operator += (const Vec2_T &RHS)
00121 {
00122     x += RHS.x;
00123     y += RHS.y;
00124     return *this;
00125 }
00126 //---------------------------------------------------------------------------
00127 template <typename T>
00128 inline Vec2_T<T> Vec2_T<T>::operator - (const Vec2_T &RHS) const
00129 {
00130     return Vec2_T(x - RHS.x, y - RHS.y);
00131 }
00132 //---------------------------------------------------------------------------
00133 template <typename T>
00134 inline Vec2_T<T> & Vec2_T<T>::operator -= (const Vec2_T &RHS)
00135 {
00136     x -= RHS.x;
00137     y -= RHS.y;
00138     return *this;
00139 }
00140 //---------------------------------------------------------------------------
00141 template <typename T>
00142 inline T Vec2_T<T>::operator * (const Vec2_T &RHS) const
00143 {
00144     return x * RHS.x + y * RHS.y;
00145 }
00146 //---------------------------------------------------------------------------
00147 template <typename T>
00148 inline Vec2_T<T> Vec2_T<T>::operator * (T t) const
00149 {
00150     return Vec2_T(t * x, t * y);
00151 }
00152 //---------------------------------------------------------------------------
00153 template <typename T>
00154 inline Vec2_T<T> operator * (T t, const Vec2_T<T> &RHS)
00155 {
00156     return Vec2_T<T>(t * RHS.x, t * RHS.y);
00157 }
00158 //---------------------------------------------------------------------------
00159 template <typename T>
00160 inline Vec2_T<T> & Vec2_T<T>::operator *= (T t)
00161 {
00162     x *= t;
00163     y *= t;
00164     return *this;
00165 }
00166 //---------------------------------------------------------------------------
00167 template <typename T>
00168 inline T Vec2_T<T>::Len() const
00169 {
00170     return Sqrt(Len2());
00171 }
00172 //---------------------------------------------------------------------------
00173 template <typename T>
00174 inline T Vec2_T<T>::Len2() const
00175 {
00176     return (*this) * (*this);
00177 }
00178 //---------------------------------------------------------------------------
00179 template <typename T>
00180 inline T Vec2_T<T>::Norm()
00181 {
00182     T l2 = Len2();
00183     T li = SqrtInv(l2);
00184     
00185     x *= li;
00186     y *= li;
00187     
00188     return li * l2; // returns length
00189 }
00190 //---------------------------------------------------------------------------
00191 }} // namespace fs::mth
00192 //---------------------------------------------------------------------------
00193 #endif // FS_MTH_FSVEC2_H

FipS' Code (Thu Feb 15 22:43:35 2007) - (c) Filip STOKLAS (FipS) - [ www ] [ Guest Book ]