Skip to content

Commit

Permalink
emath: add Rect::signed_distance_to_pos and Vec2::abs
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Apr 4, 2022
1 parent 5d19f38 commit 2d30bd7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions emath/src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ impl Rect {
dx * dx + dy * dy
}

/// Signed distance to the edge of the box.
///
/// Negative inside the box.
pub fn signed_distance_to_pos(&self, pos: Pos2) -> f32 {
let edge_distances = (pos - self.center()).abs() - self.size() * 0.5;
let inside_dist = edge_distances.x.max(edge_distances.y).min(0.0);
let outside_dist = edge_distances.max(Vec2::ZERO).length();
inside_dist + outside_dist
}

#[inline(always)]
pub fn x_range(&self) -> RangeInclusive<f32> {
self.min.x..=self.max.x
Expand Down
6 changes: 6 additions & 0 deletions emath/src/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ impl Vec2 {
vec2(self.x.ceil(), self.y.ceil())
}

#[must_use]
#[inline]
pub fn abs(self) -> Self {
vec2(self.x.abs(), self.y.abs())
}

/// True if all members are also finite.
#[inline(always)]
pub fn is_finite(self) -> bool {
Expand Down

0 comments on commit 2d30bd7

Please sign in to comment.