emath: add Rect::signed_distance_to_pos and Vec2::abs

This commit is contained in:
Emil Ernerfeldt 2022-04-04 11:24:08 +02:00
parent 5d19f381f9
commit 2d30bd751c
2 changed files with 16 additions and 0 deletions

View File

@ -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

View File

@ -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 {