From 5621a46b4b23f6e6ae86b88cb2e96e2a4551df37 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 15 Mar 2021 21:26:58 +0100 Subject: [PATCH] emath: add any_nan to Vec2, Pos2 and Rect --- emath/src/pos2.rs | 6 ++++++ emath/src/rect.rs | 7 +++++++ emath/src/vec2.rs | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/emath/src/pos2.rs b/emath/src/pos2.rs index 7afcb3aa..7fde33cb 100644 --- a/emath/src/pos2.rs +++ b/emath/src/pos2.rs @@ -122,10 +122,16 @@ impl Pos2 { pos2(self.x.ceil(), self.y.ceil()) } + /// True if all members are also finite. pub fn is_finite(self) -> bool { self.x.is_finite() && self.y.is_finite() } + /// True if any member is NaN. + pub fn any_nan(self) -> bool { + self.x.is_nan() || self.y.is_nan() + } + #[must_use] pub fn min(self, other: Self) -> Self { pos2(self.x.min(other.x), self.y.min(other.y)) diff --git a/emath/src/rect.rs b/emath/src/rect.rs index 9a8af43c..d8849c65 100644 --- a/emath/src/rect.rs +++ b/emath/src/rect.rs @@ -163,6 +163,7 @@ impl Rect { Rect::from_min_size(self.min + amnt, self.size()) } + /// The intersection of two `Rect`, i.e. the area covered by both. #[must_use] pub fn intersect(self, other: Rect) -> Self { Self { @@ -290,9 +291,15 @@ impl Rect { self.max.x < self.min.x || self.max.y < self.min.y } + /// True if all members are also finite. pub fn is_finite(&self) -> bool { self.min.is_finite() && self.max.is_finite() } + + /// True if any member is NaN. + pub fn any_nan(self) -> bool { + self.min.any_nan() || self.max.any_nan() + } } /// ## Convenience functions (assumes origin is towards left top): diff --git a/emath/src/vec2.rs b/emath/src/vec2.rs index 4a59904c..73450c24 100644 --- a/emath/src/vec2.rs +++ b/emath/src/vec2.rs @@ -155,6 +155,11 @@ impl Vec2 { self.x.is_finite() && self.y.is_finite() } + /// True if any member is NaN. + pub fn any_nan(self) -> bool { + self.x.is_nan() || self.y.is_nan() + } + #[must_use] pub fn min(self, other: Self) -> Self { vec2(self.x.min(other.x), self.y.min(other.y))