mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-02 20:18:38 -05:00
Vector Halftone: write each round dot with four curves again
The cell-clipped dots split every elliptical arc at 45 degrees, and the quadrants were joined with a zero-length line where they met on an axis. A round dot that stays inside its cell had 8 curves and 3 extra anchors instead of 4 curves, and the halftone's SVG export grew to about twice its size. - Arcs are split at 90 degrees, one curve per quarter turn. - Quadrants and spot corners that meet at the same point are joined without a line, so the join adds no anchor. Tests: an unclipped Circle and Ellipse dot is 4 curves and no line, and no dot shape repeats an anchor where its quadrants or corners meet.
This commit is contained in:
1 parent
a735b49248
commit
c679af091d
1 file changed
+56
-15
@@ -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<CubicBez>, 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<CubicBez>, first: bool| {
|
||||
let mut end: Option<Point> = 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<CubicBez>| {
|
||||
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<CubicBez>, first: bool| {
|
||||
let mut end: Option<Point> = None;
|
||||
let mut add = |curves: Vec<CubicBez>| {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user