fix rectangle snapping
This commit is contained in:
parent
215cee8c82
commit
c1ab626307
2 changed files with 17 additions and 4 deletions
|
|
@ -107,3 +107,13 @@ where
|
||||||
write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -243,16 +243,19 @@ impl Rectangle<f32> {
|
||||||
|
|
||||||
/// Snaps the [`Rectangle`] to __unsigned__ integer coordinates.
|
/// Snaps the [`Rectangle`] to __unsigned__ integer coordinates.
|
||||||
pub fn snap(self) -> Option<Rectangle<u32>> {
|
pub fn snap(self) -> Option<Rectangle<u32>> {
|
||||||
let width = self.width as u32;
|
let top_left = self.position().snap();
|
||||||
let height = self.height as u32;
|
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 {
|
if width < 1 || height < 1 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(Rectangle {
|
Some(Rectangle {
|
||||||
x: self.x as u32,
|
x: top_left.x,
|
||||||
y: self.y as u32,
|
y: top_left.y,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue