diff --git a/crates/tools/src/dimension.rs b/crates/tools/src/dimension.rs index 5dbe1fd6ef..4e76ede6b2 100644 --- a/crates/tools/src/dimension.rs +++ b/crates/tools/src/dimension.rs @@ -541,7 +541,8 @@ fn line_level(levels: &[DimensionLine], d: &Dimension, p: Point, reach: f64) -> let (u, normal) = (d.direction(), d.normal()); let mut best: Option<(f64, f64, f64, (Point, Point))> = None; for &(ends, direction) in levels { - let span = [ends.1, d.a, d.b].iter().map(|q| (*q - ends.0).dot(u).abs()).fold(1.0, f64::max); + let along_u = [ends.0, ends.1, d.a, d.b].map(|q| (q - ends.0).dot(u)); + let span = along_u.iter().fold(f64::NEG_INFINITY, |m, s| m.max(*s)) - along_u.iter().fold(f64::INFINITY, |m, s| m.min(*s)); let drift = u.cross(direction).abs() * span; if !drift.is_finite() || drift > 1e-6 { continue; @@ -1214,6 +1215,27 @@ mod tests { assert!(at.distance(pt(10_000_100.0, 170.0)) < 1e-9, "{at:?}"); } + /// The span a level is checked across runs from the lowest to the highest point of both lines + /// along the dimension, whichever end of the other line comes first, and has no lower bound. + #[test] + fn a_level_is_checked_across_the_whole_span_of_both_lines() { + let pt = Point::new; + let level = |from: Point, to: Point| ((from, to), (to - from) / (to - from).hypot()); + // 0.00000075 pt off across 100 pt drifts 0.0000015 pt across the 200 pt both lines cover. + let d = DimensionTool::default().recipe(DimensionMode::Linear, pt(-100.0, 200.0), pt(100.0, 200.0), pt(0.0, 200.0), None); + let (a, b) = (pt(0.0, 170.0), pt(100.0, 170.000_000_75)); + for slanted in [level(a, b), level(b, a)] { + assert!(line_level(&[slanted], &d, pt(0.0, 168.0), 4.0).is_none(), "{slanted:?}"); + } + // 0.0000005 pt off across the 0.1 pt both lines cover is within the tolerance. + let d = DimensionTool::default().recipe(DimensionMode::Linear, pt(0.0, 200.0), pt(0.1, 200.0), pt(0.05, 200.0), None); + let (a, b) = (pt(0.0, 170.0), pt(0.1, 170.000_000_5)); + for short in [level(a, b), level(b, a)] { + let (at, _) = line_level(&[short], &d, pt(0.05, 168.0), 4.0).expect("on the level"); + assert!((at.y - 170.0).abs() < 1e-6, "{at:?}"); + } + } + /// A level that would put the line past the largest distance a dimension allows is passed /// over: the preview goes where the pointer is, with no level guide, or onto a level further /// off within reach that keeps the line within the limit.