Skip to content

Commit aa794a2

Browse files
authored
feat: Add leftTranslateByVector2 method to Matrix4 (#351)
1 parent be9974e commit aa794a2

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 2.3.0-wip
22

3+
- Add `leftTranslateByVector2` method to `Matrix4`.
4+
Equivalent to the `leftTranslate` set of methods, that multiplies this matrix by a translation matrix from the left.
35
- Added `Matrix4.leftMultiply` method.
46
- Add translateByVector2 method to Matrix4.
57
- Require `sdk: ^3.7.0`.

lib/src/vector_math/matrix4.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,13 @@ class Matrix4 {
838838
_m4storage[15] = tw * r4;
839839
}
840840

841+
/// Multiply this by a translation from the left.
842+
@pragma('wasm:prefer-inline')
843+
@pragma('vm:prefer-inline')
844+
@pragma('dart2js:prefer-inline')
845+
void leftTranslateByVector2(Vector2 v2) =>
846+
leftTranslateByDouble(v2.x, v2.y, 0.0, 1.0);
847+
841848
/// Multiply this by a translation from the left.
842849
@pragma('wasm:prefer-inline')
843850
@pragma('vm:prefer-inline')

lib/src/vector_math_64/matrix4.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,13 @@ class Matrix4 {
838838
_m4storage[15] = tw * r4;
839839
}
840840

841+
/// Multiply this by a translation from the left.
842+
@pragma('wasm:prefer-inline')
843+
@pragma('vm:prefer-inline')
844+
@pragma('dart2js:prefer-inline')
845+
void leftTranslateByVector2(Vector2 v2) =>
846+
leftTranslateByDouble(v2.x, v2.y, 0.0, 1.0);
847+
841848
/// Multiply this by a translation from the left.
842849
@pragma('wasm:prefer-inline')
843850
@pragma('vm:prefer-inline')

test/matrix4_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,20 @@ void testLeftTranslate() {
862862
expect(result.x, equals(3.0));
863863
expect(result.y, equals(0.0));
864864
expect(result.z, equals(0.0));
865+
866+
// Matrix4 alternative methods
867+
final m2 = Matrix4.diagonal3Values(2.0, 3.0, 4.0);
868+
final result2 = Matrix4.columns(
869+
Vector4(2.0, 0.0, 0.0, 0.0),
870+
Vector4(0.0, 3.0, 0.0, 0.0),
871+
Vector4(0.0, 0.0, 4.0, 0.0),
872+
Vector4(1.0, 0.0, 0.0, 1.0),
873+
);
874+
875+
expect(m2.clone()..leftTranslateByDouble(1, 0, 0, 1), result2);
876+
expect(m2.clone()..leftTranslateByVector2(Vector2(1, 0)), result2);
877+
expect(m2.clone()..leftTranslateByVector3(Vector3(1, 0, 0)), result2);
878+
expect(m2.clone()..leftTranslateByVector4(Vector4(1, 0, 0, 1)), result2);
865879
}
866880

867881
void testMatrixClassifiers() {

0 commit comments

Comments
 (0)