From 6f9b7a9005f4d1c6b84192481d54e6c8bf91435e Mon Sep 17 00:00:00 2001 From: Andy Terra <152812+airstrike@users.noreply.github.com> Date: Wed, 29 Jan 2025 18:32:37 -0500 Subject: [PATCH] Implement division operation for `Rectangle`, `Size` and `Vector` --- core/src/rectangle.rs | 29 +++++++++++++++++++++++++++++ core/src/size.rs | 14 ++++++++++++++ core/src/vector.rs | 11 +++++++++++ 3 files changed, 54 insertions(+) diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 0a08bbe4..afc15cc8 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -354,6 +354,19 @@ impl std::ops::Mul for Rectangle { } } +impl std::ops::Div for Rectangle { + type Output = Self; + + fn div(self, scale: f32) -> Self { + Self { + x: self.x / scale, + y: self.y / scale, + width: self.width / scale, + height: self.height / scale, + } + } +} + impl From> for Rectangle { fn from(rectangle: Rectangle) -> Rectangle { Rectangle { @@ -410,3 +423,19 @@ where } } } + +impl std::ops::Div> for Rectangle +where + T: std::ops::Div + Copy, +{ + type Output = Rectangle; + + fn div(self, scale: Vector) -> Self { + Rectangle { + x: self.x / scale.x, + y: self.y / scale.y, + width: self.width / scale.x, + height: self.height / scale.y, + } + } +} diff --git a/core/src/size.rs b/core/src/size.rs index 539135bc..baa80b01 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -150,6 +150,20 @@ where } } +impl std::ops::Div for Size +where + T: std::ops::Div + Copy, +{ + type Output = Size; + + fn div(self, rhs: T) -> Self::Output { + Size { + width: self.width / rhs, + height: self.height / rhs, + } + } +} + impl std::ops::Mul> for Size where T: std::ops::Mul + Copy, diff --git a/core/src/vector.rs b/core/src/vector.rs index ff848c4f..4da0fa28 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -64,6 +64,17 @@ where } } +impl std::ops::Div for Vector +where + T: std::ops::Div + Copy, +{ + type Output = Self; + + fn div(self, scale: T) -> Self { + Self::new(self.x / scale, self.y / scale) + } +} + impl Default for Vector where T: Default,