diff --git a/crates/pathops/src/addons/pathtools/tangents.rs b/crates/pathops/src/addons/pathtools/tangents.rs index 655e84e311..e10d8df5b7 100644 --- a/crates/pathops/src/addons/pathtools/tangents.rs +++ b/crates/pathops/src/addons/pathtools/tangents.rs @@ -576,15 +576,13 @@ fn roots(at: &[f64], f: &dyn Fn(f64, usize) -> f64, tol: f64) -> Result, s: f64, k: usize, lo: f64, hi: f64| { - let g = |t: f64| f(t, k); + // Between `lo` and `hi`, where `s * g` is positive at both ends: the least of `s * g`, a double + // root where it reaches zero, two roots where it goes past it, none where it stays above. + let dip = |out: &mut Vec, s: f64, g: &dyn Fn(f64) -> f64, lo: f64, hi: f64| { let (t, least) = golden(&|t| s * g(t), lo, hi); if least < -tol { - out.push(simple(bisect(&g, lo, t))); - out.push(simple(bisect(&g, t, hi))); + out.push(simple(bisect(g, lo, t))); + out.push(simple(bisect(g, t, hi))); } else if least <= tol { out.push(Root { angle: t, double: true }); } @@ -604,12 +602,47 @@ fn roots(at: &[f64], f: &dyn Fn(f64, usize) -> f64, tol: f64) -> Result = (1..=len + 1).map(|j| ang(k, j)).collect(); + let piece = |t: f64| { + let j = edges.partition_point(|a| *a <= t); + f(t, (k + j) % n) + }; + let mut marks = vec![(theta, val(k))]; + for j in 1..len { + let (lo, hi) = (ang(k, j), ang(k, j + 1)); + let g = |t: f64| f(t, (k + j) % n); + let (t_low, low) = golden(&g, lo, hi); + let (t_high, high) = golden(&|t| -g(t), lo, hi); + let mut found: Vec<(f64, f64)> = [(t_low, low), (t_high, -high)].into_iter().filter(|(_, v)| !zero(*v)).collect(); + found.sort_by(|a, b| a.0.total_cmp(&b.0)); + marks.extend(found); + } + marks.push((ang(k, len + 1), val(k + len + 1))); + if marks.len() == 2 { + out.push(Root { angle: (ang(k, 1) + ang(k, len)) / 2.0, double: true }); + } else { + for pair in marks.windows(2) { + if let [(lo, a), (hi, b)] = *pair { + if a.signum() == b.signum() { + dip(&mut out, a.signum(), &piece, lo, hi); + } else { + out.push(simple(bisect(&piece, lo, hi))); + } + } + } + } } continue; } @@ -622,7 +655,7 @@ fn roots(at: &[f64], f: &dyn Fn(f64, usize) -> f64, tol: f64) -> Result = out.into_iter().map(|r| Root { angle: r.angle.rem_euclid(TAU), ..r }).collect(); @@ -1135,6 +1168,22 @@ mod tests { assert_eq!(at(1e-6).len(), 2); } + #[test] + fn a_point_whose_two_tangents_fall_on_neighboring_samples_keeps_both() { + // The point lies on the bisector of two neighboring sampled normals, at the distance where + // its two tangents touch the circle at exactly those normals. Both samples are zero, but + // between them the point is well outside the circle: two lines, not one. + let (r, half) = (10.0, TAU / SAMPLES as f64 / 2.0); + let p = (Vec2::new(half.cos(), half.sin()) * (r / half.cos())).to_point(); + let all = common_tangents(&Outline::point(p).unwrap(), &circle(0.0, 0.0, r)).unwrap(); + assert_eq!(all.len(), 2, "{all:?}"); + // Each runs from the point to where the circle's radius meets it square. + for angle in [0.0, 2.0 * half] { + let touch = (Vec2::new(angle.cos(), angle.sin()) * r).to_point(); + assert!(all.iter().any(|t| ends_within(t, p, touch, 1e-6)), "{angle}: {all:?}"); + } + } + #[test] fn open_concave_empty_and_identical_outlines_are_refused() { let open = SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(10.0, 0.0), Point::new(10.0, 10.0)], false);