diff --git a/crates/effects/src/addons/halftone.rs b/crates/effects/src/addons/halftone.rs index d371bfb363..4f19f09a5a 100644 --- a/crates/effects/src/addons/halftone.rs +++ b/crates/effects/src/addons/halftone.rs @@ -173,13 +173,14 @@ fn curves_area(curves: &[CubicBez]) -> f64 { curves.iter().map(|c| c.signed_area()).sum() } -/// Cubic curves along the ellipse with semi-axes `a`, `b` from angle `t0` to `t1`. +/// Cubic curves along the ellipse with semi-axes `a`, `b` from angle `t0` to `t1`, one for each +/// quarter turn or part of one. fn ellipse_arc(out: &mut Vec, a: f64, b: f64, t0: f64, t1: f64) { if t1 <= t0 { return; } let at = |th: f64| Point::new(a * th.cos(), b * th.sin()); - let n = ((t1 - t0) / std::f64::consts::FRAC_PI_4).ceil().max(1.0); + let n = ((t1 - t0) / std::f64::consts::FRAC_PI_2).ceil().max(1.0); let d = (t1 - t0) / n; let k = 4.0 / 3.0 * (d / 4.0).tan(); for i in 0..n as usize { @@ -294,20 +295,27 @@ fn spot(t: f64, h: f64, kind: SpotKind) -> BezPath { return four_quadrants(&spot_quadrant(t, h, kind)); } let mut bp = BezPath::new(); - let mut add = |curves: Vec, first: bool| { + let mut end: Option = None; + // Corners that meet at a point on the cell's side are joined without an anchor of their own. + let mut add = |curves: Vec| { for (k, c) in curves.iter().enumerate() { if k == 0 { - if first { bp.move_to(c.p0) } else { bp.line_to(c.p0) } + match end { + None => bp.move_to(c.p0), + Some(e) if e.distance(c.p0) < 1e-9 => {} + Some(_) => bp.line_to(c.p0), + } } bp.curve_to(c.p1, c.p2, c.p3); + end = Some(c.p3); } }; // Round the corners counterclockwise, each one's hole the dot of the opposite tone. let q1 = spot_quadrant(1.0 - t.min(1.0), h, kind); - add(reversed(&mapped(&q1, |p| Point::new(h - p.x, h - p.y))), true); - add(mapped(&q1, |p| Point::new(p.x - h, h - p.y)), false); - add(reversed(&mapped(&q1, |p| Point::new(p.x - h, p.y - h))), false); - add(mapped(&q1, |p| Point::new(h - p.x, p.y - h)), false); + add(reversed(&mapped(&q1, |p| Point::new(h - p.x, h - p.y)))); + add(mapped(&q1, |p| Point::new(p.x - h, h - p.y))); + add(reversed(&mapped(&q1, |p| Point::new(p.x - h, p.y - h)))); + add(mapped(&q1, |p| Point::new(h - p.x, p.y - h))); bp.close_path(); bp } @@ -410,21 +418,28 @@ fn cell_dot(t: f64, h: f64, kind: CellKind) -> PathData { } /// The closed path through `q1` (from the x axis to the y axis) and its mirror images in the other -/// three quadrants, counterclockwise. +/// three quadrants, counterclockwise. Quadrants that meet on an axis are joined without an anchor +/// of their own at the join. fn four_quadrants(q1: &[CubicBez]) -> BezPath { let mut bp = BezPath::new(); - let mut add = |curves: Vec, first: bool| { + let mut end: Option = None; + let mut add = |curves: Vec| { for (k, c) in curves.iter().enumerate() { if k == 0 { - if first { bp.move_to(c.p0) } else { bp.line_to(c.p0) } + match end { + None => bp.move_to(c.p0), + Some(e) if e.distance(c.p0) < 1e-9 => {} + Some(_) => bp.line_to(c.p0), + } } bp.curve_to(c.p1, c.p2, c.p3); + end = Some(c.p3); } }; - add(q1.to_vec(), true); - add(reversed(&mapped(q1, |p| Point::new(-p.x, p.y))), false); - add(mapped(q1, |p| Point::new(-p.x, -p.y)), false); - add(reversed(&mapped(q1, |p| Point::new(p.x, -p.y))), false); + add(q1.to_vec()); + add(reversed(&mapped(q1, |p| Point::new(-p.x, p.y)))); + add(mapped(q1, |p| Point::new(-p.x, -p.y))); + add(reversed(&mapped(q1, |p| Point::new(p.x, -p.y)))); bp.close_path(); bp } @@ -731,6 +746,7 @@ mod tests { use super::*; use vectorcraft_doc::Effect; use vectorcraft_geom::Shape; + use vectorcraft_geom::kurbo::PathEl; /// A black square's samples: `side` points of black from (10, 10), white around it. fn black_square(side: f64, h: &Halftone) -> (Samples, Rect) { @@ -833,6 +849,31 @@ mod tests { } } + #[test] + fn a_dot_inside_its_cell_is_four_curves() { + for shape in [DotShape::Circle, DotShape::Ellipse] { + let bp = shape.path(0.2, 10.0).to_bezpath(); + let curves = bp.elements().iter().filter(|el| matches!(el, PathEl::CurveTo(..))).count(); + let lines = bp.elements().iter().filter(|el| matches!(el, PathEl::LineTo(..))).count(); + assert_eq!((curves, lines), (4, 0), "{shape:?}: {:?}", bp.elements()); + } + } + + #[test] + fn dot_quadrants_join_without_a_repeated_anchor() { + for shape in [DotShape::Circle, DotShape::Ellipse, DotShape::Diamond, DotShape::Euclidean, DotShape::Elliptical] { + for t in [0.2, 0.45, 0.55, 0.6, 0.85] { + for sp in &shape.path(t, 10.0).subpaths { + let n = sp.anchors.len(); + for i in 0..n { + let (a, b) = (sp.anchors[i].p, sp.anchors[(i + 1) % n].p); + assert!(a.distance(b) > 1e-6, "{shape:?} at {t}: {:?}", sp.anchors); + } + } + } + } + } + #[test] fn euclidean_dots_meet_corner_to_corner_at_half() { let s = DotShape::Euclidean;