From 44baee56b0d94476c8d866ce00e7b58e0a8afe08 Mon Sep 17 00:00:00 2001 From: Joe Bellus Date: Sat, 13 Apr 2024 00:23:01 -0400 Subject: [PATCH] Added shifting functions for Rect Added Rect::expand and Rect::translate for conveniently mutating Rects --- src/geometry.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index 33cdb31..bfc9ee9 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -171,6 +171,16 @@ impl Rect { size: size.into(), } } + + pub fn translate(&mut self, x: i32, y: i32) { + self.pos.x = (self.pos.x as i32 + x).max(0) as usize; + self.pos.y = (self.pos.y as i32 + y).max(0) as usize; + } + + pub fn expand(&mut self, width: i32, height: i32) { + self.size.width = (self.size.width as i32 + width).max(1) as usize; + self.size.height = (self.size.height as i32 + height).max(1) as usize; + } } impl From for Rect {