Files
vectorcraft/crates/engine/src/tests/tangent_line.rs
T
dotCooCoo 6c3a092996 Line Segment: a line from one curve to another snaps to their common tangent
With Smart Guides and Snap to Tangent on, a Line Segment drag pressed
anywhere on a convex closed path with curves and dragged to within the
snapping tolerance of another such path lands on a common tangent of the
two. Both ends move to where the line touches the paths, the start sliding
along the first path. The line is the tangent whose touch point on the
second path is nearest the pointer, so moving the pointer around that path
switches between the outer tangent and the one crossing between the paths.
Each end shows the "tangent" label when Anchor/Path Labels is on, and the
line is drawn as a guide when Alignment Guides is on.

Before this change the start of a Line Segment drag always stayed where it
was pressed. It still does when Shift, Alt or Space is held, when an
anchor, center or crossing lies nearer the pointer than the second path,
when the press snapped to an anchor, center or crossing off the first path,
when Snap to Tangent is off, when Snap to Grid or pixel snapping places
drawn points, or when either path bends inward somewhere or has more than
512 segments.

The tangents come from the Common Tangents geometry. Each pointer move
looks at the 16 nearest closed subpaths within the snapping tolerance and
keeps the tangents of up to 64 pairs for the rest of the drag.
2026-11-15 16:19:42 -08:00

41 lines
2.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Snap to Tangent with the Line Segment tool: a line dragged from one circle to another lands on
//! their common tangent, drawn by `shape.line` as one undo step.
use serde_json::json;
use vectorcraft_doc::NodeKind;
use vectorcraft_geom::{Point, Vec2};
use vectorcraft_tools::{PointerEvent, PointerKind};
use crate::*;
use PointerKind::{Down, Drag, Up};
#[test]
fn a_line_segment_drag_draws_the_common_tangent_as_one_undo_step() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 500, "height": 500})).unwrap();
s.execute("shape.ellipse", &json!({"x": 60, "y": 210, "width": 80, "height": 80})).unwrap();
s.execute("shape.ellipse", &json!({"x": 240, "y": 190, "width": 120, "height": 120})).unwrap();
s.execute("select.none", &json!({})).unwrap();
let count = |s: &Session| s.doc().unwrap().doc.layers[0].children().map_or(0, |c| c.len());
assert_eq!(count(&s), 2);
// Circles about (100, 250), radius 40, and (300, 250), radius 60: the outer tangent above them
// has the normal (-0.1, -√0.99) and touches them at center + radius × normal.
let n = Vec2::new(-0.1, -(0.99f64).sqrt());
let (on_a, on_b) = (Point::new(100.0, 250.0) + n * 40.0, Point::new(300.0, 250.0) + n * 60.0);
s.select_tool("lineSegment", ViewInfo::default()).unwrap();
let to = on_b + Vec2::new(0.5, 0.7);
for (k, x, y) in [(Down, 100.0, 210.0), (Drag, 200.0, 200.0), (Drag, to.x, to.y), (Up, to.x, to.y)] {
s.pointer(&PointerEvent::new(k, x, y), ViewInfo::default()).unwrap();
}
assert_eq!(count(&s), 3);
let id = *s.doc().unwrap().selection.objects.first().unwrap();
let NodeKind::Path { path, .. } = &s.doc().unwrap().doc.node(id).unwrap().kind else { panic!("a path") };
let ends: Vec<Point> = path.subpaths[0].anchors.iter().map(|a| a.p).collect();
assert_eq!(ends.len(), 2);
// Within what the drawn circles' 0.03% radius error allows.
assert!((ends[0] - on_a).hypot() < 0.15 && (ends[1] - on_b).hypot() < 0.15, "{ends:?}, want {on_a:?} {on_b:?}");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(count(&s), 2, "one undo removes the line");
}