Merge fix/halftone-merged-dot-edges into fix/halftone-grid-styles

This commit is contained in:
dotCooCoo committed 2026-12-01 06:02:50 -08:00
commit 4f44d6469e
1 file changed
+146 -22
+146 -22
View File
@@ -111,16 +111,10 @@ impl DotShape {
fn path(self, t: f64, s: f64) -> PathData {
let square = |side: f64| shapes::rectangle(Rect::from_center_size(Point::ZERO, (side, side)));
match self {
DotShape::Circle => {
let r = s * (t / std::f64::consts::PI).sqrt();
shapes::ellipse(Rect::from_center_size(Point::ZERO, (2.0 * r, 2.0 * r)))
}
DotShape::Ellipse => {
let r = s * (t / std::f64::consts::PI).sqrt();
shapes::ellipse(Rect::from_center_size(Point::ZERO, (2.6 * r, 2.0 * r / 1.3)))
}
DotShape::Circle => cell_dot(t, s / 2.0, CellKind::Circle),
DotShape::Ellipse => cell_dot(t, s / 2.0, CellKind::Ellipse),
DotShape::Square => square(s * t.sqrt()),
DotShape::Diamond => square(s * t.sqrt()).transformed(Affine::rotate(std::f64::consts::FRAC_PI_4)),
DotShape::Diamond => cell_dot(t, s / 2.0, CellKind::Diamond),
// Cells along a row join into a line as thick as the tone.
DotShape::Line => shapes::rectangle(Rect::from_center_size(Point::ZERO, (s * 1.001, s * t))),
DotShape::Euclidean => PathData::from_bezpath(&spot(t, s / 2.0, SpotKind::Euclidean)),
@@ -369,6 +363,9 @@ fn spot_quadrant(u: f64, h: f64, kind: SpotKind) -> Vec<CubicBez> {
/// the cell with the dot of the opposite tone cut out at each corner, so the dark joins up and the
/// light becomes holes.
fn spot(t: f64, h: f64, kind: SpotKind) -> BezPath {
if t <= 0.5 {
return four_quadrants(&spot_quadrant(t, h, kind));
}
let mut bp = BezPath::new();
let mut add = |curves: Vec<CubicBez>, first: bool| {
for (k, c) in curves.iter().enumerate() {
@@ -378,20 +375,129 @@ fn spot(t: f64, h: f64, kind: SpotKind) -> BezPath {
bp.curve_to(c.p1, c.p2, c.p3);
}
};
if t <= 0.5 {
let q1 = spot_quadrant(t, h, kind);
add(q1.clone(), 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);
} else {
// 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);
// 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);
bp.close_path();
bp
}
/// The dots that grow past their cell's sides: drawn clipped to the cell, so neighbors meet along
/// the shared side instead of overlapping, sized so the clipped dot covers the tone, and the whole
/// cell at full tone.
#[derive(Clone, Copy)]
enum CellKind {
Circle,
/// 1.69 times as wide as tall.
Ellipse,
Diamond,
}
static CELL_CIRCLE: OnceLock<Vec<f64>> = OnceLock::new();
static CELL_ELLIPSE: OnceLock<Vec<f64>> = OnceLock::new();
static CELL_DIAMOND: OnceLock<Vec<f64>> = OnceLock::new();
impl CellKind {
/// The first quadrant of the dot of size `r` clipped to the quadrant [0, 1]², from the x axis to
/// the y axis. Circle: radius `r`; Ellipse: semi-axes 1.3 `r` and `r` / 1.3; Diamond: half
/// diagonal `r`.
fn quadrant(self, r: f64) -> Vec<CubicBez> {
let corner = Point::new(1.0, 1.0);
match self {
CellKind::Diamond if r <= 1.0 => vec![segment(Point::new(r, 0.0), Point::new(0.0, r))],
CellKind::Diamond if r < 2.0 => vec![
segment(Point::new(1.0, 0.0), Point::new(1.0, r - 1.0)),
segment(Point::new(1.0, r - 1.0), Point::new(r - 1.0, 1.0)),
segment(Point::new(r - 1.0, 1.0), Point::new(0.0, 1.0)),
],
CellKind::Diamond => vec![segment(Point::new(1.0, 0.0), corner), segment(corner, Point::new(0.0, 1.0))],
CellKind::Circle | CellKind::Ellipse => {
let (a, b) = if matches!(self, CellKind::Circle) { (r, r) } else { (1.3 * r, r / 1.3) };
// The ellipse stays inside x ≤ 1 from angle tx and inside y ≤ 1 up to angle ty.
let tx = (1.0 / a).min(1.0).acos();
let ty = (1.0 / b).min(1.0).asin();
if tx >= ty {
return vec![segment(Point::new(1.0, 0.0), corner), segment(corner, Point::new(0.0, 1.0))];
}
let at = |th: f64| Point::new(a * th.cos(), b * th.sin());
let mut out = vec![];
if a > 1.0 {
out.push(segment(Point::new(1.0, 0.0), at(tx)));
}
ellipse_arc(&mut out, a, b, tx, ty);
if b > 1.0 {
out.push(segment(at(ty), Point::new(0.0, 1.0)));
}
out
}
}
}
/// The size at which the dot covers the whole quadrant.
fn max_size(self) -> f64 {
match self {
CellKind::Circle => std::f64::consts::SQRT_2,
// 1 / (1.3 r)² + (1.3 / r)² = 1 at the corner.
CellKind::Ellipse => (1.0 / 1.69 + 1.69_f64).sqrt(),
CellKind::Diamond => 2.0,
}
}
fn table(self) -> &'static OnceLock<Vec<f64>> {
match self {
CellKind::Circle => &CELL_CIRCLE,
CellKind::Ellipse => &CELL_ELLIPSE,
CellKind::Diamond => &CELL_DIAMOND,
}
}
/// For tones `k / SIZE_STEPS`, the size of the dot whose clipped quadrant covers that tone.
fn sizes(self) -> Vec<f64> {
(0..=SIZE_STEPS)
.map(|k| {
let target = k as f64 / SIZE_STEPS as f64;
let (mut lo, mut hi) = (0.0, self.max_size());
for _ in 0..50 {
let mid = (lo + hi) / 2.0;
if curves_area(&self.quadrant(mid)) < target { lo = mid } else { hi = mid }
}
(lo + hi) / 2.0
})
.collect()
}
}
/// The `kind` dot for tone `t` (0..1) in a cell of half side `h` centered on the origin, clipped to
/// the cell ([`CellKind`]).
fn cell_dot(t: f64, h: f64, kind: CellKind) -> PathData {
let sizes = kind.table().get_or_init(|| kind.sizes());
let x = t.clamp(0.0, 1.0) * SIZE_STEPS as f64;
let i = (x.floor() as usize).min(SIZE_STEPS - 1);
let (lo, hi) = (sizes.get(i).copied().unwrap_or(0.0), sizes.get(i + 1).copied().unwrap_or(0.0));
let r = lo + (hi - lo) * (x - i as f64);
let q1 = mapped(&kind.quadrant(r), |p| Point::new(p.x * h, p.y * h));
PathData::from_bezpath(&four_quadrants(&q1))
}
/// The closed path through `q1` (from the x axis to the y axis) and its mirror images in the other
/// three quadrants, counterclockwise.
fn four_quadrants(q1: &[CubicBez]) -> BezPath {
let mut bp = BezPath::new();
let mut add = |curves: Vec<CubicBez>, first: bool| {
for (k, c) in curves.iter().enumerate() {
if k == 0 {
if first { bp.move_to(c.p0) } else { bp.line_to(c.p0) }
}
bp.curve_to(c.p1, c.p2, 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);
bp.close_path();
bp
}
@@ -819,6 +925,24 @@ mod tests {
assert_eq!((DotShape::parse("Euclidean"), DotShape::parse("elliptical")), (Some(DotShape::Euclidean), Some(DotShape::Elliptical)));
}
#[test]
fn round_and_diamond_dots_stay_in_their_cell_and_fill_it_at_full_tone() {
for shape in [DotShape::Circle, DotShape::Ellipse, DotShape::Diamond] {
for t in [0.2, 0.6, 0.85, 0.97] {
// The dot never reaches into a neighbor's cell, so dots of different tones meet
// along the shared side instead of overlapping into notches.
let b = shape.path(t, 2.0).control_bounds().unwrap();
assert!(b.x0 >= -1.0 - 1e-9 && b.x1 <= 1.0 + 1e-9 && b.y0 >= -1.0 - 1e-9 && b.y1 <= 1.0 + 1e-9, "{shape:?} at {t}: {b:?}");
// What it covers of the cell is the tone.
let area = shape.path(t, 2.0).to_bezpath().area().abs() / 4.0;
assert!((area - t).abs() < 2e-3, "{shape:?} at {t}: {area}");
}
// Full tone is the whole cell, its corners included.
assert!(covers(shape, 1.0, (0.49, 0.49)) && covers(shape, 1.0, (-0.49, 0.49)), "{shape:?}");
assert!(!covers(shape, 0.9, (0.499, 0.499)), "{shape:?}");
}
}
#[test]
fn euclidean_dots_meet_corner_to_corner_at_half() {
let s = DotShape::Euclidean;