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.
This commit is contained in:
dotCooCoo committed 2026-11-20 20:09:25 -08:00
1 parent 320c4d4146
commit bd3f69f233
1 file changed
+3 -1
@@ -613,8 +613,10 @@ fn roots(at: &[f64], f: &dyn Fn(f64, usize) -> f64, tol: f64) -> Result<Vec<Root
// neighboring marks: one where the sign changes, a dip where it does not. A run
// that never leaves the tolerance (outlines that meet at a corner) is one double
// root at its middle.
// The run's sample angles, in increasing order, looked up by binary search.
let edges: Vec<f64> = (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))];