Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Matrix2.cpp by Vassillen M. Chizhov
- #include "Matrix2.h"
- namespace myGameMathLib
- {
- Matrix2 Matrix2::Identity(FP(1), FP(0), FP(0), FP(1));
- Matrix2 Matrix2::Zero(FP(0), FP(0), FP(0), FP(0));
- //ctors
- Matrix2::Matrix2()
- :_11(FP(1)), _12(FP(0)),
- _21(FP(0)), _22(FP(1))
- {
- }
- Matrix2::Matrix2(FP value)
- : _11(value), _12(value),
- _21(value), _22(value)
- {
- }
- Matrix2::Matrix2(FP _11, FP _12, FP _21, FP _22)
- : _11(_11), _12(_12),
- _21(_21), _22(_22)
- {
- }
- Matrix2::Matrix2(const Matrix2& another)
- : _11(another._11), _12(another._12),
- _21(another._21), _22(another._22)
- {
- }
- //operators
- Matrix2& Matrix2::operator=(const Matrix2& another)
- {
- _11 = another._11;
- _12 = another._12;
- _21 = another._21;
- _22 = another._22;
- return *this;
- }
- Matrix2& Matrix2::operator+=(const Matrix2& another)
- {
- _11 += another._11;
- _12 += another._12;
- _21 += another._21;
- _22 += another._22;
- return *this;
- }
- Matrix2& Matrix2::operator-=(const Matrix2& another)
- {
- _11 -= another._11;
- _12 -= another._12;
- _21 -= another._21;
- _22 -= another._22;
- return *this;
- }
- Matrix2& Matrix2::operator*=(const Matrix2& another)
- {
- FP temp;
- temp = _11*another._11 + _12*another._21;
- _12 = _11*another._12 + _12*another._22;
- _11 = temp;
- temp = _21*another._11 + _22*another._21;
- _22 = _21*another._12 + _22*another._22;
- _21 = temp;
- return *this;
- }
- Matrix2& Matrix2::operator*=(FP scalar)
- {
- _11 *= scalar;
- _12 *= scalar;
- _21 *= scalar;
- _22 *= scalar;
- return *this;
- }
- Matrix2& Matrix2::operator/=(FP scalar)
- {
- _11 /= scalar;
- _12 /= scalar;
- _21 /= scalar;
- _22 /= scalar;
- return *this;
- }
- Matrix2 Matrix2::operator+()
- {
- return *this;
- }
- Matrix2 Matrix2::operator-()
- {
- return Matrix2(-_11, -_12, -_21, -_22);
- }
- FP Matrix2::det() const
- {
- return (_11*_22 - _12*_21);
- }
- FP Matrix2::trace() const
- {
- return (_11 + _22);
- }
- Matrix2 Matrix2::transpose() const
- {
- return Matrix2(_11, _21, _12, _22);
- }
- Matrix2 Matrix2::inverse() const
- {
- FP determinant = det();
- assert(!zero(determinant));
- return Matrix2(_22 / determinant, -_12 / determinant, -_21 / determinant, _11 / determinant);
- }
- bool operator==(const Matrix2& mat1, const Matrix2& mat2)
- {
- return (zero(mat1._11 - mat2._11) && zero(mat1._12 - mat2._12) && zero(mat1._21 - mat2._21) && zero(mat1._22 - mat2._22));
- }
- bool operator!=(const Matrix2& mat1, const Matrix2& mat2)
- {
- return (!zero(mat1._11 - mat2._11) || !zero(mat1._12 - mat2._12) || !zero(mat1._21 - mat2._21) || !zero(mat1._22 - mat2._22));
- }
- Matrix2 operator+(const Matrix2& mat1, const Matrix2& mat2)
- {
- return Matrix2(mat1._11 + mat2._11, mat1._12 + mat2._12, mat1._21 + mat2._21, mat1._22 + mat2._22);
- }
- Matrix2 operator-(const Matrix2& mat1, const Matrix2& mat2)
- {
- return Matrix2(mat1._11 - mat2._11, mat1._12 - mat2._12, mat1._21 - mat2._21, mat1._22 - mat2._22);
- }
- Matrix2 operator*(const Matrix2& mat1, const Matrix2& mat2)
- {
- return Matrix2(mat1._11*mat2._11 + mat1._12*mat2._21, mat1._11*mat2._12 + mat1._12*mat2._22, mat1._21*mat2._11 + mat1._22*mat2._21, mat1._21*mat2._12 + mat1._22*mat2._22);
- }
- Matrix2 operator*(const Matrix2& mat, FP scalar)
- {
- return Matrix2(mat._11*scalar, mat._12*scalar, mat._21*scalar, mat._22*scalar);
- }
- Matrix2 operator*(FP scalar, const Matrix2& mat)
- {
- return Matrix2(scalar*mat._11, scalar*mat._12, scalar*mat._21, scalar*mat._22);
- }
- Matrix2 operator/(const Matrix2& mat, FP scalar)
- {
- return Matrix2(mat._11 / scalar, mat._12 / scalar, mat._21 / scalar, mat._22 / scalar);
- }
- Matrix2 operator/(FP scalar, const Matrix2& mat)
- {
- return Matrix2(scalar / mat._11, scalar / mat._12, scalar / mat._21, scalar / mat._22);
- }
- FP det(const Matrix2& mat)
- {
- return mat.det();
- }
- FP trace(const Matrix2& mat)
- {
- return mat.trace();
- }
- Matrix2 transpose(const Matrix2& mat)
- {
- return mat.transpose();
- }
- void transpose(Matrix2& mat)
- {
- FP temp = mat._12;
- mat._12 = mat._21;
- mat._21 = temp;
- }
- Matrix2 inverse(const Matrix2& mat)
- {
- return mat.inverse();
- }
- void inverse(Matrix2& mat)
- {
- FP determinant = mat.det();
- assert(!zero(determinant));
- FP temp = mat._11;
- mat._11 = mat._22 / determinant;
- mat._22 = temp / determinant;
- mat._12 = -mat._12 / determinant;
- mat._21 = -mat._21 / determinant;
- }
- Matrix2 Matrix2::rotation(FP angle)
- {
- return Matrix2(cos(angle), -sin(angle), sin(angle), cos(angle));
- }
- Matrix2 Matrix2::shearX(FP shearX)
- {
- return Matrix2(FP(1), shearX, FP(0), FP(1));
- }
- Matrix2 Matrix2::shearY(FP shearY)
- {
- return Matrix2(FP(1), FP(0), shearY, FP(1));
- }
- Matrix2 Matrix2::scaling(FP sx, FP sy)
- {
- return Matrix2(sx, FP(0), FP(0), sy);
- }
- std::ostream& operator<<(std::ostream& os, const Matrix2& mat)
- {
- os << "\n" << mat._11 << " " << mat._12 << "\n" << mat._21 << " " << mat._22 << std::endl;
- return os;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement