Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up GMatrix #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 51 additions & 83 deletions include/GMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@

class GMatrix {
public:
GMatrix() { this->setIdentity(); }
GMatrix(float a, float b, float c, float d, float e, float f) {
fMat[0] = a; fMat[1] = b; fMat[2] = c;
fMat[3] = d; fMat[4] = e; fMat[5] = f;
}
GMatrix() : fMat{1, 0, 0, 0, 1, 0} {}

void set6(float a, float b, float c, float d, float e, float f) {
fMat[0] = a; fMat[1] = b; fMat[2] = c;
fMat[3] = d; fMat[4] = e; fMat[5] = f;
}
GMatrix(float a, float b, float c, float d, float e, float f) : fMat{a, b, c, d, e, f} {}

enum {
SX, KX, TX,
Expand All @@ -40,46 +33,37 @@ class GMatrix {
}
return true;
}
bool operator!=(const GMatrix& m) { return !(*this == m); }

// These 7 methods must be implemented by the student.
static GMatrix MakeTranslate(float tx, float ty) { return GMatrix(1, 0, tx, 0, 1, ty); }

/**
* Set this matrix to identity.
*/
void setIdentity();

/**
* Set this matrix to translate by the specified amounts.
*/
void setTranslate(float tx, float ty);
static GMatrix MakeScale(float sx, float sy) { return GMatrix(sx, 0, 0, 0, sy, 0); }

/**
* Set this matrix to scale by the specified amounts.
*/
void setScale(float sx, float sy);
static GMatrix MakeScale(float scale) { return GMatrix::MakeScale(scale, scale); }

// These 4 methods must be implemented by the student.
/**
* Set this matrix to rotate by the specified radians.
* Return a matrix to rotate by the specified radians.
*
* Note: since positive-Y goes down, a small angle of rotation will increase Y.
*/
void setRotate(float radians);
static GMatrix MakeRotate(float radians);

/**
* Set this matrix to the concatenation of the two specified matrices, such that the resulting
* Return the concatenation of the two specified matrices, such that the resulting
* matrix, when applied to points will have the same effect as first applying the primo matrix
* to the points, and then applying the secundo matrix to the resulting points.
*
* Pts' = Secundo * Primo * Pts
*/
void setConcat(const GMatrix& secundo, const GMatrix& primo);
static GMatrix MakeConcat(const GMatrix& secundo, const GMatrix& primo);

/*
* If this matrix is invertible, return true and (if not null) set the inverse parameter.
* If this matrix is not invertible, return false and ignore the inverse parameter.
*/
bool invert(GMatrix* inverse) const;

/**
* Transform the set of points in src, storing the resulting points in dst, by applying this
* matrix. It is the caller's responsibility to allocate dst to be at least as large as src.
Expand All @@ -93,82 +77,66 @@ class GMatrix {
void mapPoints(GPoint dst[], const GPoint src[], int count) const;

///////////////////////////////////////////////////////////////////////////////////////////////
// These helper methods are implemented in terms of the previous 7 methods.
// These helper methods are implemented in terms of the previous methods.

GMatrix& set6(float a, float b, float c, float d, float e, float f) {
return *this = GMatrix{a, b, c, d, e, f};
}

GMatrix& setIdentity() {
return *this = GMatrix();
}

GMatrix& setTranslate(float tx, float ty) {
return *this = GMatrix::MakeTranslate(tx, ty);
}

GMatrix& setScale(float sx, float sy) {
return *this = GMatrix::MakeScale(sx, sy);
}

GMatrix& setRotate(float radians) {
return *this = GMatrix::MakeRotate(radians);
}

GMatrix& setConcat(const GMatrix& secundo, const GMatrix& primo) {
return *this = GMatrix::MakeConcat(secundo, primo);
}

GMatrix& preConcat(const GMatrix& primo) {
this->setConcat(*this, primo);
return *this;
return *this = GMatrix::MakeConcat(*this, primo);
}

GMatrix& preTranslate(float x, float y) {
GMatrix trans;
trans.setTranslate(x, y);
return this->preConcat(trans);
return this->preConcat(GMatrix::MakeTranslate(x, y));
}

GMatrix& preScale(float sx, float sy) {
GMatrix scale;
scale.setScale(sx, sy);
return this->preConcat(scale);
return this->preConcat(GMatrix::MakeScale(sx, sy));
}

GMatrix& preRotate(float radians) {
GMatrix rotate;
rotate.setRotate(radians);
return this->preConcat(rotate);
return this->preConcat(GMatrix::MakeRotate(radians));
}

GMatrix& postConcat(const GMatrix& secundo) {
this->setConcat(secundo, *this);
return *this;
return *this = GMatrix::MakeConcat(secundo, *this);
}

GMatrix& postTranslate(float x, float y) {
GMatrix trans;
trans.setTranslate(x, y);
return this->postConcat(trans);
return this->postConcat(GMatrix::MakeTranslate(x, y));
}

GMatrix& postScale(float sx, float sy) {
GMatrix scale;
scale.setScale(sx, sy);
return this->postConcat(scale);
return this->postConcat(GMatrix::MakeScale(sx, sy));
}

GMatrix& postRotate(float radians) {
GMatrix rotate;
rotate.setRotate(radians);
return this->postConcat(rotate);
}

static GMatrix MakeTranslate(float tx, float ty) {
GMatrix m;
m.setTranslate(tx, ty);
return m;
}

static GMatrix MakeScale(float scale) {
GMatrix m;
m.setScale(scale, scale);
return m;
}

static GMatrix MakeScale(float sx, float sy) {
GMatrix m;
m.setScale(sx, sy);
return m;
}

static GMatrix MakeRotate(float radians) {
GMatrix m;
m.setRotate(radians);
return m;
}

return this->postConcat(GMatrix::MakeRotate(radians));
}

GPoint mapXY(float x, float y) const {
GPoint pts[1]{ x, y };
this->mapPoints(pts, pts, 1);
return pts[0];
return this->mapPt(GPoint{x, y});
}

GPoint mapPt(GPoint pt) const {
Expand Down