1
0
forked from josh/j3ml

Implement Transform2D

This commit is contained in:
2024-01-23 21:46:07 -05:00
parent f04e08201d
commit d012af1214
4 changed files with 63 additions and 10 deletions

View File

@@ -58,6 +58,7 @@ namespace LinearAlgebra {
void SetRow(int i, const Vector3 &vector3); void SetRow(int i, const Vector3 &vector3);
void SetColumn(int i, const Vector3& vector); void SetColumn(int i, const Vector3& vector);
void SetAt(int x, int y, float value);
void Orthonormalize(int c0, int c1, int c2) void Orthonormalize(int c0, int c1, int c2)
{ {
@@ -85,14 +86,20 @@ namespace LinearAlgebra {
// Transforming a vector v using this matrix computes the vector // Transforming a vector v using this matrix computes the vector
// v' == M * v == R*S*v == (R * (S * v)) which means the scale operation // v' == M * v == R*S*v == (R * (S * v)) which means the scale operation
// is applied to the vector first, followed by rotation, and finally translation // is applied to the vector first, followed by rotation, and finally translation
static Matrix3x3 FromRS(const Quaternion& rotate, const Matrix3x3& scale); static Matrix3x3 FromRS(const Quaternion& rotate, const Vector3& scale)
static Matrix3x3 FromRS(const Matrix3x3 &rotate, const Matrix3x3& scale); {
return Matrix3x3(rotate) * Matrix3x3::Scale(scale);
}
static Matrix3x3 FromRS(const Matrix3x3 &rotate, const Vector3& scale)
{
return rotate * Matrix3x3::Scale(scale);
}
/// Creates a new transformation matrix that scales by the given factors. /// Creates a new transformation matrix that scales by the given factors.
// This matrix scales with respect to origin. // This matrix scales with respect to origin.
static Matrix3x3 Scale(float sx, float sy, float sz); static Matrix3x3 Scale(float sx, float sy, float sz);
static Matrix3x3 Scale(const Matrix3x3& scale); static Matrix3x3 Scale(const Vector3& scale);
/// Returns the main diagonal. /// Returns the main diagonal.
Vector3 Diagonal() const; Vector3 Diagonal() const;

View File

@@ -8,17 +8,33 @@ namespace LinearAlgebra {
protected: protected:
Matrix3x3 transformation; Matrix3x3 transformation;
public: public:
const static Transform2D Identity;
const static Transform2D FlipX;
const static Transform2D FlipY;
Transform2D(float rotation, const Vector2& pos);
Transform2D(float px, float py, float sx, float sy, float ox, float oy, float kx, float ky, float rotation)
{
transformation = Matrix3x3(px, py, rotation, sx, sy, ox, oy, kx, ky);
}
Transform2D(const Vector2& pos, const Vector2& scale, const Vector2& origin, const Vector2& skew, float rotation);
Transform2D(const Matrix3x3& transform);
Transform2D Translate(const Vector2& offset) const; Transform2D Translate(const Vector2& offset) const;
Transform2D Translate(float x, float y) const; Transform2D Translate(float x, float y) const;
Transform2D Scale(float scale); // Perform Uniform Scale Transform2D Scale(float scale); // Perform Uniform Scale
Transform2D Scale(float x, float y); // Perform Nonunform Scale Transform2D Scale(float x, float y); // Perform Nonunform Scale
Transform2D Scale(const Vector2& scales); // Perform Nonuniform Scale Transform2D Scale(const Vector2& scales); // Perform Nonuniform Scale
Transform2D Rotate(); Transform2D Rotate();
Vector2 Transform(const Vector2& input) const;
Vector2 Transform(const Vector2& input) const Transform2D Inverse() const;
{ Transform2D AffineInverse() const;
return transformation * input; float Determinant() const;
} Vector2 GetOrigin() const;
float GetRotation() const;
Vector2 GetScale() const;
float GetSkew() const;
Transform2D OrthoNormalize();
}; };
} }

View File

@@ -37,9 +37,14 @@ namespace LinearAlgebra {
return {x, y, z}; return {x, y, z};
} }
float Matrix3x3::At(int x, int y) const { float Matrix3x3::At(int x, int y) const {
return this->elems[x][y]; return this->elems[x][y];
}
void Matrix3x3::SetAt(int x, int y, float value)
{
this->elems[x][y] = value;
} }
Vector3 Matrix3x3::operator*(const Vector3 &rhs) const { Vector3 Matrix3x3::operator*(const Vector3 &rhs) const {

View File

@@ -2,4 +2,29 @@
namespace LinearAlgebra { namespace LinearAlgebra {
const Transform2D Transform2D::Identity = Transform2D({0, 0}, {1, 1}, {0,0}, {0,0}, 0);
const Transform2D Transform2D::FlipX = Transform2D({0, 0}, {-1, 1}, {0,0}, {0,0}, 0);
const Transform2D Transform2D::FlipY = Transform2D({0, 0}, {1, -1}, {0,0}, {0,0}, 0);
Vector2 Transform2D::Transform(const Vector2 &input) const {
return transformation * input;
}
Transform2D::Transform2D(const Matrix3x3 &transform) : transformation(transform) { }
Transform2D::Transform2D(const Vector2& pos, const Vector2& scale, const Vector2& origin, const Vector2& skew, float rotation) {
transformation = Matrix3x3(pos.x, pos.y, rotation, scale.x, scale.y, origin.x, origin.y, skew.x, skew.y);
}
Transform2D Transform2D::Translate(float x, float y) const {
auto copy = Matrix3x3(transformation);
copy.SetAt(0, 0, transformation.At(0, 0) + x);
copy.SetAt(0, 1, transformation.At(0, 1) + y);
return Transform2D(copy);
}
Transform2D Transform2D::Translate(const LinearAlgebra::Vector2 &input) const {
return Translate(input.x, input.y);
}
} }