From d11b02d45e6e1a614dd4e341ae424751bea3906f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 16 Jan 2021 20:57:31 +0100 Subject: [PATCH] [emath] Add Pos2::zero() and Rect::clamp(point) --- emath/src/pos2.rs | 7 +++++++ emath/src/rect.rs | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/emath/src/pos2.rs b/emath/src/pos2.rs index b0d18283..d00c6cb5 100644 --- a/emath/src/pos2.rs +++ b/emath/src/pos2.rs @@ -35,6 +35,13 @@ impl From<&[f32; 2]> for Pos2 { } impl Pos2 { + /// The zero position, the origin. + /// The top left corner in a GUI. + /// Same as `Pos2::default()`. + pub const fn zero() -> Self { + Self { x: 0.0, y: 0.0 } + } + pub const fn new(x: f32, y: f32) -> Self { Self { x, y } } diff --git a/emath/src/rect.rs b/emath/src/rect.rs index 8817822a..12606874 100644 --- a/emath/src/rect.rs +++ b/emath/src/rect.rs @@ -131,6 +131,14 @@ impl Rect { && p.y <= self.min.y + self.size().y } + /// Return the given points clamped to be inside the rectangle + #[must_use] + pub fn clamp(&self, mut p: Pos2) -> Pos2 { + p.x = clamp(p.x, self.x_range()); + p.y = clamp(p.y, self.y_range()); + p + } + pub fn extend_with(&mut self, p: Pos2) { self.min = self.min.min(p); self.max = self.max.max(p);