philsupertramp/game-math
Loading...
Searching...
No Matches
mat/TestMat2.cpp

This is an example on how to use the mat2.h file.

#include "../Test.h"
#include <math/math.h> // NOLINT
template<class T>
class Mat2TestCase : public Test
{
assert(foo[0][0] == (T)0);
assert(foo[0][1] == (T)0);
assert(foo[1][0] == (T)0);
assert(foo[1][1] == (T)0);
return true;
}
bool TestUtils() {
assert(foo[0][0] == (T)1);
assert(foo[0][1] == (T)0);
assert(foo[1][0] == (T)0);
assert(foo[1][1] == (T)1);
foo = mat2<T>((T)1, (T)2, (T)2, (T)1);
assert(foo[0][0] == (T)1);
assert(foo[0][1] == (T)2);
assert(foo[1][0] == (T)2);
assert(foo[1][1] == (T)1);
assert(foo.IsSymmetric());
assert(fooCopy[0][0] == foo[0][0]);
assert(fooCopy[0][1] == foo[0][1]);
assert(fooCopy[1][0] == foo[1][0]);
assert(fooCopy[1][1] == foo[1][1]);
// nothing happened matrix is symmetric
assert(fooCopy[0][0] == foo[0][0]);
assert(fooCopy[0][1] == foo[0][1]);
assert(fooCopy[1][0] == foo[1][0]);
assert(fooCopy[1][1] == foo[1][1]);
fooCopy = mat2<T>((T)0, (T)-1, (T)1, (T)0);
assert(fooCopy[0][0] == (T)0);
assert(fooCopy[0][1] == (T)1);
assert(fooCopy[1][0] == (T)-1);
assert(fooCopy[1][1] == (T)0);
// std::cout << "mat2\n" << foo;
return true;
}
bool TestMath() {
mat2<T> foo(1, 2, 3, 4);
mat2<T> bar(5, 6, 7, 8);
vec2<T> vec(1, 2);
float delta = 2.0f;
// +
fooBar = foo + bar;
assert(fooBar[0][0] == (T)6.0f);
assert(fooBar[0][1] == (T)8.0f);
assert(fooBar[1][0] == (T)10.0f);
assert(fooBar[1][1] == (T)12.0f);
fooBar += bar;
assert(fooBar[0][0] == (T)6.0f);
assert(fooBar[0][1] == (T)8.0f);
assert(fooBar[1][0] == (T)10.0f);
assert(fooBar[1][1] == (T)12.0f);
// -
fooBar = foo - bar;
assert(fooBar[0][0] == (T)-4.0f);
assert(fooBar[0][1] == (T)-4.0f);
assert(fooBar[1][0] == (T)-4.0f);
assert(fooBar[1][1] == (T)-4.0f);
fooBar -= bar;
assert(fooBar[0][0] == (T)-4.0f);
assert(fooBar[0][1] == (T)-4.0f);
assert(fooBar[1][0] == (T)-4.0f);
assert(fooBar[1][1] == (T)-4.0f);
// /
assert(fooBar[0][0] == (T)0.5f);
assert(fooBar[0][1] == (T)1.0f);
assert(fooBar[1][0] == (T)1.5f);
assert(fooBar[1][1] == (T)2.0f);
assert(fooBar[0][0] == (T)0.5f);
assert(fooBar[0][1] == (T)1.0f);
assert(fooBar[1][0] == (T)1.5f);
assert(fooBar[1][1] == (T)2.0f);
// *
fooBar = foo * bar;
assert(fooBar[0][0] == (T)19.0f);
assert(fooBar[0][1] == (T)22.0f);
assert(fooBar[1][0] == (T)43.0f);
assert(fooBar[1][1] == (T)50.0f);
fooBar *= bar;
assert(fooBar[0][0] == (T)19.0f);
assert(fooBar[0][1] == (T)22.0f);
assert(fooBar[1][0] == (T)43.0f);
assert(fooBar[1][1] == (T)50.0f);
assert(fooVec.x == (T)5.0f);
assert(fooVec.y == (T)11.0f);
assert(fooBar[0][0] == (T)2.0f);
assert(fooBar[0][1] == (T)4.0f);
assert(fooBar[1][0] == (T)6.0f);
assert(fooBar[1][1] == (T)8.0f);
assert(fooBar[0][0] == (T)2.0f);
assert(fooBar[0][1] == (T)4.0f);
assert(fooBar[1][0] == (T)6.0f);
assert(fooBar[1][1] == (T)8.0f);
return true;
}
bool TestInverse() {
mat2<T> A = mat2<T>(1, 0, 0, 1);
AssertEqual(A, A.Inverse());
if(extended) {
A = mat2<T>(4, 7, 2, 6);
AssertEqual(A.Inverse(), mat2<T>(0.6, -0.7, -0.2, 0.4));
}
return true;
}
public:
void run() override {
}
};
int main() {
testCase.extended = true;
testCase.run();
testCase2.extended = true;
testCase2.run();
return 0;
}
Definition Matrix.h:42
constexpr Matrix< T > Transpose() const
Definition Matrix.h:256
Matrix()
Definition Matrix.h:69
static mat2< T > Unit()
Definition mat2.h:57