From 17807ab6dcaab411a98d6253d03d20b272ccc2cb Mon Sep 17 00:00:00 2001 From: semmelrinde <124386289+semmelrinde@users.noreply.github.com> Date: Wed, 28 Oct 2026 23:11:05 +0000 Subject: [PATCH] Sketch MidPoint: refuse a line's own end as its midpoint (#8562) A line's own end held at its middle shrinks the line to a point. The solve accepted that, and a later angle, tangent or parallel on the line had no direction to hold: its points went millions of millimetres away while the sketch still reported solved. MidPoint now refuses the pick, saying why, and leaves the sketch as it was. Constraints already in a design are untouched (only the command refuses). Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/sketch.rs | 14 +++++++++++++- crates/engine/src/cmd/sketch_constraints_tests.rs | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 6643d3514..fe626d4c7 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -2106,7 +2106,7 @@ fn c_midpoint(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.constraint.midpoint"; let p = &super::sketch_project::project_operands(s, p, cmd, &["a", "b", "point", "line"])?; let p = &line_stays(s, p, cmd); - constrain(s, p, cmd, |sk| { + let pick = |sk: &mut Sketch| -> Result { if p.get("a").is_none() && p.get("b").is_none() { return Ok(ConstraintKind::Midpoint { p: point_ref(sk, p.get("point"), cmd, "point")?, l: curve_ref(sk, p.get("line"), cmd, "line")? }); } @@ -2139,6 +2139,18 @@ fn c_midpoint(s: &mut Session, p: &Value) -> Result { } _ => Err(bad(cmd, "MidPoint needs a point and a line, or two lines")), } + }; + constrain(s, p, cmd, |sk| { + let k = pick(sk)?; + // A line's own end at its middle shrinks the line to a point; an angle, tangent or + // parallel on it afterwards had no direction to hold and threw points millions of + // millimetres away. + if let ConstraintKind::Midpoint { p: q, l } = k + && matches!(sk.curves.get(l).map(|c| &c.kind), Some(CurveKind::Line { a, b }) if *a == q || *b == q) + { + return Err(bad(cmd, "a line's own end can't be its midpoint: the line would shrink to a point")); + } + Ok(k) }) } diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs index 029a24af6..98f89ab3a 100644 --- a/crates/engine/src/cmd/sketch_constraints_tests.rs +++ b/crates/engine/src/cmd/sketch_constraints_tests.rs @@ -979,6 +979,10 @@ fn midpoint_rejects_wrong_picks_without_changing_the_sketch() { json!({"a":"l1","b":"missing"}), json!({"a":"l1"}), json!({"a":12,"b":"l1"}), + // The line's own end: it would shrink the line to a point. + json!({"a":"l1.end","b":"l1"}), + json!({"a":"l1","b":"l1.start"}), + json!({"point":"l1.end","line":"l1"}), ] { let before = s.doc.to_json(); let undo = s.undo.len();