00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef GENLIB2_VECTOR2_INCLUDED
00022 #define GENLIB2_VECTOR2_INCLUDED
00023
00024 #include <iostream>
00025 #include "matrix.h"
00026
00027
00028 using namespace std;
00029
00030 namespace GenLib2
00031 {
00032
00033 template<class T>
00034 class Vector2
00035 {
00036 public:
00037
00038 Vector2() { x=0; y=0; }
00039 Vector2(T s) { init(s); }
00040 Vector2(T x, T y) { init(x,y); }
00041 Vector2(const Vector2& that) { init(that.x,that.y); }
00042
00043 Vector2& init(T X, T Y) { x=X; y=Y; return *this; }
00044 Vector2& init(T s) { x=s; y=s; return *this; }
00045
00046 Vector2& operator=(const Vector2& a) {
00047 return init((x=a.x),(y=a.y));
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 Vector2& operator+=(const Vector2& a) {
00059 x += a.x; y += a.y;
00060 return *this;
00061 }
00062
00063 Vector2& operator-=(const Vector2& a) {
00064 x -= a.x; y -= a.y;
00065 return *this;
00066 }
00067
00068 Vector2& operator*=(const Vector2& a) {
00069 x *= a.x; y *= a.y;
00070 return *this;
00071 }
00072
00073 Vector2& operator/=(const Vector2& a) {
00074 x /= a.x; y /= a.y;
00075 return *this;
00076 }
00077
00078
00079 Vector2& prod(const Matrix<T>& A, const Vector2& b) {
00080 assert(A.rows() == 2);
00081 assert(A.cols() == 2);
00082
00083 x = A(0,0) * b.x + A(0,1) * b.y;
00084 y = A(1,0) * b.x + A(1,1) * b.y;
00085
00086 return *this;
00087 }
00088
00089 Vector2& prod_trn(const Matrix<T>& A, const Vector2& b) {
00090 assert(A.rows() == 2);
00091 assert(A.cols() == 2);
00092
00093 x = A(0,0) * b.x + A(1,0) * b.y;
00094 y = A(0,1) * b.x + A(1,1) * b.y;
00095
00096 return *this;
00097 }
00098
00099
00100
00101 T x, y;
00102 };
00103
00104
00105
00106
00107
00108
00109 template<class T> inline
00110 Vector2<T> operator+(const Vector2<T>& a, const Vector2<T>& b)
00111 {
00112 Vector2<T> tmp(a.x + b.x, a.y + b.y);
00113 return tmp;
00114 }
00115
00116 template<class T> inline
00117 Vector2<T> operator-(const Vector2<T>& a, const Vector2<T>& b)
00118 {
00119 Vector2<T> tmp(a.x - b.x, a.y - b.y);
00120 return tmp;
00121 }
00122
00123 template<class T> inline
00124 Vector2<T> operator*(const Vector2<T>& a, const Vector2<T>& b)
00125 {
00126 Vector2<T> tmp(a.x * b.x, a.y * b.y);
00127 return tmp;
00128 }
00129
00130 template<class T> inline
00131 Vector2<T> operator/(const Vector2<T>& a, const Vector2<T>& b)
00132 {
00133 Vector2<T> tmp(a.x / b.x, a.y / b.y);
00134 return tmp;
00135 }
00136
00137
00138
00139
00140 template<class T> inline
00141 Vector2<T> operator*(const Vector2<T>& a, T s)
00142 {
00143 Vector2<T> tmp(a.x * s, a.y * s);
00144 return tmp;
00145 }
00146
00147 template<class T> inline
00148 Vector2<T> operator/(const Vector2<T>& a, T s)
00149 {
00150 Vector2<T> tmp(a.x / s, a.y / s);
00151 return tmp;
00152 }
00153
00154
00155
00156
00157 template<class T> inline
00158 bool operator==(const Vector2<T>& a, const Vector2<T>& b)
00159 {
00160 return (a.x == b.x && a.y == b.y);
00161 }
00162
00163 template<class T> inline
00164 bool operator!=(const Vector2<T>& a, const Vector2<T>& b)
00165 {
00166 return (a.x != b.x || a.y != b.y);
00167 }
00168
00169
00170
00171
00172 template<class T> inline
00173 istream& operator>>(istream& is, Vector2<T>& a)
00174 {
00175 T x = 0, y = 0;
00176 char c = 0;
00177
00178 is >> c;
00179 if (c == '(')
00180 {
00181 is >> x >> c;
00182 if (c == ',') is >> y >> c;
00183 if (c != ')') is.clear(ios::badbit);
00184 }
00185 else
00186 {
00187 is.putback(c);
00188 is >> x >> y;
00189 }
00190
00191 if (is) a = Vector2<T>(x, y);
00192 return is;
00193 }
00194
00195
00196
00197
00198 template<class T> inline
00199 ostream& operator<<(ostream& os, const Vector2<T>& a)
00200 {
00201 return os << "( " << a.x << ", " << a.y << ")";
00202 }
00203
00204
00205
00206
00207
00208
00209 template<class T> inline
00210 T abs2(const Vector2<T>& a)
00211 {
00212 return (a.x * a.x + a.y * a.y);
00213 }
00214
00215 template<class T> inline
00216 T abs(const Vector2<T>& a)
00217 {
00218 return sqrt(abs2(a));
00219 }
00220
00221
00222
00223
00224 template<class T> inline
00225 T dot(const Vector2<T>& a, const Vector2<T>& b)
00226 {
00227 return (a.x * b.x + a.y * b.y);
00228 }
00229
00230
00231
00232
00233 template<class T>
00234 Vector2<T> operator*(const Matrix<T>& A, const Vector2<T>& b)
00235 {
00236 assert(A.rows() == 2);
00237 assert(A.cols() == 2);
00238
00239 Vector2<T> c;
00240 c.prod(A, b);
00241
00242
00243
00244 return c;
00245 }
00246
00247 }
00248
00249 #endif
00250