00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __Matrix2x2_h__
00010 #define __Matrix2x2_h__
00011
00012 #include "Vector2.h"
00013
00014
00015 template <class V> class Matrix2x2 {
00016 public:
00017
00018 Vector2<V> c[2];
00019
00020
00021 Matrix2x2<V>()
00022 {
00023 c[0]=Vector2<V>(1,0);
00024 c[1]=Vector2<V>(0,1);
00025 }
00026
00027
00028
00029
00030
00031
00032 Matrix2x2<V>(const Vector2<V>& c0,const Vector2<V>& c1)
00033 {
00034 c[0]=c0;
00035 c[1]=c1;
00036 }
00037
00038
00039
00040
00041
00042
00043 Matrix2x2<V>& operator=(const Matrix2x2<V>& other)
00044 {
00045 c[0] = other.c[0];
00046 c[1] = other.c[1];
00047 return *this;
00048 }
00049
00050
00051
00052
00053
00054 Matrix2x2<V>(const Matrix2x2<V>& other)
00055 {
00056 *this = other;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065 Vector2<V>& operator[](int i)
00066 {
00067 return c[i];
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 Vector2<V> operator*(const Vector2<V>& vector) const
00077 {
00078 return (c[0]*vector.x + c[1]*vector.y);
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 Matrix2x2<V> operator*(const Matrix2x2<V>& other) const
00088 {
00089 Matrix2x2<V> returnMatrix;
00090 returnMatrix.c[0].x = c[0].x * other.c[0].x + c[1].x * other.c[0].y;
00091 returnMatrix.c[0].y = c[0].y * other.c[0].x + c[1].y * other.c[0].y;
00092 returnMatrix.c[1].x = c[0].x * other.c[1].x + c[1].x * other.c[1].y;
00093 returnMatrix.c[1].y = c[0].y * other.c[1].x + c[1].y * other.c[1].y;
00094 return returnMatrix;
00095 }
00096
00097
00098
00099
00100
00101
00102 Matrix2x2<V> operator*=(const Matrix2x2<V>& other)
00103 {
00104 return *this = *this * other;
00105 }
00106
00107
00108
00109
00110
00111
00112 Matrix2x2<V>& operator*=(const V& factor)
00113 {
00114 c[0] *= factor;
00115 c[1] *= factor;
00116 return *this;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 Matrix2x2<V>& operator/=(const V& factor)
00126 {
00127 return *this *= 1 / factor;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136 Matrix2x2<V> operator*(const V& factor) const
00137 {
00138 return Matrix2x2<V>(*this) *= factor;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 Matrix2x2<V> operator/(const V& factor) const
00148 {
00149 return Matrix2x2<V>(*this) /= factor;
00150 }
00151
00152
00153
00154 Matrix2x2<V> operator+(const Matrix2x2<V>& other) const
00155 {
00156 return Matrix2x2<V>(Vector2<V>(c[0].x + other.c[0].x,c[0].y + other.c[0].y),
00157 Vector2<V>(c[1].x + other.c[1].x,c[1].y + other.c[1].y));
00158 }
00159
00160
00161
00162 Matrix2x2<V> operator-(const Matrix2x2<V>& other) const
00163 {
00164 return Matrix2x2<V>(Vector2<V>(c[0].x - other.c[0].x,c[0].y - other.c[0].y),
00165 Vector2<V>(c[1].x - other.c[1].x,c[1].y - other.c[1].y));
00166 }
00167
00168
00169 Matrix2x2<V> invert()
00170 {
00171 double globFactor;
00172 if ((c[1].y * c[0].x - c[0].y * c[1].x) != 0.0)
00173 globFactor = 1 / (c[1].y * c[0].x - c[0].y * c[1].x);
00174 else globFactor = 0.00000000000000001;
00175 return Matrix2x2<V>(Vector2<V>(globFactor * c[1].y,-globFactor * c[0].y),
00176 Vector2<V>(-globFactor * c[1].x,globFactor * c[0].x));
00177 }
00178
00179
00180
00181
00182
00183
00184
00185 bool operator==(const Matrix2x2<V>& other) const
00186 {
00187 return (c[0]==other.c[0] && c[1]==other.c[1]);
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 bool operator!=(const Matrix2x2<V>& other) const
00197 {
00198 return !(*this == other);
00199 }
00200
00201
00202
00203
00204
00205 Matrix2x2<V> transpose() const
00206 {
00207 return Matrix2x2<V>(Vector2<V>(c[0].x,c[1].x),
00208 Vector2<V>(c[0].y,c[1].y));
00209 }
00210
00211
00212
00213
00214
00215
00216 V det() const
00217 {
00218 return c[0].x * c[1].y - c[1].x * c[0].y;
00219 }
00220 };
00221
00222
00223
00224
00225
00226
00227
00228 template <class V> In& operator>>(In& stream, Matrix2x2<V>& matrix2x2);
00229
00230
00231
00232
00233
00234
00235
00236 template <class V> Out& operator<<(Out& stream, const Matrix2x2<V>& matrix2x2);
00237
00238
00239
00240 #endif // __Matrix2x2_h__