chiark / gitweb /
Pos, Area: Provide Mean and middle
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 24 Mar 2021 12:38:06 +0000 (12:38 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 24 Mar 2021 12:47:55 +0000 (12:47 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/prelude.rs
src/spec.rs

index 967c85255976980e4ef3723b50db98e72cf0f618..975f7019245fa5ef557ca2b61bb8df87d9b1efd8 100644 (file)
@@ -136,6 +136,7 @@ pub use crate::pieces::*;
 pub use crate::shapelib;
 pub use crate::slotmap_slot_idx::*;
 pub use crate::spec::*;
+pub use crate::spec::pos_traits::*;
 pub use crate::spec::piece_specs::{FaceColourSpecs, SimpleCommon};
 pub use crate::sse;
 pub use crate::toml_de;
index 316d655d061263a320aebe387d20818ad89cf021..0f2892cc294d71f5e2b619c9a4d9094d188c5580 100644 (file)
@@ -276,6 +276,8 @@ pub mod pos_traits {
   use std::ops::{Add,Sub,Mul,Neg};
   use crate::prelude::*;
 
+  pub trait Mean { fn mean(&self, other: &Self) -> Self; }
+
   impl<T:CheckedArith> Add<PosC<T>> for PosC<T> {
     type Output = Result<Self, CoordinateOverflow>;
     #[throws(CoordinateOverflow)]
@@ -344,6 +346,15 @@ pub mod pos_traits {
     }
   }
 
+  impl<T> Mean for PosC<T> where T: Mean + Debug {
+    fn mean(&self, other: &Self) -> Self where T: Mean {
+      PosC::try_from_iter_2(
+        izip!(&self.0, &other.0)
+          .map(|(a,b)| Ok::<_,Void>(a.mean(b)))
+      ).unwrap_or_else(|v| match v { })
+    }
+  }
+
   impl PosC<Coord> {
     pub fn promote(&self) -> PosC<f64> { self.map(|v| v as f64) }
   }
@@ -410,6 +421,13 @@ pub mod implementation {
     }
   }
 
+  impl<T> AreaC<T> where T: Mean + Debug {
+    pub fn middle(&self) -> PosC<T> {
+      Mean::mean(&self.0[0],
+                 &self.0[1])
+    }
+  }
+
   #[test]
   fn empty_area() {
     let empty = Area::empty();