diff --git a/src/geom.rs b/src/geom.rs index 3df765c..d343683 100644 --- a/src/geom.rs +++ b/src/geom.rs @@ -124,7 +124,7 @@ impl Line { if self.is_ascending_x() { x >= self.from.x && x < self.to.x } else { - x >= self.to.x && x < self.from.x + x <= self.from.x && x > self.to.x } } @@ -133,17 +133,24 @@ impl Line { if self.is_ascending_x() { y >= self.from.y && y < self.to.y } else { - y >= self.to.y && y < self.from.y + y <= self.from.y && y > self.to.y } } + pub fn min_x(&self) -> f64 { + self.from.x.min(self.to.x).into() + } + + pub fn max_x(&self) -> f64 { + self.from.x.max(self.to.x).into() + } + pub fn clamp_x(&self, x: f64) -> f64 { - let x = real(x); match x { - v if v < self.from.x => self.from.x, - v if v > self.to.x => self.to.x, + v if v < self.min_x() => self.min_x(), + v if v > self.max_x() => self.max_x(), v => v, - }.into() + } } pub fn clamp_by_x(&self, x: f64) -> Point { diff --git a/src/mat.rs b/src/mat.rs index 26deee6..5f2949c 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -228,14 +228,14 @@ mod test { #[test] fn mh_curve_move_from_inner_to_inner() { let curve = mh_curve_for_test(); - assert_eq!(curve.move_to(0.0, 0.0, 5.0), (5.0, 0.0)); - assert_eq!(curve.move_to(0.0, 5.0, 10.0), (5.0, 5.0)); + assert_eq!(curve.step_toward(0.0, 0.0, 5.0), Ok((5.0, 0.0))); + assert_eq!(curve.step_toward(0.0, 5.0, 10.0), Ok((5.0, 5.0))); - assert_eq!(curve.move_to(-5.0, 5.0, -3.0), (-8.0, 5.0)); - assert_eq!(curve.move_to(-5.0, 5.0, 7.0), (2.0, 5.0)); + assert_eq!(curve.step_toward(-5.0, 5.0, -3.0), Ok((-8.0, 5.0))); + assert_eq!(curve.step_toward(-5.0, 5.0, 7.0), Ok((2.0, 5.0))); - assert_eq!(curve.move_to(5.0, -5.0, -3.0), (2.0, -5.0)); - assert_eq!(curve.move_to(5.0, -5.0, 3.0), (8.0, -5.0)); + assert_eq!(curve.step_toward(5.0, -5.0, -3.0), Ok((2.0, -5.0))); + assert_eq!(curve.step_toward(5.0, -5.0, 3.0), Ok((8.0, -5.0))); } #[test] @@ -252,4 +252,19 @@ mod test { // middle of segment to end of segment assert_eq!(curve.step_toward(12.0, 20.0, 120.0), Err((20.0, 100.0))); } + + #[test] + fn mh_curve_ascend_along_negative_edge() { + let curve = mh_curve_for_test(); + // start of segment to middle of segment + assert_eq!(curve.step_toward(-10.0, 0.0, -32.0), Ok((-12.0, -20.0))); + // start of segment NOOP + assert_eq!(curve.step_toward(-10.0, 0.0, -10.0), Ok((-10.0, 0.0))); + // middle of segment NOOP + assert_eq!(curve.step_toward(-12.0, -20.0, -32.0), Ok((-12.0, -20.0))); + // middle of segment to middle of segment + assert_eq!(curve.step_toward(-12.0, -20.0, -54.0), Ok((-14.0, -40.0))); + // middle of segment to end of segment + assert_eq!(curve.step_toward(-12.0, -20.0, -120.0), Err((-20.0, -100.0))); + } }