From d364dfac66f3c7dd68b8bc8b10608664efb3e358 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 12 Apr 2022 11:33:48 +0200 Subject: [PATCH] Add Rect::distance_to_pos --- emath/src/rect.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/emath/src/rect.rs b/emath/src/rect.rs index cfb92f0b..38dbc7e5 100644 --- a/emath/src/rect.rs +++ b/emath/src/rect.rs @@ -317,6 +317,17 @@ impl Rect { self.width() * self.height() } + /// The distance from the rect to the position. + /// + /// The distance is zero when the position is in the interior of the rectangle. + #[inline] + pub fn distance_to_pos(&self, pos: Pos2) -> f32 { + self.distance_sq_to_pos(pos).sqrt() + } + + /// The distance from the rect to the position, squared. + /// + /// The distance is zero when the position is in the interior of the rectangle. #[inline] pub fn distance_sq_to_pos(&self, pos: Pos2) -> f32 { let dx = if self.min.x > pos.x {