Skip to content

Commit c41fc06

Browse files
authored
Merge pull request #770 from Ughuuu/expose-determinant-in-2d-transform
Add determinant to Transform2D
2 parents 7641696 + 8573e6f commit c41fc06

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ gen
2323
*.bat
2424
config.toml
2525
exp.rs
26+
27+
# Mac specific
28+
.DS_Store

godot-core/src/builtin/transform2d.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ impl Transform2D {
151151
self.glam(|aff| aff.inverse())
152152
}
153153

154+
/// Returns the determinant of the basis matrix.
155+
///
156+
/// If the basis is uniformly scaled, then its determinant equals the square of the scale factor.
157+
///
158+
/// A negative determinant means the basis was flipped, so one part of the scale is negative.
159+
/// A zero determinant means the basis isn't invertible, and is usually considered invalid.
160+
///
161+
/// _Godot equivalent: `Transform2D.determinant()`_
162+
pub fn determinant(&self) -> real {
163+
self.basis().determinant()
164+
}
165+
154166
/// Returns the transform's rotation (in radians).
155167
///
156168
/// _Godot equivalent: `Transform2D.get_rotation()`_
@@ -447,7 +459,7 @@ impl Basis2D {
447459
}
448460

449461
/// Returns the determinant of the matrix.
450-
pub(crate) fn determinant(&self) -> real {
462+
pub fn determinant(&self) -> real {
451463
self.glam(|mat| mat.determinant())
452464
}
453465

itest/rust/src/builtin_tests/geometry/transform2d_test.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ fn transform2d_equiv() {
5757
);
5858
}
5959

60+
#[cfg(since_api = "4.1")]
61+
#[itest]
62+
fn transform2d_determinant() {
63+
let inner = InnerTransform2D::from_outer(&TEST_TRANSFORM);
64+
let outer = TEST_TRANSFORM;
65+
66+
assert_eq_approx!(
67+
real::from_f64(inner.determinant()),
68+
outer.determinant(),
69+
"function: determinant\n"
70+
);
71+
}
72+
6073
#[itest]
6174
fn transform2d_xform_equiv() {
6275
let vec = Vector2::new(1.0, 2.0);

0 commit comments

Comments
 (0)