From c1ab6263073b7e6f1195969df359d4bf9cb169df Mon Sep 17 00:00:00 2001 From: edwloef Date: Thu, 30 Jan 2025 15:00:33 +0100 Subject: [PATCH 1/3] fix rectangle snapping --- core/src/point.rs | 10 ++++++++++ core/src/rectangle.rs | 11 +++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/core/src/point.rs b/core/src/point.rs index cea57518..510f0d86 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -107,3 +107,13 @@ where write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y) } } + +impl Point { + /// Snaps the [`Point`] to __unsigned__ integer coordinates. + pub fn snap(self) -> Point { + Point { + x: self.x.round() as u32, + y: self.y.round() as u32, + } + } +} diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 0c4add05..44c29287 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -243,16 +243,19 @@ impl Rectangle { /// Snaps the [`Rectangle`] to __unsigned__ integer coordinates. pub fn snap(self) -> Option> { - let width = self.width as u32; - let height = self.height as u32; + let top_left = self.position().snap(); + let bottom_right = (self.position() + self.size().into()).snap(); + + let width = bottom_right.x - top_left.x; + let height = bottom_right.y - top_left.y; if width < 1 || height < 1 { return None; } Some(Rectangle { - x: self.x as u32, - y: self.y as u32, + x: top_left.x, + y: top_left.y, width, height, }) From 6f935f3449a3cfe7a64a8454a295688d41f2e733 Mon Sep 17 00:00:00 2001 From: edwloef Date: Thu, 29 May 2025 23:55:05 +0200 Subject: [PATCH 2/3] fix underflow when calculating new bounds --- core/src/rectangle.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 44c29287..8ffb0797 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -246,8 +246,8 @@ impl Rectangle { let top_left = self.position().snap(); let bottom_right = (self.position() + self.size().into()).snap(); - let width = bottom_right.x - top_left.x; - let height = bottom_right.y - top_left.y; + let width = bottom_right.x.checked_sub(top_left.x)?; + let height = bottom_right.y.checked_sub(top_left.y)?; if width < 1 || height < 1 { return None; From 08a37406f2d2fa6c8aacb6131e0675e7f12cda35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 4 Jun 2025 12:14:31 +0200 Subject: [PATCH 3/3] Replace generic `into` call with `Vector::from` --- core/src/rectangle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 8ffb0797..bea27b9d 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -244,7 +244,7 @@ impl Rectangle { /// Snaps the [`Rectangle`] to __unsigned__ integer coordinates. pub fn snap(self) -> Option> { let top_left = self.position().snap(); - let bottom_right = (self.position() + self.size().into()).snap(); + let bottom_right = (self.position() + Vector::from(self.size())).snap(); let width = bottom_right.x.checked_sub(top_left.x)?; let height = bottom_right.y.checked_sub(top_left.y)?;