00001 #ifndef GUI_VECTOR2_HPP 00002 #define GUI_VECTOR2_HPP 00003 00004 #include <cmath> 00005 #include <iosfwd> 00006 00007 namespace gui 00008 { 00009 template<class T> 00010 class vector2 00011 { 00012 public : 00013 00014 vector2() 00015 { 00016 } 00017 00018 vector2(T mX, T mY) : x(mX), y(mY) 00019 { 00020 } 00021 00022 void set(T mX, T mY) 00023 { 00024 x = mX; y = mY; 00025 } 00026 00027 T get_norm() const 00028 { 00029 return T(sqrt(x*x + y*y)); 00030 } 00031 00032 T get_norm_squared() const 00033 { 00034 return x*x + y*y; 00035 } 00036 00037 void normalize() 00038 { 00039 T mNorm = T(sqrt(x*x + y*y)); 00040 x = x/mNorm; 00041 y = y/mNorm; 00042 } 00043 00044 vector2 get_unit() const 00045 { 00046 T mNorm = T(sqrt(x*x + y*y)); 00047 return vector2(x/mNorm, y/mNorm); 00048 } 00049 00050 void rotate(const float& fAngle) 00051 { 00052 vector2 p; 00053 00054 double ca = cos(fAngle), sa = sin(fAngle); 00055 00056 p.x = x*ca - y*sa; 00057 p.y = x*sa + y*ca; 00058 00059 x = p.x; 00060 y = p.y; 00061 } 00062 00063 vector2 get_rotated(const float& fAngle) const 00064 { 00065 double ca = cos(fAngle), sa = sin(fAngle); 00066 return vector2(x*ca - y*sa, x*sa + y*ca); 00067 } 00068 00069 void scale(const vector2& v) 00070 { 00071 x *= v.x; 00072 y *= v.y; 00073 } 00074 00075 vector2 get_scale(const vector2& v) const 00076 { 00077 return vector2(x*v.x, y*v.y); 00078 } 00079 00080 vector2 operator + (const vector2& v) const 00081 { 00082 return vector2(x + v.x, y + v.y); 00083 } 00084 void operator += (const vector2& v) 00085 { 00086 x += v.x; y += v.y; 00087 } 00088 00089 vector2 operator - () const 00090 { 00091 return vector2(-x, -y); 00092 } 00093 00094 vector2 operator - (const vector2& v) const 00095 { 00096 return vector2(x - v.x, y - v.y); 00097 } 00098 void operator -= (const vector2& v) 00099 { 00100 x -= v.x; y -= v.y; 00101 } 00102 00103 bool operator == (const vector2& v) const 00104 { 00105 return (x == v.x) && (y == v.y); 00106 } 00107 bool operator != (const vector2& v) const 00108 { 00109 return (x != v.x) || (y != v.y); 00110 } 00111 00112 vector2 operator * (T mValue) const 00113 { 00114 return vector2(x*mValue, y*mValue); 00115 } 00116 00117 void operator *= (T mValue) 00118 { 00119 x *= mValue; y *= mValue; 00120 } 00121 00122 vector2 operator / (T mValue) const 00123 { 00124 return vector2(x/mValue, y/mValue); 00125 } 00126 00127 void operator /= (T mValue) 00128 { 00129 x /= mValue; y /= mValue; 00130 } 00131 00132 T operator * (const vector2& v) const 00133 { 00134 return x*v.x + y*v.y; 00135 } 00136 00137 static const vector2 ZERO; 00138 static const vector2 UNIT; 00139 static const vector2 X; 00140 static const vector2 Y; 00141 00142 T x, y; 00143 }; 00144 00145 template<class T> 00146 const vector2<T> vector2<T>::ZERO(0, 0); 00147 00148 template<class T> 00149 const vector2<T> vector2<T>::UNIT(1, 1); 00150 00151 template<class T> 00152 const vector2<T> vector2<T>::X(1, 0); 00153 00154 template<class T> 00155 const vector2<T> vector2<T>::Y(0, 1); 00156 00157 typedef vector2<float> vector2f; 00158 00159 template<class T> 00160 vector2<T> operator * (T mValue, const vector2<T>& mV) 00161 { 00162 return vector2<T>(mV.x*mValue, mV.y*mValue); 00163 } 00164 00165 template<class T> 00166 std::ostream& operator << (std::ostream& mStream, const vector2<T>& mV) 00167 { 00168 return mStream << mV.x << ", " << mV.y; 00169 } 00170 } 00171 00172 #endif