From 7884babf0a2722ed7edac310d10dee320dbcdbe0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 16:00:24 -0800 Subject: [PATCH] Path Precision Tools: the Tangent Circle Tool finds both circles where two second segments touch close together With a radius, each second segment is searched on its own, and each circle is then checked against all of them. Before, one search over the nearest of the second segments found one circle where two segments touched circles between the same two places tried along the first segment, so the tool could not pick the other. Follows #8526. --- .../addons/pathtools/tangent_circle/on_segments.rs | 14 ++++++++++++-- .../src/addons/pathtools/tangent_circle/tests.rs | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/pathops/src/addons/pathtools/tangent_circle/on_segments.rs b/crates/pathops/src/addons/pathtools/tangent_circle/on_segments.rs index 615c7f1412..f5d4df2647 100644 --- a/crates/pathops/src/addons/pathtools/tangent_circle/on_segments.rs +++ b/crates/pathops/src/addons/pathtools/tangent_circle/on_segments.rs @@ -7,7 +7,7 @@ //! //! The roots are found by sampling a distance function along one parameter: samples where it is 0, //! bisection where it changes sign, and a golden-section search where it comes down to 0 between -//! samples without changing sign. +//! samples without changing sign. For a radius, each second segment has its own distance function. use kurbo::{CubicBez, ParamCurve, ParamCurveDeriv, ParamCurveNearest, Point, Vec2}; @@ -256,7 +256,17 @@ pub fn circles_of_radius(first: CubicBez, second: &[CubicBez], radius: f64) -> V let mut found = vec![]; for side in [1.0, -1.0] { let center = |t: f64| normal(&first, t, own).map(|n| first.eval(t) + n * (side * radius)); - for t in roots(&samples, |t| center(t).and_then(|c| nearest(c, second)).map(|(d, _)| d - radius), CONTACT * radius) { + // Each second segment is searched on its own, so that two segments touching circles close + // together along `first` give both; `touching` then checks each circle against all of them. + let mut ts: Vec = second + .iter() + .flat_map(|seg| { + let dist = |t: f64| center(t).and_then(|c| nearest(c, std::slice::from_ref(seg))).map(|(d, _)| d - radius); + roots(&samples, dist, CONTACT * radius) + }) + .collect(); + ts.sort_by(f64::total_cmp); + for t in ts { if let Some(c) = center(t) && let Some(touch) = touching(c, radius, second, own) { diff --git a/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs b/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs index c130fb5be3..0104672125 100644 --- a/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs +++ b/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs @@ -654,6 +654,20 @@ fn two_circles_between_the_same_two_places_tried_are_both_found() { assert!(near(found[0].circle.center, x - dx, 20.0, 1e-6) && near(found[1].circle.center, x + dx, 20.0, 1e-6), "{found:?}"); } +#[test] +fn two_segments_that_touch_circles_close_together_give_both() { + // A peak whose two feet touch circles of radius 20 on y = 0 at both feet, between the same + // places tried along the line; then the same on a line 10,000 pt long, with the feet 10 pt apart. + for (len, a, b) in [(100.0, 50.03, 50.15), (1e4, 5003.0, 5013.0)] { + let top = 0.5 * (a + b); + let second = [line((a, 40.0), (top, 60.0)), line((top, 60.0), (b, 40.0))]; + let found = circles_of_radius(line((0.0, 0.0), (len, 0.0)), &second, 20.0); + assert_eq!(found.len(), 2, "{len}: {found:?}"); + assert!(near(found[0].circle.center, a, 20.0, 1e-6) && near(found[0].second, a, 40.0, 1e-6), "{len}: {found:?}"); + assert!(near(found[1].circle.center, b, 20.0, 1e-6) && near(found[1].second, b, 40.0, 1e-6), "{len}: {found:?}"); + } +} + #[test] fn a_tiny_circle_between_long_lines_close_together_is_found() { let found = circles_through(line((0.0, 0.0), (100.0, 0.0)), 0.5, &[line((0.0, 1e-4), (100.0, 1e-4))]);