Advertisement
Guest User

Matrix2.cpp

a guest
Mar 5th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. //Matrix2.cpp by Vassillen M. Chizhov
  2. #include "Matrix2.h"
  3.  
  4. namespace myGameMathLib
  5. {
  6.     Matrix2 Matrix2::Identity(FP(1), FP(0), FP(0), FP(1));
  7.     Matrix2 Matrix2::Zero(FP(0), FP(0), FP(0), FP(0));
  8.  
  9.  
  10.     //ctors
  11.     Matrix2::Matrix2()
  12.         :_11(FP(1)), _12(FP(0)),
  13.         _21(FP(0)), _22(FP(1))
  14.     {
  15.  
  16.     }
  17.  
  18.     Matrix2::Matrix2(FP value)
  19.         : _11(value), _12(value),
  20.         _21(value), _22(value)
  21.     {
  22.  
  23.     }
  24.  
  25.     Matrix2::Matrix2(FP _11, FP _12, FP _21, FP _22)
  26.         : _11(_11), _12(_12),
  27.         _21(_21), _22(_22)
  28.     {
  29.  
  30.     }
  31.  
  32.     Matrix2::Matrix2(const Matrix2& another)
  33.         : _11(another._11), _12(another._12),
  34.         _21(another._21), _22(another._22)
  35.     {
  36.  
  37.     }
  38.  
  39.  
  40.     //operators
  41.     Matrix2& Matrix2::operator=(const Matrix2& another)
  42.     {
  43.         _11 = another._11;
  44.         _12 = another._12;
  45.         _21 = another._21;
  46.         _22 = another._22;
  47.  
  48.         return *this;
  49.     }
  50.  
  51.     Matrix2& Matrix2::operator+=(const Matrix2& another)
  52.     {
  53.         _11 += another._11;
  54.         _12 += another._12;
  55.         _21 += another._21;
  56.         _22 += another._22;
  57.  
  58.         return *this;
  59.     }
  60.  
  61.     Matrix2& Matrix2::operator-=(const Matrix2& another)
  62.     {
  63.         _11 -= another._11;
  64.         _12 -= another._12;
  65.         _21 -= another._21;
  66.         _22 -= another._22;
  67.  
  68.         return *this;
  69.     }
  70.  
  71.     Matrix2& Matrix2::operator*=(const Matrix2& another)
  72.     {
  73.         FP temp;
  74.         temp = _11*another._11 + _12*another._21;
  75.         _12 = _11*another._12 + _12*another._22;
  76.         _11 = temp;
  77.         temp = _21*another._11 + _22*another._21;
  78.         _22 = _21*another._12 + _22*another._22;
  79.         _21 = temp;
  80.  
  81.         return *this;
  82.     }
  83.  
  84.     Matrix2& Matrix2::operator*=(FP scalar)
  85.     {
  86.         _11 *= scalar;
  87.         _12 *= scalar;
  88.         _21 *= scalar;
  89.         _22 *= scalar;
  90.  
  91.         return *this;
  92.     }
  93.  
  94.     Matrix2& Matrix2::operator/=(FP scalar)
  95.     {
  96.         _11 /= scalar;
  97.         _12 /= scalar;
  98.         _21 /= scalar;
  99.         _22 /= scalar;
  100.  
  101.         return *this;
  102.     }
  103.  
  104.     Matrix2 Matrix2::operator+()
  105.     {
  106.         return *this;
  107.     }
  108.  
  109.     Matrix2 Matrix2::operator-()
  110.     {
  111.         return Matrix2(-_11, -_12, -_21, -_22);
  112.     }
  113.  
  114.     FP Matrix2::det() const
  115.     {
  116.         return (_11*_22 - _12*_21);
  117.     }
  118.  
  119.     FP Matrix2::trace() const
  120.     {
  121.         return (_11 + _22);
  122.     }
  123.  
  124.     Matrix2 Matrix2::transpose() const
  125.     {
  126.         return Matrix2(_11, _21, _12, _22);
  127.     }
  128.  
  129.     Matrix2 Matrix2::inverse() const
  130.     {
  131.         FP determinant = det();
  132.         assert(!zero(determinant));
  133.         return Matrix2(_22 / determinant, -_12 / determinant, -_21 / determinant, _11 / determinant);
  134.     }
  135.  
  136.     bool operator==(const Matrix2& mat1, const Matrix2& mat2)
  137.     {
  138.         return (zero(mat1._11 - mat2._11) && zero(mat1._12 - mat2._12) && zero(mat1._21 - mat2._21) && zero(mat1._22 - mat2._22));
  139.     }
  140.  
  141.     bool operator!=(const Matrix2& mat1, const Matrix2& mat2)
  142.     {
  143.         return (!zero(mat1._11 - mat2._11) || !zero(mat1._12 - mat2._12) || !zero(mat1._21 - mat2._21) || !zero(mat1._22 - mat2._22));
  144.     }
  145.  
  146.     Matrix2 operator+(const Matrix2& mat1, const Matrix2& mat2)
  147.     {
  148.         return Matrix2(mat1._11 + mat2._11, mat1._12 + mat2._12, mat1._21 + mat2._21, mat1._22 + mat2._22);
  149.     }
  150.  
  151.     Matrix2 operator-(const Matrix2& mat1, const Matrix2& mat2)
  152.     {
  153.         return Matrix2(mat1._11 - mat2._11, mat1._12 - mat2._12, mat1._21 - mat2._21, mat1._22 - mat2._22);
  154.     }
  155.  
  156.     Matrix2 operator*(const Matrix2& mat1, const Matrix2& mat2)
  157.     {
  158.         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);
  159.     }
  160.  
  161.     Matrix2 operator*(const Matrix2& mat, FP scalar)
  162.     {
  163.         return Matrix2(mat._11*scalar, mat._12*scalar, mat._21*scalar, mat._22*scalar);
  164.     }
  165.  
  166.     Matrix2 operator*(FP scalar, const Matrix2& mat)
  167.     {
  168.         return Matrix2(scalar*mat._11, scalar*mat._12, scalar*mat._21, scalar*mat._22);
  169.     }
  170.  
  171.     Matrix2 operator/(const Matrix2& mat, FP scalar)
  172.     {
  173.         return Matrix2(mat._11 / scalar, mat._12 / scalar, mat._21 / scalar, mat._22 / scalar);
  174.     }
  175.  
  176.     Matrix2 operator/(FP scalar, const Matrix2& mat)
  177.     {
  178.         return Matrix2(scalar / mat._11, scalar / mat._12, scalar / mat._21, scalar / mat._22);
  179.     }
  180.  
  181.     FP det(const Matrix2& mat)
  182.     {
  183.         return mat.det();
  184.     }
  185.  
  186.     FP trace(const Matrix2& mat)
  187.     {
  188.         return mat.trace();
  189.     }
  190.  
  191.     Matrix2 transpose(const Matrix2& mat)
  192.     {
  193.         return mat.transpose();
  194.     }
  195.  
  196.     void transpose(Matrix2& mat)
  197.     {
  198.         FP temp = mat._12;
  199.         mat._12 = mat._21;
  200.         mat._21 = temp;
  201.     }
  202.  
  203.     Matrix2 inverse(const Matrix2& mat)
  204.     {
  205.         return mat.inverse();
  206.     }
  207.  
  208.     void inverse(Matrix2& mat)
  209.     {
  210.         FP determinant = mat.det();
  211.         assert(!zero(determinant));
  212.         FP temp = mat._11;
  213.         mat._11 = mat._22 / determinant;
  214.         mat._22 = temp / determinant;
  215.         mat._12 = -mat._12 / determinant;
  216.         mat._21 = -mat._21 / determinant;
  217.     }
  218.  
  219.     Matrix2 Matrix2::rotation(FP angle)
  220.     {
  221.         return Matrix2(cos(angle), -sin(angle), sin(angle), cos(angle));
  222.     }
  223.  
  224.     Matrix2 Matrix2::shearX(FP shearX)
  225.     {
  226.         return Matrix2(FP(1), shearX, FP(0), FP(1));
  227.     }
  228.  
  229.     Matrix2 Matrix2::shearY(FP shearY)
  230.     {
  231.         return Matrix2(FP(1), FP(0), shearY, FP(1));
  232.     }
  233.  
  234.     Matrix2 Matrix2::scaling(FP sx, FP sy)
  235.     {
  236.         return Matrix2(sx, FP(0), FP(0), sy);
  237.     }
  238.  
  239.     std::ostream& operator<<(std::ostream& os, const Matrix2& mat)
  240.     {
  241.         os << "\n" << mat._11 << " " << mat._12 << "\n" << mat._21 << " " << mat._22 << std::endl;
  242.         return os;
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement