diff --git a/crates/engine/src/cmd/addons/pathtools/tangents.rs b/crates/engine/src/cmd/addons/pathtools/tangents.rs index 1625bbe2e7..e45e736109 100644 --- a/crates/engine/src/cmd/addons/pathtools/tangents.rs +++ b/crates/engine/src/cmd/addons/pathtools/tangents.rs @@ -31,7 +31,7 @@ pub(super) fn specs() -> Vec { "Common Tangents", ["Object", "Plug-ins", "Path Precision Tools"], None, - "{tangents?: \"external\" (both paths on the same side of each line) | \"internal\" (the lines that run between them) | \"all\" (\"all\"), centerline?: bool (false: true also adds the line joining the two centers, an ellipse's center or a path's center of area), ids?: [id, id] (default: the selection)} add the common tangent lines of two convex closed paths as separate open paths, each from where it touches the first path to where it touches the second (the whole stretch where a straight side lies along it), stroked with the current stroke color and weight (black when the stroke is None) and unfilled, directly above the upper of the two, as one undo step; the new lines are selected. An ellipse drawn with the Ellipse tool, turned or scaled, is measured as an exact ellipse; any other path must be closed, have one outline, bend the same way all around and have at most 10000 segments. Separate paths have two external and two internal tangents; overlapping paths only external ones; one path inside the other has none. Paths that touch, from outside or from inside, share one tangent there instead: a line centered on the point where they touch and as long as the narrower path is wide along it; paths that meet at a corner or along a shared stretch of outline get one such line, at the middle of the directions that touch both. Paths count as touching when the gap between them is less than a billionth of the larger of their sizes and the distance between their centers. One of the two may be a point (a path of one anchor): the lines then run from the point to where they touch the path, two when the point lies outside it (one that lies along a straight side covers the whole side), one when the point lies on its outline (centered on the point and as long as the path is wide along it, or the whole straight side the point lies on), none when it lies inside; every `tangents` value gives the same lines, counted as internal when `tangents` is \"internal\" and as external otherwise, and the centerline runs from the point to the path's center; two points are refused. When there is no tangent of the kind asked for, nothing changes and the result has a `warning` → {ids, external, internal, centerline (its id or null), warning?}", + "{tangents?: \"external\" (both paths on the same side of each line) | \"internal\" (the lines that run between them) | \"all\" (\"all\"), centerline?: bool (false: true also adds the line joining the two centers, an ellipse's center or a path's center of area), ids?: [id, id] (default: the selection)} add the common tangent lines of two convex closed paths as separate open paths, each from where it touches the first path to where it touches the second (the whole stretch where a straight side lies along it), stroked with the current stroke color and weight (black when the stroke is None) and unfilled, directly above the upper of the two, as one undo step; the new lines are selected. An ellipse drawn with the Ellipse tool, turned or scaled, is measured as an exact ellipse; any other path must be closed, have one outline, bend the same way all around and have at most 10000 segments. Separate paths have two external and two internal tangents; overlapping paths only external ones; one path inside the other has none. Paths that touch, from outside or from inside, share one tangent there instead: a line centered on the point where they touch and as long as the narrower path is wide along it; paths that meet at a corner or along a shared stretch of outline get one such line, at the middle of the directions that touch both. Paths count as touching when the gap between them is less than a billionth of the larger of their sizes and the distance between their centers. One of the two may be a point (a path of one anchor): the lines then run from the point to where they touch the path, two when the point lies outside it (one that lies along a straight side covers the whole side), one when the point lies on its outline, that is, less than the touching tolerance from it, with the point's distance from the path's center in place of the distance between centers (centered on the point and as long as the path is wide along it, or the whole straight side the point lies on), none when it lies inside; every `tangents` value gives the same lines, counted as internal when `tangents` is \"internal\" and as external otherwise, and the centerline runs from the point to the path's center; two points are refused. When there is no tangent of the kind asked for, nothing changes and the result has a `warning` → {ids, external, internal, centerline (its id or null), warning?}", has_selection, common_tangents )] diff --git a/crates/pathops/src/addons/pathtools/tangents.rs b/crates/pathops/src/addons/pathtools/tangents.rs index 0a1b6ef715..655e84e311 100644 --- a/crates/pathops/src/addons/pathtools/tangents.rs +++ b/crates/pathops/src/addons/pathtools/tangents.rs @@ -1084,34 +1084,57 @@ mod tests { #[test] fn a_point_placed_on_a_turned_outline_has_one_tangent() { // A point worked out on the outline lies a rounding error inside or outside it, and still - // gets the one line there. - let one = |p: Point, o: &Outline| { + // gets the one line there, as long as the outline is wide along it. + let one = |p: Point, o: &Outline, samples: &[Point]| { let all = common_tangents(&Outline::point(p).unwrap(), o).unwrap(); assert_eq!(all.len(), 1, "{p:?}: {all:?}"); // The line runs through the point. let (a, d) = (all[0].from, all[0].to - all[0].from); assert!(d.hypot() > 1.0 && ((p - a).cross(d) / d.hypot()).abs() < 1e-6, "{p:?}: {all:?}"); + // Its length is the outline's width along it, measured on dense samples. + let u = d / d.hypot(); + let (lo, hi) = samples.iter().map(|s| s.to_vec2().dot(u)).fold((f64::MAX, f64::MIN), |(lo, hi), v| (lo.min(v), hi.max(v))); + assert!((d.hypot() - (hi - lo)).abs() < 1e-3, "{p:?}: {} against {}", d.hypot(), hi - lo); }; let m = ellipse_map(100.0, 50.0, 40.0, 15.0, 30.0); - let oval = Outline::ellipse(m).unwrap(); + let (oval, oval_samples) = (Outline::ellipse(m).unwrap(), sample_ellipse(m, 20_000)); for k in 0..37 { let a = 0.17 + k as f64 * TAU / 37.0; - one(m * Point::new(a.cos(), a.sin()), &oval); + one(m * Point::new(a.cos(), a.sin()), &oval, &oval_samples); } // A closed Bézier circle, turned and moved: on each anchor and partway along each segment. let turned = shapes::ellipse(Rect::new(-60.0, -60.0, 60.0, 60.0)).transformed(Affine::translate((37.3, -12.9)) * Affine::rotate(0.7)); let sp = turned.subpaths[0].clone(); - let path = Outline::convex(&sp).unwrap(); + let (path, path_samples) = (Outline::convex(&sp).unwrap(), sample_path(&sp, 5_000)); for an in sp.anchors.iter() { - one(an.p, &path); + one(an.p, &path, &path_samples); } for k in 0..sp.segment_count() { for t in [0.21, 0.5, 0.83] { - one(sp.segment(k).eval(t), &path); + one(sp.segment(k).eval(t), &path, &path_samples); } } } + #[test] + fn a_point_within_the_touching_tolerance_counts_as_on_the_outline() { + // The tolerance is a billionth of the larger of the circle's size and the point's distance + // from its center: closer than that, inside or outside, the point gets the one line there. + let round = circle(0.0, 0.0, 10.0); + let tol = 1e-9 * round.size().max(10.0); + let at = |dx: f64| common_tangents(&Outline::point(Point::new(10.0 + dx, 0.0)).unwrap(), &round).unwrap(); + for dx in [-0.5 * tol, 0.5 * tol] { + let on = at(dx); + assert_eq!(on.len(), 1, "{dx}: {on:?}"); + assert!(ends_within(&on[0], Point::new(10.0, -10.0), Point::new(10.0, 10.0), 1e-5), "{dx}: {on:?}"); + } + // Farther than that, a point inside gets none and a point outside gets two. + assert!(at(-100.0 * tol).is_empty()); + assert_eq!(at(100.0 * tol).len(), 2); + assert!(at(-1e-6).is_empty()); + assert_eq!(at(1e-6).len(), 2); + } + #[test] fn open_concave_empty_and_identical_outlines_are_refused() { let open = SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(10.0, 0.0), Point::new(10.0, 10.0)], false); diff --git a/crates/ui-egui/src/addons/pathtools/tests.rs b/crates/ui-egui/src/addons/pathtools/tests.rs index 3f7006e536..e714b85aa2 100644 --- a/crates/ui-egui/src/addons/pathtools/tests.rs +++ b/crates/ui-egui/src/addons/pathtools/tests.rs @@ -329,3 +329,45 @@ fn common_tangents_previews_cancels_and_runs_on_ok() { assert_eq!((steps(&app), count(&app)), (n, nodes)); assert!(app.ui.status.contains("inside the other"), "{}", app.ui.status); } + +/// Common Tangents… on a point and an ellipse: every Tangents choice previews the same two lines +/// from the point, OK keeps them as one step, and a point inside the ellipse changes nothing and +/// says why. +#[test] +fn common_tangents_on_a_point_gives_the_same_lines_for_every_choice() { + let mut app = app(); + let oval = app.run("shape.ellipse", json!({"x": 180.0, "y": 120.0, "width": 80.0, "height": 60.0})).unwrap()["id"].as_u64().unwrap(); + let point = + |app: &mut VectorcraftApp, x: f64, y: f64| app.run("path.create", json!({"anchors": [{"x": x, "y": y}]})).unwrap()["id"].as_u64().unwrap(); + let dot = point(&mut app, 40.0, 150.0); + app.run("select.set", json!({"ids": [oval, dot]})).unwrap(); + let steps = |app: &VectorcraftApp| app.session.doc().unwrap().history.undo.len(); + let count = |app: &VectorcraftApp| app.session.doc().unwrap().doc.node_count(); + let (n, nodes) = (steps(&app), count(&app)); + let open = |app: &mut VectorcraftApp| { + let entries = menus::menu_entries(app); + let item = entries.iter().find(|e| e.path == ["Object", "Plug-ins", "Path Precision Tools"] && e.label == "Common Tangents…").unwrap(); + assert!(item.enabled); + let (cmd, p) = menus::click_target(&item.label, item.command.as_deref().unwrap(), &item.params); + menus::invoke(app, &cmd, p); + }; + + open(&mut app); + crate::dialogs::set_field(app.ui.dialog.as_mut().unwrap(), "preview".into(), json!(true)); + for (_, kind) in super::TANGENT_KINDS { + crate::dialogs::set_field(app.ui.dialog.as_mut().unwrap(), "tangents".into(), json!(kind)); + painted_text(&mut app, |app, ui| crate::dialogs::show(app, ui.ctx())); + assert_eq!(count(&app), nodes + 2, "{kind}: the two lines from the point"); + } + crate::dialogs::confirm(&mut app).unwrap(); + assert_eq!((steps(&app), count(&app)), (n + 1, nodes + 2), "one undo step"); + + // A point inside the ellipse: nothing changes, and a message says why. + let inside = point(&mut app, 220.0, 150.0); + app.run("select.set", json!({"ids": [oval, inside]})).unwrap(); + let (n, nodes) = (steps(&app), count(&app)); + open(&mut app); + crate::dialogs::confirm(&mut app).unwrap(); + assert_eq!((steps(&app), count(&app)), (n, nodes)); + assert!(app.ui.status.contains("The point lies inside the path"), "{}", app.ui.status); +} diff --git a/docs/mcp.md b/docs/mcp.md index 7589d2ff1e..79ab9876d8 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -12734,13 +12734,15 @@ Commands: anchor: the lines then run from the point to where they touch the path, two when the point lies outside it (one that lies along a straight side covers the whole side), one when the point lies on its outline (centered on the point and as long as the path is wide along it, or the whole straight side the point lies on), and none when it - lies inside. Every `tangents` value gives the same lines, counted in `internal` when `tangents` is `internal` and - in `external` otherwise; the centerline runs from the point to the path's center. Two points are refused with - "select two closed paths". The lines are stroked with the current stroke color and weight (black when the stroke - is None), have no fill, go directly above the upper of the two paths and are selected; one undo step. When there - is no tangent of the kind asked for, nothing changes, and the result's `warning` says why. Common Tangents… - opens a dialog (Tangents, Centerline) with Preview. → `{ids, external, internal, centerline, warning?}` - (`centerline`: the centerline's id, or null). + lies inside. The point counts as on the outline when it is less than the touching tolerance from it, with the + point's distance from the path's center in place of the distance between centers. Every `tangents` value gives + the same lines, counted in `internal` when `tangents` is `internal` and in `external` otherwise; the centerline + runs from the point to the path's center. Two points are refused with "select two closed paths". The lines are + stroked with the current stroke color and weight (black when the stroke is None), have no fill, go directly above + the upper of the two paths and are selected; one undo step. When there is no tangent of the kind asked for, + nothing changes, and the result's `warning` says why. Common Tangents… opens a dialog (Tangents, Centerline) with + Preview; with a point selected, every Tangents choice gives the same lines. → `{ids, external, internal, + centerline, warning?}` (`centerline`: the centerline's id, or null). A parametric shape is an ordinary path. It keeps its numbers, with a fingerprint of the outline they drew, in one entry of its object data (`vc-pathtools-shape`, as `document.node` lists `attrs.data`), which SVG export leaves out; diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index a0ae1bcc7e..859deb9f5b 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2645,7 +2645,7 @@ A VectorCraft addition, so no area score moves. Object › Plug-ins › Cutting ## Path Precision Tools add-on: Common Tangents (2026-11-15) -A VectorCraft addition, so no area score moves. Object › Plug-ins › Path Precision Tools › Common Tangents… (`addon.pathtools.commonTangents`) adds the common tangent lines of two convex closed paths ([tangent lines to circles](https://en.wikipedia.org/wiki/Tangent_lines_to_circles)) as separate open paths: external, internal or all, with the line joining the two centers if asked. The rest is VectorCraft's own: an ellipse drawn with the Ellipse tool, turned or scaled, is measured as an exact ellipse, and any other path must be closed, have one outline and bend the same way all around. Each line runs between the points where it touches the two paths. Separate paths have four tangents, overlapping paths only external ones (two for ellipses), and a path inside the other has none, in which case nothing changes and a message says why. Paths that touch, from outside or from inside, share one tangent there instead: a line centered on the point where they touch and as long as the narrower path is wide along it. Since 2026-11-18 one of the two may be a point (a path of one anchor): the lines run from the point to where they touch the path, two from a point outside it, one at a point on its outline (centered on the point, or covering the straight side it lies on), and none from a point inside, with a message; the centerline runs from the point to the path's center. The lines take the current stroke and no fill; one undo step. The dialog has Preview. Tests: `addons::pathtools::tangents::tests` in `vectorcraft-pathops` (circles against the closed form, turned ellipses against a brute-force scan, nested, overlapping and touching circles, a convex Bézier path, flat sides, squares meeting at a corner, a curve touched beside an anchor, a point outside a turned ellipse, on a circle, inside it, beside a square and at its corner, refused outlines), `cmd::addons::pathtools::tangents::tests` in `vectorcraft-engine`, `common_tangents_previews_cancels_and_runs_on_ok` in `vectorcraft-ui-egui` and `command_sweep`. Commands: `docs/mcp.md` › Path Precision Tools add-on. +A VectorCraft addition, so no area score moves. Object › Plug-ins › Path Precision Tools › Common Tangents… (`addon.pathtools.commonTangents`) adds the common tangent lines of two convex closed paths ([tangent lines to circles](https://en.wikipedia.org/wiki/Tangent_lines_to_circles)) as separate open paths: external, internal or all, with the line joining the two centers if asked. The rest is VectorCraft's own: an ellipse drawn with the Ellipse tool, turned or scaled, is measured as an exact ellipse, and any other path must be closed, have one outline and bend the same way all around. Each line runs between the points where it touches the two paths. Separate paths have four tangents, overlapping paths only external ones (two for ellipses), and a path inside the other has none, in which case nothing changes and a message says why. Paths that touch, from outside or from inside, share one tangent there instead: a line centered on the point where they touch and as long as the narrower path is wide along it. Since 2026-11-18 one of the two may be a point (a path of one anchor): the lines run from the point to where they touch the path, two from a point outside it, one at a point on its outline or less than the touching tolerance from it (centered on the point, or covering the straight side it lies on), and none from a point inside, with a message; the centerline runs from the point to the path's center, and every Tangents choice gives the same lines. The lines take the current stroke and no fill; one undo step. The dialog has Preview. Tests: `addons::pathtools::tangents::tests` in `vectorcraft-pathops` (circles against the closed form, turned ellipses against a brute-force scan, nested, overlapping and touching circles, a convex Bézier path, flat sides, squares meeting at a corner, a curve touched beside an anchor, a point outside a turned ellipse, on a circle, inside it, beside a square and at its corner, on a turned outline, within the touching tolerance, refused outlines), `cmd::addons::pathtools::tangents::tests` in `vectorcraft-engine`, `common_tangents_previews_cancels_and_runs_on_ok` and `common_tangents_on_a_point_gives_the_same_lines_for_every_choice` in `vectorcraft-ui-egui` and `command_sweep`. Commands: `docs/mcp.md` › Path Precision Tools add-on. ## Path Precision Tools add-on: Divide into Dashes (2026-11-01)