From e89a8f56c14b5670cdbc4bd3ea2dee58537b7028 Mon Sep 17 00:00:00 2001 From: dudedagger42 <76926918+dudedagger42@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:49:49 +0000 Subject: [PATCH] MidPoint: the point goes to the line's middle, whichever is picked first (#5011) MidPoint on a point and a line keeps the first pick still and moves the second. With the point picked first (an end of one line, then the line it should sit in the middle of), the line turned about its far end to bring its middle to the point: a line from (0,0) to (20,0) went to (0,0)-(10,10) for a point at (5,5). A point and a line now put the line first, so the point moves and the line stays; the line still comes to the point when the point can't move. Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/sketch.rs | 21 ++++++++++++++++ .../src/cmd/sketch_constraints_tests.rs | 25 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index eef0f704e..2062fc7b1 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -1992,6 +1992,7 @@ fn c_collinear(s: &mut Session, p: &Value) -> Result { } fn c_midpoint(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.constraint.midpoint"; + let p = &line_stays(s, p, cmd); constrain(s, p, cmd, |sk| { 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")? }); @@ -2028,6 +2029,26 @@ fn c_midpoint(s: &mut Session, p: &Value) -> Result { }) } +/// A point and a line picked for MidPoint, the point first: the line first instead, so the point +/// goes to the line's middle and the line stays (the first pick stays, see `apply_in_order`). +/// Picked the other way, the line turned about its far end to bring its middle to the point (an +/// end of another line clicked, then the line it should sit in the middle of). The line still +/// moves when the point can't. +fn line_stays(s: &Session, p: &Value, cmd: &str) -> Value { + let swapped = (|| { + let sk = s.doc.sketch(target_sketch(s, p, cmd).ok()?).ok()?; + let (a, b) = (p.get("a")?.as_str()?, p.get("b")?.as_str()?); + let line = sk.curve_index(b).filter(|c| matches!(sk.curves.get(*c).map(|c| &c.kind), Some(CurveKind::Line { .. }))); + (sk.curve_index(a).is_none() && sk.resolve_point(a).is_some() && line.is_some()).then(|| { + let mut q = p.clone(); + q["a"] = json!(b); + q["b"] = json!(a); + q + }) + })(); + swapped.unwrap_or_else(|| p.clone()) +} + /// Point `q` is already tied to the middle of line `l`. fn existing_midpoint_of(sk: &Sketch, q: usize, l: usize) -> bool { sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Midpoint { p, l: line } if p == q && line == l) && !c.driven) diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs index 1b2306ff9..87f1eb451 100644 --- a/crates/engine/src/cmd/sketch_constraints_tests.rs +++ b/crates/engine/src/cmd/sketch_constraints_tests.rs @@ -1109,3 +1109,28 @@ fn equal_over_a_list_refuses_bad_lists_and_conflicts_without_changing_the_sketch assert!(s.execute("sketch.constraint.equal", &json!({"curves": ["c1", "c2", "c3"]})).is_err()); assert_eq!(s.doc.to_json(), before); } + +/// MidPoint on a point and a line, picked either way round: the point goes to the line's +/// middle and the line stays. Picked point first, the line used to turn about its far end to +/// bring its middle to the point (the end of another line clicked, then the line). +#[test] +fn midpoint_moves_the_point_whichever_is_picked_first() { + for (a, b) in [("l2.start", "l1"), ("l1", "l2.start")] { + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points":[[0,0],[20,0]],"infer":false,"connect":false})); + run(&mut s, "sketch.line", json!({"points":[[5,5],[15,15]],"infer":false,"connect":false})); + let stays = line_ends(&sketch(&s), "l1"); + run(&mut s, "sketch.constraint.midpoint", json!({"a": a, "b": b})); + let sk = sketch(&s); + assert_eq!(line_ends(&sk, "l1"), stays, "{a}, {b}: the line stays"); + let (start, _) = line_ends(&sk, "l2"); + assert!(start.dist(Vec2::new(10.0, 0.0)) < 1e-6, "{a}, {b}: the point is at the middle: {start:?}"); + } + // A point that can't move: the line comes to it instead. + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points":[[0,0],[20,0]],"infer":false,"connect":false})); + run(&mut s, "sketch.point", json!({"point":[5,5],"id":"p"})); + run(&mut s, "sketch.constraint.fix", json!({"entities":["p"],"fixed":true})); + run(&mut s, "sketch.constraint.midpoint", json!({"a":"p","b":"l1"})); + assert!(line_midpoint(&sketch(&s), "l1").dist(Vec2::new(5.0, 5.0)) < 1e-6); +}