diff --git a/crates/engine/src/cmd/addons/arrange/artboard_guides.rs b/crates/engine/src/cmd/addons/arrange/artboard_guides.rs index d849047fb9..982b3cd757 100644 --- a/crates/engine/src/cmd/addons/arrange/artboard_guides.rs +++ b/crates/engine/src/cmd/addons/arrange/artboard_guides.rs @@ -117,13 +117,17 @@ fn traces(path: &PathData, corners: &[Point; 4]) -> bool { if (0..n).any(|i| !straight(&sp.anchors[i], &sp.anchors[(i + 1) % n])) { return false; } + // A last anchor on the first one only closes the path: the check above has kept its handles on that + // point, so the corners are read without it. + let m = if sp.anchors[n - 1].p == sp.anchors[0].p { n - 1 } else { n }; let points: Vec = sp .anchors .iter() + .take(m) .enumerate() .filter_map(|(i, a)| { - let prev = sp.anchors[(i + sp.anchors.len() - 1) % sp.anchors.len()].p; - let next = sp.anchors[(i + 1) % sp.anchors.len()].p; + let prev = sp.anchors[(i + m - 1) % m].p; + let next = sp.anchors[(i + 1) % m].p; let interior = (prev.x == a.p.x && a.p.x == next.x && a.p.y > prev.y.min(next.y) && a.p.y < prev.y.max(next.y)) || (prev.y == a.p.y && a.p.y == next.y && a.p.x > prev.x.min(next.x) && a.p.x < prev.x.max(next.x)); (!interior).then_some(a.p) diff --git a/crates/engine/src/cmd/addons/arrange/artboard_guides/tests.rs b/crates/engine/src/cmd/addons/arrange/artboard_guides/tests.rs index 5152dcffe9..492af2a371 100644 --- a/crates/engine/src/cmd/addons/arrange/artboard_guides/tests.rs +++ b/crates/engine/src/cmd/addons/arrange/artboard_guides/tests.rs @@ -198,6 +198,30 @@ fn collinear_midpoints_do_not_add_another_guide_or_history_step() { } } +#[test] +fn a_closing_anchor_on_the_first_one_does_not_add_another_guide_or_history_step() { + let mut s = session(); + let r = s.execute(COMMAND, &json!({})).unwrap(); + let [id] = ids(&r)[..] else { panic!("one guide: {r}") }; + let b = board(&s, 0); + let corners = [Point::new(b.x0, b.y0), Point::new(b.x1, b.y0), Point::new(b.x1, b.y1), Point::new(b.x0, b.y1)]; + for reverse in [false, true] { + let mut points = corners.to_vec(); + if reverse { + points.reverse(); + } + points.push(points[0]); + let d = Arc::make_mut(&mut s.doc_mut().unwrap().doc); + let NodeKind::Path { path, .. } = &mut d.node_mut(id).unwrap().kind else { panic!("guide path") }; + *path = PathData::single(SubPath::polyline(&points, true)); + let (before, n) = (s.doc().unwrap().doc.clone(), steps(&s)); + let again = s.execute(COMMAND, &json!({})).unwrap(); + assert_eq!(again, json!({"ids": [], "layer": r["layer"], "skipped": 1}), "reverse: {reverse}"); + assert_eq!(steps(&s), n); + assert_eq!(s.doc().unwrap().doc, before); + } +} + #[test] fn handles_along_a_side_still_match_and_handles_off_it_do_not() { let mut s = session();