From 00e3d4a92106ac214013fce3b110fe00d80dd4bf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 17:58:22 -0800 Subject: [PATCH] Shapes: the Curvature Circle's other-side circle on an arc touches the path at the clicked point On a cubic that counts as a circular arc but strays slightly from the arc's circle, the circle on the other side was placed against the arc's circle, a few points off the path. It now sits one radius out from the point on the path. The Undo test also checks that the click after Undo draws the first circle's own circle. --- crates/engine/src/tests/curvature_circle.rs | 2 ++ crates/geom/src/curvature.rs | 29 ++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/tests/curvature_circle.rs b/crates/engine/src/tests/curvature_circle.rs index 59d3892e58..e862a03aaf 100644 --- a/crates/engine/src/tests/curvature_circle.rs +++ b/crates/engine/src/tests/curvature_circle.rs @@ -221,4 +221,6 @@ fn the_next_click_passes_over_the_circle_the_tool_drew() { let count = object_count(&s); click(&mut s, Mods::default(), 280.0, 241.0); assert_eq!(object_count(&s), count + 1, "the circle drawn first is within reach again"); + let third = *s.doc().unwrap().selection.objects.first().unwrap(); + assert!(bounds_are(&s, third, Rect::new(100.0, 200.0, 300.0, 400.0)), "the click drew the first circle's own circle"); } diff --git a/crates/geom/src/curvature.rs b/crates/geom/src/curvature.rs index 1c88328fd5..304f20557e 100644 --- a/crates/geom/src/curvature.rs +++ b/crates/geom/src/curvature.rs @@ -46,8 +46,9 @@ const SMOOTH_TURN: f64 = 0.5 * std::f64::consts::PI / 180.0; /// The circle that touches segment `c` at `t` (0 to 1) with its curvature there, on the side the /// segment bends toward, or on the other side with `convex`. A segment that is the usual cubic of -/// a circular arc of 90° or less gives the circle of that arc: on the concave side it is the arc's -/// own circle, and it touches the arc's circle at the point nearest `c` at `t`. A `t` outside 0 to +/// a circular arc of 90° or less gives a circle of that arc's radius: on the concave side it is the +/// arc's own circle, and it touches the arc's circle at the point nearest `c` at `t`; on the convex +/// side it touches `c` at `t`, its center one radius out from there. A `t` outside 0 to /// 1 and a segment whose points are not finite give [`NoCircle::Cusp`]. pub fn curvature_circle(c: CubicBez, t: f64, convex: bool) -> Result { let finite = [c.p0, c.p1, c.p2, c.p3].iter().all(|p| p.is_finite()); @@ -62,9 +63,13 @@ pub fn curvature_circle(c: CubicBez, t: f64, convex: bool) -> Result { @@ -225,6 +230,24 @@ mod tests { } } + /// A quarter circle of radius 10,000 whose handles are 0.09% longer than the usual cubic's still + /// counts as the arc, and its middle lies a few points off the arc's circle: on the convex side + /// the circle still touches the segment at that point, one radius from its center. + #[test] + fn the_convex_circle_of_a_near_arc_touches_the_segment() { + let r = 10_000.0; + let k = 4.0 / 3.0 * (std::f64::consts::FRAC_PI_8).tan() * r * 1.0009; + let c = CubicBez::new(Point::new(r, 0.0), Point::new(r, k), Point::new(k, r), Point::new(0.0, r)); + assert!(circular_arc(c).is_some()); + let at = c.eval(0.5); + assert!((at.distance(Point::ORIGIN) - r).abs() > 1.0, "the middle lies off the arc's circle"); + let got = curvature_circle(c, 0.5, true).unwrap(); + assert!(got.nominal); + assert_eq!(got.at, at); + assert!((got.center.distance(at) - got.radius).abs() < 1e-6, "{got:?}"); + assert!(got.center.distance(Point::ORIGIN) > got.radius, "the center is on the side away from the arc's"); + } + /// The cubic's own radius of curvature on a quarter circle's segment is about 2.2% larger than /// the circle's at its ends and 0.6% larger at its middle: the nominal radius is the circle's. #[test]