Change width to size, as it is more correct.

Add calculation of determinant, submatric, minorm cofactor and inverse of a matrix.
This commit is contained in:
Godzil
2020-02-14 19:19:36 +00:00
parent 9f2a41e6f3
commit 0621ca86e1
3 changed files with 325 additions and 33 deletions

View File

@@ -13,24 +13,30 @@
class Matrix
{
private:
protected:
/* 4x4 is the default */
double data[4*4];
int width;
int size;
public:
Matrix(int width);
Matrix(double values[], int width);
Matrix(int size);
Matrix(double values[], int size);
double get(int x, int y) const { return this->data[this->width * x + y]; };
void set(int x, int y, double v) { this->data[this->width * x + y] = v; };
double get(int x, int y) const { return this->data[this->size * x + y]; };
void set(int x, int y, double v) { this->data[this->size * x + y] = v; };
Matrix identity();
Matrix transpose();
double determinant();
Matrix submatrix(int row, int column);
Matrix inverse();
double minor(int row, int column) { return this->submatrix(row, column).determinant(); }
double cofactor(int row, int column) { return (((column+row)&1)?-1:1) * this->minor(row, column); }
bool operator==(const Matrix &b) const;
bool operator!=(const Matrix &b) const;
bool isInvertible() { return this->determinant() != 0; }
Matrix operator*(const Matrix &b) const;
Tuple operator*(const Tuple &b) const;
};
@@ -42,14 +48,6 @@ public:
Matrix4(double values[]) : Matrix(values, 4) { };
};
class Matrix2 : public Matrix
{
public:
Matrix2() : Matrix(2) { };
Matrix2(double values[]) : Matrix(values, 2) { };
};
class Matrix3 : public Matrix
{
public:
@@ -57,4 +55,13 @@ public:
Matrix3(double values[]) : Matrix(values, 3) { };
};
class Matrix2 : public Matrix
{
private:
using Matrix::data;
public:
Matrix2() : Matrix(2) { };
Matrix2(double values[]) : Matrix(values, 2) { };
};
#endif /* DORAYME_MATRIX_H */