Merge pull request #7905 from dotCooCoo/fix/touch-point-on-sample

Smart Guides: tangent and perpendicular snaps find a touch point that falls exactly on a sample
This commit is contained in:
BFlat
2026-11-18 03:19:25 +07:00
committed by GitHub
2 changed files with 68 additions and 2 deletions
+14 -2
View File
@@ -29,7 +29,9 @@ pub(super) fn seg_direction(seg: &PathSeg, t: f64) -> Vec2 {
}
/// The parameters in (0, 1) where the line from `at` to `seg` is square to it (`perpendicular`)
/// or grazes it, found by sampling the curve for a change of sign and bisecting.
/// or grazes it, found by sampling the curve for a change of sign and bisecting. A sample where
/// the line is exactly square or grazing, and its neighbors are not, is one of them too: no sign
/// changes on either side of it.
pub(super) fn touch_params(seg: &PathSeg, at: Point, perpendicular: bool) -> Vec<f64> {
const STEPS: usize = 24;
let f = |t: f64| {
@@ -41,7 +43,17 @@ pub(super) fn touch_params(seg: &PathSeg, at: Point, perpendicular: bool) -> Vec
for i in 1..=STEPS {
let t = i as f64 / STEPS as f64;
let cur = (t, f(t));
if prev.1.is_finite() && cur.1.is_finite() && prev.1 * cur.1 < 0.0 {
if cur.1 == 0.0 {
// Not at the end (t = 1), not where `at` lies on the curve or the direction is zero
// (a cusp), and not along a stretch that is zero throughout, such as a straight
// curve seen from its own line. The sign need not change: the tangent at an
// inflection touches zero without crossing it.
let isolated = |n: f64| n.is_finite() && n != 0.0;
let (v, d) = (seg.eval(t) - at, seg_direction(seg, t));
if i < STEPS && isolated(prev.1) && isolated(f((i + 1) as f64 / STEPS as f64)) && v.hypot() * d.hypot() > 1e-12 {
out.push(t);
}
} else if prev.1.is_finite() && cur.1.is_finite() && prev.1 * cur.1 < 0.0 {
let (mut lo, mut hi) = (prev, cur);
for _ in 0..40 {
let mid = 0.5 * (lo.0 + hi.0);
+54
View File
@@ -535,6 +535,60 @@ fn drawn_lines_land_perpendicular_to_segments_and_tangent_to_curves() {
}
}
/// A touch point that falls exactly on one of the samples `touch_params` takes (t = k / 24) is
/// found, once: the foot of the perpendicular from above a segment's middle, and the tangent
/// point of a line drawn from a free point to a circle (#7846). A line to an oval lands tangent
/// between samples, as before.
#[test]
fn touch_points_exactly_on_a_sample_are_found() {
use super::snap::touch_params;
use vectorcraft_doc::{Appearance, Node};
use vectorcraft_geom::kurbo::{CubicBez, Line, ParamCurve};
use vectorcraft_geom::shapes;
// (50, 40) above the segment (0, 0) to (100, 0): the foot is (50, 0), at t = 0.5 = 12 / 24.
let flat = PathSeg::Line(Line::new((0.0, 0.0), (100.0, 0.0)));
assert_eq!(touch_params(&flat, Point::new(50.0, 40.0), true), [0.5]);
assert_eq!(touch_params(&flat, Point::new(30.0, 40.0), true).len(), 1, "between samples, as before");
assert!(touch_params(&flat, Point::new(0.0, 40.0), true).is_empty(), "the ends stay out");
assert!(touch_params(&flat, Point::new(100.0, 40.0), true).is_empty(), "the ends stay out");
// From a point on the segment itself there is no line to be square.
assert!(touch_params(&flat, Point::new(50.0, 0.0), true).is_empty());
// A straight cubic seen from its own line runs along that line everywhere: no touch point.
let straight = PathSeg::Cubic(CubicBez::new((0.0, 0.0), (30.0, 0.0), (70.0, 0.0), (100.0, 0.0)));
assert!(touch_params(&straight, Point::new(-50.0, 0.0), false).is_empty());
// y = 400 (t - 0.5)^3 has its inflection at t = 0.5, and the tangent there (the x axis)
// touches zero without a change of sign; seen from (-100, 0) it is still found.
let wave = PathSeg::Cubic(CubicBez::new((0.0, -50.0), (100.0 / 3.0, 50.0), (200.0 / 3.0, -50.0), (100.0, 50.0)));
assert_eq!(touch_params(&wave, Point::new(-100.0, 0.0), false), [0.5]);
let (mut d, _) = doc_with_rect();
let l = d.layers[0].id;
let id = d.alloc_id();
let circle = shapes::ellipse(Rect::new(450.0, 450.0, 550.0, 550.0));
// The middle of the lower right quarter, and a point on the circle's tangent there.
let quarter = PathSeg::Cubic(circle.subpaths[0].segment(2));
let mid = quarter.eval(0.5);
let from = mid + Vec2::new(-64.0, 64.0);
assert!(((mid - Point::new(500.0, 500.0)).hypot() - 50.0).abs() < 0.1, "{mid:?}");
assert_eq!(touch_params(&quarter, from, false), [0.5]);
d.insert(Some(l), 1, Node::path(id, circle, Appearance::default_art())).unwrap();
// An oval (centre (500, 300), 100 by 50) seen from (300, 300): the tangent points are at
// x = 450, y = 300 -/+ 25 * sqrt(3), between samples.
let oval = d.alloc_id();
d.insert(Some(l), 2, Node::path(oval, shapes::ellipse(Rect::new(400.0, 250.0, 600.0, 350.0)), Appearance::default_art())).unwrap();
let (s, p) = (Selection::default(), paint());
let c = cx(&d, &s, &p);
let leave = Leave::segment(&c, from, false);
let (q, ov) = PointSnap::new(&c, || Targets::for_drawing(&d, &[], None, None)).snap_from(&c, mid + Vec2::new(1.5, 1.0), Some(&leave));
assert_eq!(labels(&ov), ["tangent"]);
assert!((q - mid).hypot() < 1e-9, "{q:?} {mid:?}");
assert_eq!(lines(&ov), vec![(from, q)]);
let start = Point::new(300.0, 300.0);
let leave = Leave::segment(&c, start, false);
let (q, ov) = PointSnap::new(&c, || Targets::for_drawing(&d, &[], None, None)).snap_from(&c, Point::new(452.0, 259.0), Some(&leave));
assert_eq!(labels(&ov), ["tangent"]);
assert!((q - Point::new(450.0, 300.0 - 25.0 * 3f64.sqrt())).hypot() < 0.2, "{q:?}");
}
/// A box dragged from a corner (Rectangle, Ellipse, ...) never snaps square to a segment, and
/// a busy spot does not hide the contact: every segment in reach is tried.
#[test]