Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. class Matrix
  2. {
  3.     //moya
  4. public:
  5.     typedef double Cell;
  6. private:
  7.     int size;
  8.     Cell **cell;
  9.     void AllocateMemory(int);
  10.     void FreeMemory();
  11. public:
  12.     Matrix() : size(0), cell(NULL) {}   // Конструктор по умолчанию
  13.     Matrix(const Matrix &);             // Конструктор копирования
  14.     Matrix(int, Cell*);                 // Конструктор матрицы из списка Cell
  15.     ~Matrix();                          // Деструктор
  16.  
  17.     Cell &operator()(int i, int j) { return cell[i][j]; }
  18.    
  19.     Matrix&operator=(const Matrix &);   // Перегрузка оператора присваивания
  20.     Matrix operator+(const Matrix &);   // Сложение матриц
  21.     Matrix operator-(const Matrix &);   // Вычитание матриц
  22.     Matrix operator*(const Matrix &);   // Умножение матриц
  23.  
  24.     Matrix& Translate(Cell, Cell);
  25.     Matrix& Translate3d(Cell, Cell, Cell);
  26. };
  27.  
  28. Matrix::Matrix(const Matrix &m)
  29. {
  30.     AllocateMemory(m.size);
  31.     for (int i=0; i<size; i++)
  32.         for (int j=0; j<size; j++)
  33.             cell[i][j] = m.cell[i][j];
  34. }
  35.  
  36. Matrix::Matrix(int n, Cell* list=NULL)
  37. {
  38.     AllocateMemory(n);
  39.     if (list == NULL)
  40.         for (int i = 0; i < size; i++)
  41.             for (int j = 0; j < size; j++)
  42.                 cell[i][j] = 0;
  43.     else
  44.         for (int i = 0; i < size; i++)
  45.             for (int j = 0; j < size; j++)
  46.             {
  47.                 cell[i][j] = list [j] ;
  48.             }
  49. //      заполнение матрицы элементами из списка list
  50. }
  51.  
  52. Matrix::~Matrix()
  53. {
  54.     FreeMemory();
  55. }
  56.  
  57. Matrix& Matrix::operator=(const Matrix &m)
  58. {
  59.     if (size != m.size)
  60.     {
  61.         FreeMemory();
  62.         AllocateMemory(m.size);
  63.     }
  64.     for (int i=0; i<size; i++)
  65.         for (int j=0; j<size; j++)
  66.             cell[i][j] = m.cell[i][j];
  67.     return *this;
  68. }
  69.  
  70. Matrix Matrix::operator+(const Matrix &m)
  71. {
  72.     Matrix res(*this);
  73.     if (size == m.size)
  74.     {
  75.         for (int i=0; i<size; i++)
  76.             for (int j=0; j<size; j++)
  77.                 res.cell[i][j]+=m.cell[i][j];
  78.     }
  79.     return res;
  80. }
  81.  
  82. Matrix Matrix::operator-(const Matrix &m)
  83. {
  84.     Matrix res(*this);
  85.     if (size == m.size)
  86.     {
  87.         for (int i=0; i<size; i++)
  88.             for (int j=0; j<size; j++)
  89.                 res.cell[i][j]-=m.cell[i][j];
  90.     }
  91.     return res;
  92. }
  93.  
  94. Matrix Matrix::operator*(const Matrix &m)
  95. {
  96.         Matrix res(*this);
  97.     if (size == m.size)
  98.     {
  99.         for (int i=0; i<size; i++)
  100.             for (int j=0; j<size; j++)
  101.                 for (int k=0; k<size; k++)
  102.                     res.cell[i][j]+=res.cell[i][k] * res.cell[k][j];
  103.     }
  104.     return res;
  105. }
  106.  
  107.  
  108.  
  109. void Matrix::AllocateMemory(int n)
  110. {
  111.     size=n;
  112.     cell = new Cell*[size];
  113.     for (int i=0; i<size; i++)
  114.         cell[i] = new Cell[size];
  115. }
  116.  
  117. void Matrix::FreeMemory()
  118. {
  119.     for (int i = 0; i < size; i++)
  120.         delete[] cell[i];
  121.     delete[] cell;
  122.     size = 0;
  123.     //очистка
  124. }
  125.  
  126. Matrix Translation(Matrix::Cell x, Matrix::Cell y)
  127. {
  128.     Matrix::Cell t[9]={1,0,x, 0,1,y, 0,0,1};
  129.     return Matrix(3, t);
  130. }
  131. Matrix Scaling(double k_x, double k_y)
  132. {
  133.     Matrix::Cell t[9] = { k_x, 0, 0, 0, k_y, 0, 0, 0, 1 };
  134.     return Matrix(3, t);
  135. }
  136.  
  137. Matrix& Matrix::Translate(Cell x, Cell y)
  138. {
  139.     return *this = Translation(x,y) * (*this);
  140. }
  141.  
  142. Matrix Translation3d(Matrix::Cell x, Matrix::Cell y, Matrix::Cell z)
  143. {
  144.     //Matrix::Cell t[16]=...;
  145.     //return Matrix(4, t);
  146. }
  147.  
  148. Matrix& Matrix::Translate3d(Cell x, Cell y, Cell z)
  149. {
  150.     return *this = Translation3d(x,y,z) * (*this);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement