vec2.hpp

Go to the documentation of this file.
00001 /*
00002     RenderStack  Support library for OpenGL 3+
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00027 #ifndef VEC2_HPP
00028 #define VEC2_HPP
00029 
00030 #include <cmath>
00031 
00032 namespace renderstack {
00033 
00035 class vec2
00036 {
00037 public:
00038     vec2(float _x = 0, float _y = 0)
00039     :   x(_x)
00040     ,   y(_y)
00041     {
00042     }
00043 
00044     vec2 operator+(vec2 const &v) const 
00045     {
00046         return vec2(x + v.x, y + v.y);
00047     }
00048 
00049     vec2 &operator+=(vec2 const &v)
00050     {
00051         x += v.x;
00052         y += v.y;
00053         return *this;
00054     }
00055 
00056     vec2 operator-(vec2 const &v) const
00057     {
00058         return vec2(x - v.x, y - v.y);
00059     }
00060 
00061     vec2 &operator-=(vec2 const &v) 
00062     {
00063         x -= v.x;
00064         y -= v.y;
00065         return *this;
00066     }
00067 
00068     bool operator==(vec2 const &v) const
00069     {
00070         return 
00071             (x == v.x) && 
00072             (y == v.y);
00073     }
00074     vec2 operator*(float f) const
00075     {
00076         return vec2(f * x, f * y);
00077     }
00078 
00079     vec2 &operator*=(float f) 
00080     {
00081         x *= f;
00082         y *= f;
00083         return *this;
00084     }
00085 
00086     vec2 operator/(float f) const
00087     {
00088         float inv = 1.0f / f;
00089         return vec2(x * inv, y * inv);
00090     }
00091 
00092     vec2 &operator/=(float f)
00093     {
00094         float inv = 1.f / f;
00095         x *= inv; 
00096         y *= inv; 
00097         return *this;
00098     }
00099 
00100     vec2 operator-() const 
00101     {
00102         return vec2(-x, -y);
00103     }
00104 
00105     float operator[](int i) const 
00106     {
00107         return (&x)[i];
00108     }
00109 
00110     float &operator[](int i)
00111     {
00112         return (&x)[i];
00113     }
00114 
00115     float length_squared() const { return x * x + y * y; }
00116     float length() const { return std::sqrt(length_squared()); }
00117 
00118     float x;
00119     float y;
00120 };
00121 
00122 inline float dot(vec2 const &v1, vec2 const &v2)
00123 {
00124     return v1.x * v2.x + v1.y * v2.y;
00125 }
00126 
00127 inline vec2 normalize(vec2 const &v)
00128 {
00129     return v / v.length();
00130 }
00131 
00132 }
00133 
00134 #endif 
00135 
Generated on Sun Apr 11 12:23:09 2010 for RenderStack by  doxygen 1.6.3