From c1ab6263073b7e6f1195969df359d4bf9cb169df Mon Sep 17 00:00:00 2001 From: edwloef Date: Thu, 30 Jan 2025 15:00:33 +0100 Subject: [PATCH] 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, })