From: Ian Jackson Date: Wed, 24 Mar 2021 12:38:06 +0000 (+0000) Subject: Pos, Area: Provide Mean and middle X-Git-Tag: otter-0.5.0~468 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=41c56b9a7461a751e72f97a8dd3ad85c25254097;p=otter.git Pos, Area: Provide Mean and middle Signed-off-by: Ian Jackson --- diff --git a/src/prelude.rs b/src/prelude.rs index 967c8525..975f7019 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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; diff --git a/src/spec.rs b/src/spec.rs index 316d655d..0f2892cc 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -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 Add> for PosC { type Output = Result; #[throws(CoordinateOverflow)] @@ -344,6 +346,15 @@ pub mod pos_traits { } } + impl Mean for PosC 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 { pub fn promote(&self) -> PosC { self.map(|v| v as f64) } } @@ -410,6 +421,13 @@ pub mod implementation { } } + impl AreaC where T: Mean + Debug { + pub fn middle(&self) -> PosC { + Mean::mean(&self.0[0], + &self.0[1]) + } + } + #[test] fn empty_area() { let empty = Area::empty();