philsupertramp/game-math
Loading...
Searching...
No Matches
mat2.h
Go to the documentation of this file.
1#pragma once
2
3#include "../vec/vec2.h"
4
9template<class T>
10struct mat2 {
11public:
13 T values[2][2];
14
18 mat2() {
19 // clang-format off
20 values[0][0] = static_cast<T>(0); values[0][1] = static_cast<T>(0);
21 values[1][0] = static_cast<T>(0); values[1][1] = static_cast<T>(0); // clang-format on
22 }
23
31 mat2(const T& _a, const T& _b, const T& _c, const T& _d) {
32 // clang-format off
33 values[0][0] = _a; values[0][1] = _b;
34 values[1][0] = _c; values[1][1] = _d; // clang-format on
35 }
36
42 mat2(const vec2<T>& A, const vec2<T>& B) {
43 // clang-format off
44 values[0][0] = A.x; values[0][1] = B.x;
45 values[1][0] = A.y; values[1][1] = B.y; // clang-format on
46 }
47
51 ~mat2() = default;
52
57 static inline mat2<T> Unit() {
58 return mat2<T>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(1));
59 }
60
65 inline mat2<T> Transpose() { return mat2<T>(values[0][0], values[1][0], values[0][1], values[1][1]); }
66
71 inline bool IsSymmetric() { return values[1][0] == values[0][1]; }
76 inline float Determinant() { return values[0][0] * values[1][1] - values[0][1] * values[1][0]; }
77
78 inline mat2<T> Inverse() const {
79 return mat2<T>(values[1][1], -values[0][1], -values[1][0], values[0][0])
80 * static_cast<T>(1 / (values[0][0] * values[1][1] - values[0][1] * values[1][0]));
81 }
82
89 friend mat2<T> operator+(mat2<T> lhs, const mat2<T>& rhs) { return lhs += rhs; }
90
97 friend mat2<T> operator-(mat2<T> lhs, const mat2<T>& rhs) { return lhs -= rhs; }
98
105 friend mat2<T> operator*(mat2<T> lhs, const T& rhs) { return lhs *= rhs; }
106
113 friend vec2<T> operator*(mat2<T> lhs, const vec2<T>& rhs) {
114 return vec2<T>(lhs[0][0] * rhs.x + lhs[0][1] * rhs.y, lhs[1][0] * rhs.x + lhs[1][1] * rhs.y);
115 }
116
123 friend mat2<T> operator*(mat2<T> lhs, const mat2<T>& rhs) { return lhs *= rhs; }
124
131 friend mat2<T> operator/(mat2<T> lhs, const T& rhs) { return lhs /= rhs; }
132
133 /* compound assignment */
140 // clang-format off
141 values[0][0] += rhs[0][0]; values[0][1] += rhs[0][1];
142 values[1][0] += rhs[1][0]; values[1][1] += rhs[1][1]; // clang-format on
143 return *this;
144 }
145
152 // clang-format off
153 values[0][0] -= rhs[0][0]; values[0][1] -= rhs[0][1];
154 values[1][0] -= rhs[1][0]; values[1][1] -= rhs[1][1]; // clang-format on
155 return *this;
156 }
157
164 // clang-format off
165 T _a = values[0][0], _b = values[0][1],
166 _c = values[1][0], _d = values[1][1];
167 values[0][0] = _a * rhs[0][0] + _b * rhs[1][0]; values[0][1] = _a * rhs[0][1] + _b * rhs[1][1];
168 values[1][0] = _c * rhs[0][0] + _d * rhs[1][0]; values[1][1] = _c * rhs[0][1] + _d * rhs[1][1]; // clang-format on
169 return *this;
170 }
171
177 mat2<T>& operator*=(const T& rhs) {
178 // clang-format off
179 values[0][0] *= rhs; values[0][1] *= rhs;
180 values[1][0] *= rhs; values[1][1] *= rhs; // clang-format on
181 return *this;
182 }
183
189 mat2<T>& operator/=(const T& rhs) {
190 // clang-format off
191 values[0][0] /= rhs; values[0][1] /= rhs;
192 values[1][0] /= rhs; values[1][1] /= rhs; // clang-format on
193 return *this;
194 }
195
196 /* Member access */
202 T* operator[](const int& index) { return values[index]; }
208 const T* operator[](const int& index) const { return values[index]; }
209
210 /* stream operators */
216 template<class U>
217 friend std::ostream& operator<<(std::ostream&, const mat2<U>&);
218};
219
220template<class U>
221std::ostream& operator<<(std::ostream& out, const mat2<U>& mat) {
222 out.precision(17);
223 out << "[\n\t" << mat[0][0] << ", " << mat[0][1] << ";\n\t" << mat[1][0] << ", " << mat[1][1] << "\n]\n";
224 return out;
225}
226
227
std::ostream & operator<<(std::ostream &out, const mat2< U > &mat)
Definition: mat2.h:221
Definition: mat2.h:10
T values[2][2]
loosely packed data
Definition: mat2.h:13
const T * operator[](const int &index) const
Definition: mat2.h:208
~mat2()=default
float Determinant()
Definition: mat2.h:76
static mat2< T > Unit()
Definition: mat2.h:57
T * operator[](const int &index)
Definition: mat2.h:202
mat2< T > & operator*=(const mat2< T > &rhs)
Definition: mat2.h:163
mat2()
Definition: mat2.h:18
mat2< T > & operator+=(const mat2< T > &rhs)
Definition: mat2.h:139
mat2< T > & operator/=(const T &rhs)
Definition: mat2.h:189
mat2< T > & operator*=(const T &rhs)
Definition: mat2.h:177
friend mat2< T > operator/(mat2< T > lhs, const T &rhs)
Definition: mat2.h:131
friend mat2< T > operator-(mat2< T > lhs, const mat2< T > &rhs)
Definition: mat2.h:97
mat2< T > Inverse() const
Definition: mat2.h:78
friend vec2< T > operator*(mat2< T > lhs, const vec2< T > &rhs)
Definition: mat2.h:113
friend mat2< T > operator+(mat2< T > lhs, const mat2< T > &rhs)
Definition: mat2.h:89
friend mat2< T > operator*(mat2< T > lhs, const T &rhs)
Definition: mat2.h:105
mat2(const vec2< T > &A, const vec2< T > &B)
Definition: mat2.h:42
friend std::ostream & operator<<(std::ostream &, const mat2< U > &)
Definition: mat2.h:221
mat2< T > & operator-=(const mat2< T > &rhs)
Definition: mat2.h:151
mat2< T > Transpose()
Definition: mat2.h:65
bool IsSymmetric()
Definition: mat2.h:71
mat2(const T &_a, const T &_b, const T &_c, const T &_d)
Definition: mat2.h:31
friend mat2< T > operator*(mat2< T > lhs, const mat2< T > &rhs)
Definition: mat2.h:123
Definition: vec2.h:11
T y
Definition: vec2.h:29
T x
Definition: vec2.h:29