Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
matrix_2.h
1//--------------------------------------------------------------//
2// Math Library
3// Matrix 2 Class Definition.
4//--------------------------------------------------------------//
5//
6// m0 m1
7// m2 m3
8//
9//--------------------------------------------------------------//
10
11#ifndef __MATRIX2_H__
12#define __MATRIX2_H__ 1
13
14#include "math/vector_2.h"
15namespace coma {
16class Mat2 {
17 public:
18 Mat2();
19 Mat2(float a[4]);
20 Mat2(float value);
21 Mat2(const Vec2& a, const Vec2& b);
22 Mat2(const Mat2& copy);
23 ~Mat2();
24 Mat2 Identity() const;
25 Mat2 Multiply(const Mat2& other) const;
26 float Determinant() const;
27 Mat2 Adjoint() const;
28 Vec2 GetLine(int line) const;
29 Vec2 GetColum(int line) const;
30
31 Mat2 Inverse() const;
32 Mat2 Transpose() const;
33
34 inline Mat2 operator+(const Mat2& other) const;
35 inline void operator+=(const Mat2& other);
36 inline Mat2 operator+(float value) const;
37 inline void operator+=(float value);
38 inline Mat2 operator-(const Mat2& other) const;
39 inline void operator-=(const Mat2& other);
40 inline Mat2 operator-(float value) const;
41 inline void operator-=(float value);
42
43 inline Mat2 operator*(float value) const;
44 inline void operator*=(float value);
45 inline Mat2 operator/(float value) const;
46 inline void operator/=(float value);
47
48 bool operator==(const Mat2& other) const;
49 bool operator!=(const Mat2& other) const;
50 inline void operator=(const Mat2& other);
51
52 float m[4];
53};
54
55inline Mat2 Mat2::operator+(const Mat2& other) const {
56 Mat2 res;
57 for (int i = 0; i < 4; i++) {
58 res.m[i] = m[i] + other.m[i];
59 }
60 return res;
61}
62
63inline void Mat2::operator+=(const Mat2& other) {
64 for (int i = 0; i < 4; i++) {
65 m[i] += other.m[i];
66 }
67}
68
69inline Mat2 Mat2::operator+(float value) const {
70 Mat2 res;
71 for (int i = 0; i < 4; i++) {
72 res.m[i] = m[i] + value;
73 }
74 return res;
75}
76
77inline void Mat2::operator+=(float value) {
78 for (int i = 0; i < 4; i++) {
79 m[i] += value;
80 }
81}
82
83inline Mat2 Mat2::operator-(const Mat2& other) const {
84 Mat2 res;
85 for (int i = 0; i < 4; i++) {
86 res.m[i] = m[i] - other.m[i];
87 }
88 return res;
89}
90
91inline void Mat2::operator-=(const Mat2& other) {
92 for (int i = 0; i < 4; i++) {
93 m[i] -= other.m[i];
94 }
95}
96
97inline Mat2 Mat2::operator-(float value) const {
98 Mat2 res;
99 for (int i = 0; i < 4; i++) {
100 res.m[i] = m[i] - value;
101 }
102 return res;
103}
104
105inline void Mat2::operator-=(float value) {
106 for (int i = 0; i < 4; i++) {
107 m[i] -= value;
108 }
109}
110
111inline Mat2 Mat2::operator*(float value) const {
112 Mat2 res;
113 for (int i = 0; i < 4; i++) {
114 res.m[i] = m[i] * value;
115 }
116 return res;
117}
118
119inline void Mat2::operator*=(float value) {
120 for (int i = 0; i < 4; i++) {
121 m[i] *= value;
122 }
123}
124
125inline Mat2 Mat2::operator/(float value) const {
126 Mat2 res;
127 for (int i = 0; i < 4; i++) {
128 res.m[i] = m[i] / value;
129 }
130 return res;
131}
132
133inline void Mat2::operator/=(float value) {
134 for (int i = 0; i < 4; i++) {
135 m[i] /= value;
136 }
137}
138
139inline bool Mat2::operator==(const Mat2& other) const {
140 for (int i = 0; i < 4; i++) {
141 if (m[i] != other.m[i]) return false;
142 }
143 return true;
144}
145
146inline bool Mat2::operator!=(const Mat2& other) const {
147 for (int i = 0; i < 4; i++) {
148 if (m[i] != other.m[i]) return true;
149 }
150 return false;
151}
152
153inline void Mat2::operator=(const Mat2& other) {
154 for (int i = 0; i < 4; i++) {
155 m[i] = other.m[i];
156 }
157}
158
159inline Mat2 Mat2::Identity() const { return Mat2(Vec2(1, 0), Vec2(0, 1)); }
160
161inline float Mat2::Determinant() const { return m[0] * m[3] - m[1] * m[2]; }
162
163inline Mat2 Mat2::Inverse() const {
164 float det = this->Determinant();
165 Mat2 res = this->Adjoint() / det;
166 return res;
167}
168
169inline Mat2 Mat2::Multiply(const Mat2& other) const {
170 Mat2 res;
171 for (int row = 0; row < 2; row++) {
172 for (int col = 0; col < 2; col++) {
173 res.m[row * 2 + col] = m[row * 2 + 0] * other.m[0 * 2 + col] +
174 m[row * 2 + 1] * other.m[1 * 2 + col] +
175 m[row * 2 + 2] * other.m[2 * 2 + col];
176 }
177 }
178 return res;
179}
180
181inline Mat2 Mat2::Adjoint() const {
182 return Mat2(Vec2(m[3], -m[1]), Vec2(-m[2], m[0]));
183}
184
185inline Mat2 Mat2::Transpose() const {
186 return Mat2(Vec2(m[0], m[2]), Vec2(m[1], m[3]));
187}
188
189inline Vec2 Mat2::GetLine(int line) const {
190 return Vec2(m[line * 2 + 0], m[line * 2 + 1]);
191}
192
193inline Vec2 Mat2::GetColum(int line) const {
194 return Vec2(m[line], m[2 + line]);
195}
196} // namespace coma
197#endif
copperdielf Math Library
Definition: buffer.hpp:5