ARRAYPATH: no extra item where a closed path closes, by spacing

The JSON spacing form made floor(length / spacing) + 1 items on every path,
so on a closed one the last sat on top of the first (or nearer to it than
the spacing). AutoCAD 2024 fills a closed path with floor(length / spacing)
items and an open one with one more, at its end (measured with Fill entire
path on a closed and an open square and a circle). The spacing form now does
the same, letting length / spacing fall 1e-9 short for rounding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2ez4gcx
2026-11-04 19:46:34 +07:00
co-authored by Claude Opus 5.5
parent 2099288f54
commit a113667951
2 changed files with 33 additions and 1 deletions
+7 -1
View File
@@ -26,7 +26,13 @@ pub(crate) fn arraypath(
return Err(other("Select objects to array."));
}
let (n, step) = match (count, spacing) {
(_, Some(sp)) if sp.is_finite() && sp > 0.0 => (((l / sp).floor() as usize).saturating_add(1), sp),
// As AutoCAD 2024 fills a path by distance: floor(length / spacing) items on a closed path,
// so none lands on or near the first where the path closes, and one more, at the end, on
// an open one (measured). The 1e-9 lets a length that comes out a hair short still fit.
(_, Some(sp)) if sp.is_finite() && sp > 0.0 => {
let fit = (l / sp + 1e-9).floor() as usize;
(if c.closed { fit.max(1) } else { fit.saturating_add(1) }, sp)
}
(Some(n), _) => {
let n = n.max(1);
(n, if c.closed { l / n as f64 } else { l / (n.max(2) - 1) as f64 })
+26
View File
@@ -639,6 +639,32 @@ fn arraypath_json_and_command_line() {
assert!(matches!(last(&s), EntityKind::Line(l) if near(l.a.xy(), -5.0, 20.0) && near(l.b.xy(), -6.0, 20.0)));
assert!(s.execute("arraypath", &json!({"handles": [dot], "path": dot})).is_err());
// By spacing, AutoCAD 2024 fills a closed path with floor(length / spacing) items and an open
// one with one more, at the end (measured on a 100 square and on the same square open).
let square = [[0, 0], [100, 0], [100, 100], [0, 100]];
let r400 = 400.0 / std::f64::consts::TAU;
for (closed, spacing, items) in
[(true, 100, 4), (true, 30, 13), (true, 200, 2), (true, 400, 1), (true, 399, 1), (false, 100, 4), (false, 30, 11), (false, 300, 2)]
{
let mut s = Session::new();
let dot = h(&s.execute("circle", &json!({"center": [0, 0], "radius": 1})).unwrap());
let path = h(&s.execute("pline", &json!({"points": square, "closed": closed})).unwrap());
let r = s.execute("arraypath", &json!({"handles": [dot], "path": path, "spacing": spacing})).unwrap();
assert_eq!(r["handles"].as_array().map(Vec::len), Some(items - 1), "closed {closed}, spacing {spacing}");
}
// A circle 400 round: 4 items at 100, none on the first (AutoCAD 2024, measured).
let mut s = Session::new();
let dot = h(&s.execute("circle", &json!({"center": [0, 0], "radius": 1})).unwrap());
let path = h(&s.execute("circle", &json!({"center": [0, r400], "radius": r400})).unwrap());
let r = s.execute("arraypath", &json!({"handles": [dot], "path": path, "spacing": 100})).unwrap();
assert_eq!(r["handles"].as_array().map(Vec::len), Some(3));
// Three sides of 0.35 add up to a hair under 3 × 0.35; the item at the end still fits.
let mut s = Session::new();
let dot = h(&s.execute("circle", &json!({"center": [0, 0], "radius": 0.01})).unwrap());
let path = h(&s.execute("pline", &json!({"points": [[0, 0], [0.35, 0], [0.35, 0.35], [0, 0.35]]})).unwrap());
let r = s.execute("arraypath", &json!({"handles": [dot], "path": path, "spacing": 0.35})).unwrap();
assert_eq!(r["handles"].as_array().map(Vec::len), Some(3));
let mut s = Session::new();
s.execute("circle", &json!({"center": [0, 1], "radius": 0.25})).unwrap();
s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap();