From 320c4d41468df89a96bd01117077b182dfab2f29 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 19:29:36 -0800 Subject: [PATCH 1/2] Path Precision Tools: Common Tangents keeps both lines from a point close to a round path When two or more neighboring sampled normal angles all came out within the touching tolerance, the root search took the run as one place where the outlines touch and returned a single line at its middle. A point just outside a circle, with both of its tangents on neighboring samples, then got one line that misses the circle instead of two. The search now looks between each pair of those samples for a value that leaves the tolerance, and finds the roots between those places: a crossing where the sign changes, a dip where it does not. A run that stays within the tolerance, as at a corner, still gives one line. --- .../pathops/src/addons/pathtools/tangents.rs | 67 ++++++++++++++++--- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/crates/pathops/src/addons/pathtools/tangents.rs b/crates/pathops/src/addons/pathtools/tangents.rs index 655e84e311..e893a93997 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,45 @@ fn roots(at: &[f64], f: &dyn Fn(f64, usize) -> f64, tol: f64) -> Result = [(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 +653,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 +1166,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); From bd3f69f23347df3caa7007f9659d4b4892885fed Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 20:09:25 -0800 Subject: [PATCH 2/2] Path Precision Tools: Common Tangents finds a run's sample by binary search The touching-run search looked up the sample before each angle with a linear scan over the run, so a long run of samples within the tolerance cost time in proportion to the square of its length. It now collects the run's sample angles once and uses partition_point, which gives the same sample at every angle. --- crates/pathops/src/addons/pathtools/tangents.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/pathops/src/addons/pathtools/tangents.rs b/crates/pathops/src/addons/pathtools/tangents.rs index e893a93997..e10d8df5b7 100644 --- a/crates/pathops/src/addons/pathtools/tangents.rs +++ b/crates/pathops/src/addons/pathtools/tangents.rs @@ -613,8 +613,10 @@ 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 = (1..=len + 1).take_while(|j| ang(k, *j) <= t).count(); + let j = edges.partition_point(|a| *a <= t); f(t, (k + j) % n) }; let mut marks = vec![(theta, val(k))];