Merge pull request #2768 from edwloef/fix_rectangle_snap

fix rectangle snapping
This commit is contained in:
Héctor 2025-06-04 12:22:51 +02:00 committed by GitHub
commit 89f480bdae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 4 deletions

View file

@ -107,3 +107,13 @@ where
write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
}
}
impl Point<f32> {
/// Snaps the [`Point`] to __unsigned__ integer coordinates.
pub fn snap(self) -> Point<u32> {
Point {
x: self.x.round() as u32,
y: self.y.round() as u32,
}
}
}

View file

@ -244,16 +244,19 @@ impl Rectangle<f32> {
/// Snaps the [`Rectangle`] to __unsigned__ integer coordinates.
pub fn snap(self) -> Option<Rectangle<u32>> {
let width = self.width as u32;
let height = self.height as u32;
let top_left = self.position().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)?;
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,
})