Skip to content

Commit

Permalink
fix rectangle snapping
Browse files Browse the repository at this point in the history
  • Loading branch information
edwloef committed Feb 8, 2025
1 parent f2c9b6b commit 6d64fb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 10 additions & 0 deletions core/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
12 changes: 8 additions & 4 deletions core/src/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,20 @@ 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();
let bottom_right = (top_left + self.size().into()).snap();
let top_left = top_left.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,
})
Expand Down

0 comments on commit 6d64fb8

Please sign in to comment.