From 89c834155d862ad38ae7983688bb892171fc5a30 Mon Sep 17 00:00:00 2001 From: Jason Brashear <115739170+webdevtodayjason@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:36:51 -0500 Subject: [PATCH] Geometry: keep vector midpoints finite at coordinate extremes (#793) --- crates/geom/src/vec.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/geom/src/vec.rs b/crates/geom/src/vec.rs index a75492c6..ba0f358e 100644 --- a/crates/geom/src/vec.rs +++ b/crates/geom/src/vec.rs @@ -66,7 +66,7 @@ impl Vec2 { self + (o - self) * t } pub fn mid(self, o: Vec2) -> Vec2 { - self.lerp(o, 0.5) + Vec2::new(self.x.midpoint(o.x), self.y.midpoint(o.y)) } pub fn is_finite(self) -> bool { self.x.is_finite() && self.y.is_finite() @@ -205,6 +205,18 @@ impl Neg for Vec3 { mod tests { use super::*; + #[test] + fn midpoint_handles_finite_coordinate_extremes() { + for (a, b, expected) in [ + (Vec2::new(-1e308, 1e308), Vec2::new(1e308, -1e308), Vec2::ZERO), + (Vec2::new(1e308, -1e308), Vec2::new(1.6e308, -1.6e308), Vec2::new(1.3e308, -1.3e308)), + (Vec2::new(f64::from_bits(1), 0.0), Vec2::new(f64::from_bits(1), 0.0), Vec2::new(f64::from_bits(1), 0.0)), + ] { + assert_eq!(a.mid(b), expected); + assert_eq!(b.mid(a), expected); + } + } + #[test] fn near_uses_finite_nonnegative_tolerance_without_squaring() { let origin = Vec2::ZERO;