Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix
- {
- //moya
- public:
- typedef double Cell;
- private:
- int size;
- Cell **cell;
- void AllocateMemory(int);
- void FreeMemory();
- public:
- Matrix() : size(0), cell(NULL) {} // Конструктор по умолчанию
- Matrix(const Matrix &); // Конструктор копирования
- Matrix(int, Cell*); // Конструктор матрицы из списка Cell
- ~Matrix(); // Деструктор
- Cell &operator()(int i, int j) { return cell[i][j]; }
- Matrix&operator=(const Matrix &); // Перегрузка оператора присваивания
- Matrix operator+(const Matrix &); // Сложение матриц
- Matrix operator-(const Matrix &); // Вычитание матриц
- Matrix operator*(const Matrix &); // Умножение матриц
- Matrix& Translate(Cell, Cell);
- Matrix& Translate3d(Cell, Cell, Cell);
- };
- Matrix::Matrix(const Matrix &m)
- {
- AllocateMemory(m.size);
- for (int i=0; i<size; i++)
- for (int j=0; j<size; j++)
- cell[i][j] = m.cell[i][j];
- }
- Matrix::Matrix(int n, Cell* list=NULL)
- {
- AllocateMemory(n);
- if (list == NULL)
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- cell[i][j] = 0;
- else
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- {
- cell[i][j] = list [j] ;
- }
- // заполнение матрицы элементами из списка list
- }
- Matrix::~Matrix()
- {
- FreeMemory();
- }
- Matrix& Matrix::operator=(const Matrix &m)
- {
- if (size != m.size)
- {
- FreeMemory();
- AllocateMemory(m.size);
- }
- for (int i=0; i<size; i++)
- for (int j=0; j<size; j++)
- cell[i][j] = m.cell[i][j];
- return *this;
- }
- Matrix Matrix::operator+(const Matrix &m)
- {
- Matrix res(*this);
- if (size == m.size)
- {
- for (int i=0; i<size; i++)
- for (int j=0; j<size; j++)
- res.cell[i][j]+=m.cell[i][j];
- }
- return res;
- }
- Matrix Matrix::operator-(const Matrix &m)
- {
- Matrix res(*this);
- if (size == m.size)
- {
- for (int i=0; i<size; i++)
- for (int j=0; j<size; j++)
- res.cell[i][j]-=m.cell[i][j];
- }
- return res;
- }
- Matrix Matrix::operator*(const Matrix &m)
- {
- Matrix res(*this);
- if (size == m.size)
- {
- for (int i=0; i<size; i++)
- for (int j=0; j<size; j++)
- for (int k=0; k<size; k++)
- res.cell[i][j]+=res.cell[i][k] * res.cell[k][j];
- }
- return res;
- }
- void Matrix::AllocateMemory(int n)
- {
- size=n;
- cell = new Cell*[size];
- for (int i=0; i<size; i++)
- cell[i] = new Cell[size];
- }
- void Matrix::FreeMemory()
- {
- for (int i = 0; i < size; i++)
- delete[] cell[i];
- delete[] cell;
- size = 0;
- //очистка
- }
- Matrix Translation(Matrix::Cell x, Matrix::Cell y)
- {
- Matrix::Cell t[9]={1,0,x, 0,1,y, 0,0,1};
- return Matrix(3, t);
- }
- Matrix Scaling(double k_x, double k_y)
- {
- Matrix::Cell t[9] = { k_x, 0, 0, 0, k_y, 0, 0, 0, 1 };
- return Matrix(3, t);
- }
- Matrix& Matrix::Translate(Cell x, Cell y)
- {
- return *this = Translation(x,y) * (*this);
- }
- Matrix Translation3d(Matrix::Cell x, Matrix::Cell y, Matrix::Cell z)
- {
- //Matrix::Cell t[16]=...;
- //return Matrix(4, t);
- }
- Matrix& Matrix::Translate3d(Cell x, Cell y, Cell z)
- {
- return *this = Translation3d(x,y,z) * (*this);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement