TCMathCoord2D.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCMathCoord2D.h 801 2008-01-23 19:46:25Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TCCOORD_2D_H_
00037 #define _TCCOORD_2D_H_
00038 
00039 #include "TCMathUtil.h"
00040 #include "TCStream.h"
00041 #include "TCUtil.h"
00042 
00043 #include <cmath>
00044 
00045 namespace TC
00046 {
00047 namespace Math
00048 {
00064    template <class T>
00065    class Coord2D
00066    {
00067    public:
00072       Coord2D(T vIn=(T)0)
00073       {
00074          m_data[0]=vIn;
00075          m_data[1]=vIn;
00076       }
00082       Coord2D(const T xIn, const T yIn)
00083       {
00084          m_data[0]=xIn;
00085          m_data[1]=yIn;
00086       }
00091       Coord2D(const Coord2D<T>& a)
00092       {
00093          m_data[0]=a.m_data[0];
00094          m_data[1]=a.m_data[1];
00095       } 
00096       // --------------------------------------------------
00097 
00098       // --------------------------------------------------
00099       // accesing values
00100       const T& operator[] (sint32 pos) const { return m_data[pos];}
00101       // setting values
00102       T& operator[] (sint32 pos) { return m_data[pos];}
00103       // --------------------------------------------------
00104 
00105       // --------------------------------------------------
00106       // Assignment operators
00107       Coord2D<T>& operator=(const Coord2D<T>& a)  { m_data[0]=a.m_data[0]; m_data[1]=a.m_data[1]; return *this;}
00108       Coord2D<T>& operator=(const T &vIn)         { m_data[0]=m_data[1]=vIn; return *this;}
00109       Coord2D<T>& operator+=(const Coord2D<T>& b) { m_data[0]+=b.m_data[0]; m_data[1]+=b.m_data[1]; return *this;}
00110       Coord2D<T>& operator+=(const T &b)          { m_data[0]+=b; m_data[1]+=b; return *this;}
00111       Coord2D<T>& operator-=(const Coord2D<T>& b) { m_data[0]-=b.m_data[0]; m_data[1]-=b.m_data[1]; return *this;}
00112       Coord2D<T>& operator-=(const T &b)          { m_data[0]-=b; m_data[1]-=b; return *this;}
00113       Coord2D<T>& operator*=(const T &vIn)        { m_data[0]*=vIn; m_data[1]*=vIn; return *this;}
00114       Coord2D<T>& operator/=(const T &vIn)        { m_data[0]/=vIn; m_data[1]/=vIn; return *this;}
00115       // --------------------------------------------------
00116 
00118       operator T*() {return m_data;}
00120       operator const T*() const {return m_data;}
00121 
00126       T* GetPtr()       {return m_data;}
00131       const T* GetPtr() const {return m_data;}
00135       double Length2() const { return (m_data[0]*m_data[0] + m_data[1]*m_data[1]);}
00139       double Length () const { return std::sqrt(Length2());}
00144       void Normalize()                                                    
00145       {
00146          double len = Length2();
00147          if (len > 0.0)
00148          {
00149             len = std::sqrt(len);
00150             m_data[0] = (T)((double)(m_data[0])/len);
00151             m_data[1] = (T)((double)(m_data[1])/len);
00152          }
00153       }
00157       T MaxValue() const
00158       {
00159          return Util::Max(m_data[0], m_data[1]);
00160       }
00161 
00162       typedef T DataType;
00163       enum
00164       {
00165          NUM_COMPONENTS = 2
00166       };
00167    private:
00168       T m_data[NUM_COMPONENTS];
00169    };
00170 
00171    template <class T>
00172    inline const Coord2D<T> operator*(const Coord2D<T> &v, const T &vIn)
00173    {
00174       return Coord2D<T>(v[0]*vIn, v[1]*vIn);
00175    }
00176 
00177    template <class T>
00178    inline const Coord2D<T> operator*(const T &vIn, const Coord2D<T> &v)
00179    {
00180       return Coord2D<T>(v[0]*vIn, v[1]*vIn);
00181    }
00182 
00183    template <class T>
00184    inline const T operator*(const Coord2D<T> &a, const Coord2D<T> &b)
00185    {
00186       return (a[0] * b[0] + a[1] * b[1]);
00187    }
00188 
00189    template <class T>
00190    inline const Coord2D<T> operator/(const Coord2D<T> &v, const T &vIn)
00191    {
00192       return Coord2D<T>(v[0]/vIn, v[1]/vIn);
00193    }
00194 
00195    template <class T>
00196    inline const Coord2D<T> operator/(const T &vIn, const Coord2D<T> &v)
00197    {
00198       return Coord2D<T>(v[0]/vIn, v[1]/vIn);
00199    }
00200 
00201    template <class T>
00202    inline const Coord2D<T> operator+(const Coord2D<T>& v, const Coord2D<T>& b)
00203    {
00204       return Coord2D<T>(v[0]+b[0], v[1]+b[1]);
00205    }
00206 
00207    template <class T>
00208    inline const Coord2D<T> operator+(const Coord2D<T>& v, const T& b)
00209    {
00210       return Coord2D<T>(v[0]+b, v[1]+b);
00211    }
00212 
00213    template <class T>
00214    inline const Coord2D<T> operator-(const Coord2D<T>& v, const Coord2D<T>& b)
00215    {
00216       return Coord2D<T>(v[0]-b[0], v[1]-b[1]);
00217    }
00218 
00219    template <class T>
00220    inline const Coord2D<T> operator-(const Coord2D<T>& v, const T& b)
00221    {
00222       return Coord2D<T>(v[0]-b, v[1]-b);
00223    }
00224 
00225    template <class T>
00226    inline const Coord2D<T> operator-(const Coord2D<T>& v)
00227    {
00228       return Coord2D<T>(-v[0], -v[1]);
00229    }
00230 
00231    // ------------------------------------------------------------------------------------------------------------
00232    // operators for coordinates checking
00233    // ------------------------------------------------------------------------------------------------------------
00234    template <class T>
00235    inline bool operator>(const Coord2D<T>&a, const Coord2D<T>&b)
00236    {
00237       if (a[0] > b[0]) return true;
00238       if (a[0] < b[0]) return false;
00239       if (a[1] > b[1]) return true;
00240       if (a[1] < b[1]) return false;
00241       return 0;
00242    }
00243 
00244    template <class T>
00245    inline bool operator<(const Coord2D<T>&a, const Coord2D<T>&b)
00246    {
00247       if (a[0] < b[0]) return true;
00248       if (a[0] > b[0]) return false;
00249       if (a[1] < b[1]) return true;
00250       if (a[1] > b[1]) return false;
00251       return false;
00252    }
00253 
00254    template <class T>
00255    inline bool operator==(const Coord2D<T>&a, const Coord2D<T>&b)
00256    {
00257       return (a[0]==b[0] && a[1]==b[1]);
00258    }
00259 
00260    template <class T>
00261    inline bool operator!=(const Coord2D<T>&a, const Coord2D<T>&b)
00262    {
00263       return (a[0]!=b[0] || a[1]!=b[1]);
00264    }
00265 
00266    template <class T>
00267    inline bool operator>=(const Coord2D<T>&a, const Coord2D<T>&b)
00268    {
00269       return (a==b || a>b);
00270    }
00271 
00272    template <class T>
00273    inline bool operator<=(const Coord2D<T>&a, const Coord2D<T>&b)
00274    {
00275       return (a==b || a<b);
00276    }
00277 
00284    template <class T>
00285    inline StreamPtr operator>>(StreamPtr stream, Coord2D<T>& coord)
00286    {
00287       return stream >> coord[0] >> coord[1];
00288    }
00295    template <class T>
00296    inline StreamPtr operator<<(StreamPtr stream, const Coord2D<T>& coord)
00297    {
00298       return stream << coord[0] << space << coord[1];
00299    }
00300 
00305 } // namespace Math
00306 } // namespace TC
00307 
00308 #endif // _TCCOORD_2D_H_

Copyright (c) Thomas Goessler 2003 - 2008