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.
This commit is contained in:
dotCooCoo committed 2026-11-20 16:00:24 -08:00
1 parent 063f550802
commit 7884babf0a
2 files changed
+26 -2

No files matched your search

@@ -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<f64> = 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)
{
@@ -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))]);