Sketch drawing: a point snapped to a line's midpoint stays there ("mid:<line>" point arguments add a MidPoint constraint)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AS5Tn3xieewUSg3uxrMVdL
This commit is contained in:
1 parent
d729918034
commit
063d55bdb3
4 files changed
+36
-8
No files matched your search
@@ -35,12 +35,13 @@ pub fn refine_snap(
|
||||
sk: &solvecraft_engine::sketch::Sketch,
|
||||
plane: &solvecraft_engine::geom::Plane,
|
||||
lp: Vec2,
|
||||
) -> Option<Vec2> {
|
||||
) -> Option<(Vec2, Option<String>)> {
|
||||
// Millimetres per pixel here.
|
||||
let (a, b) = (proj.to_screen(plane.to_world(lp))?, proj.to_screen(plane.to_world(lp + Vec2::X))?);
|
||||
let px = 1.0 / (a.distance(b) as f64).max(1e-9);
|
||||
let tol = 7.0 * px;
|
||||
let mut best: Option<(f64, &'static str, Vec2)> = None;
|
||||
let mut mid_of: Option<String> = None;
|
||||
for (i, c) in sk.curves.iter().enumerate() {
|
||||
if let solvecraft_engine::sketch::CurveKind::Line { .. } = c.kind
|
||||
&& let Some(solvecraft_engine::sketch::Shape::Line { a, b }) = sk.shape(i)
|
||||
@@ -49,6 +50,7 @@ pub fn refine_snap(
|
||||
let d = m.dist(lp);
|
||||
if d < tol && best.is_none_or(|x| d < x.0) {
|
||||
best = Some((d, "Mid", m));
|
||||
mid_of = Some(c.id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,7 +78,8 @@ pub fn refine_snap(
|
||||
}
|
||||
}
|
||||
SNAP.with(|s| *s.borrow_mut() = best.map(|(_, l, p)| (l, p)));
|
||||
best.map(|(_, _, p)| p)
|
||||
// A midpoint snap is a point argument the commands understand ("mid:<line>").
|
||||
best.map(|(_, l, p)| (p, if l == "Mid" { mid_of.map(|m| format!("mid:{m}")) } else { None }))
|
||||
}
|
||||
|
||||
/// A click on a constraint glyph selects it (true: the click was used).
|
||||
|
||||
@@ -799,7 +799,7 @@ pub fn sketch_point_at(app: &SolveApp, proj: &Proj, pos: Pos2) -> Option<(Vec2,
|
||||
let (minor, _) = grid_step(app.cam.half_height());
|
||||
let step = minor / 10.0;
|
||||
if let Some(p) = crate::sketch_tools::refine_snap(app, proj, &ss.sketch, &ss.plane, lp) {
|
||||
return Some((p, None));
|
||||
return Some(p);
|
||||
}
|
||||
Some((Vec2::new((lp.x / step).round() * step, (lp.y / step).round() * step), None))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user