Tests for magnetizing in the negative direction

This commit is contained in:
2020-08-28 12:19:38 -07:00
parent 65c6f6caa4
commit 6b509b6994
2 changed files with 34 additions and 12 deletions

View File

@@ -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 {

View File

@@ -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)));
}
}