From 20a74fe319a8f18b5fe7125909be82bc95ed4141 Mon Sep 17 00:00:00 2001 From: Corwin Date: Wed, 6 Mar 2024 12:39:49 +0000 Subject: [PATCH] introduce vector and rect abs --- agb-fixnum/src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/agb-fixnum/src/lib.rs b/agb-fixnum/src/lib.rs index 9d912bff..7df4964c 100644 --- a/agb-fixnum/src/lib.rs +++ b/agb-fixnum/src/lib.rs @@ -759,6 +759,16 @@ impl SubAssign for Vector2D { } } +impl Vector2D { + /// Calculates the absolute value of the x and y components. + pub fn abs(self) -> Self { + Self { + x: self.x.fixed_abs(), + y: self.y.fixed_abs(), + } + } +} + impl Vector2D> { #[must_use] /// Truncates the x and y coordinate, see [Num::trunc] @@ -1073,6 +1083,20 @@ impl Rect { } } +impl Rect { + /// Makes a rectangle that represents the equivalent location in space but with a positive size + pub fn abs(self) -> Self { + Self { + position: ( + self.position.x + self.size.x.min(0.into()), + self.position.y + self.size.y.min(0.into()), + ) + .into(), + size: self.size.abs(), + } + } +} + impl Vector2D { /// Created a vector from the given coordinates /// ```