From bc21f5ab725920ba9df53c8488051fb8c4905088 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sat, 2 Feb 2019 23:55:12 -0800 Subject: [PATCH] Implemented unimplemented things --- src/math.rs | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/math.rs b/src/math.rs index 779f4ef..0b7dd6e 100644 --- a/src/math.rs +++ b/src/math.rs @@ -42,8 +42,33 @@ impl Region for Region1 { } } - fn nearest(&self, n: &Scalar) -> Option { - unimplemented!(); + fn nearest(&self, s: &Scalar) -> Option { + use Region1::*; + match self { + Empty => None, + Full => Some(*s), + Singleton(n) => Some(*n), + Range(l, u) => match (l < s, s < u) { + (true, true) => Some(*s), + (true, false) => Some(*u), + (false, true) => Some(*l), + _ => None, + }, + Union(r1, r2) => { + let distance = |a: Scalar, b: Scalar| (a - b).abs(); + match (r1.nearest(s), r2.nearest(s)) { + (None, None) => None, + (Some(n), None) | (None, Some(n)) => Some(n), + (Some(n1), Some(n2)) => Some({ + if distance(*s, n1) <= distance(*s, n2) { + n1 + } else { + n2 + } + }), + } + } + } } } @@ -153,6 +178,15 @@ impl Region for Region2 { } impl Region2 { + pub fn union(r1: Region2, r2: Region2) -> Region2 { + use Region2::*; + match (r1, r2) { + (Empty, r) | (r, Empty) => r, + (Full, _) | (_, Full) => Full, + (r1, r2) => Union(Box::new(r1), Box::new(r2)), + } + } + pub fn intersect(&self, other: &Region2) -> Region2 { use Region2::*; match (self, other) { @@ -173,7 +207,9 @@ impl Region2 { } } (Line(l1), Line(l2)) => l1.intersect(l2), - _ => unimplemented!(), + (Union(un1, un2), o) | (o, Union(un1, un2)) => { + Self::union(un1.intersect(o), un2.intersect(o)) + } } } }