mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 10:51:11 +01:00
Vector Halftone: Elliptical dots bridge along their length from 40%
Below 50% the Elliptical dot was an ellipse clipped to the cell's diamond, so from 40% horizontal neighbors met at a single point and at 50% the dot reached the vertical neighbors too. The dot is now an ellipse as wide as its cell at 40%; past that it is clipped to the cell's side, which makes a bridge to the next dot, and to a middle-tone cut line through the quadrant's center, which keeps the short side apart until 60%. Areas still match the tone.
This commit is contained in:
1 parent
b143f92f3f
commit
0a4fcb67ae
1 file changed
+133
-35
@@ -102,25 +102,21 @@ impl DotShape {
|
||||
DotShape::Diamond => square(s * t.sqrt()).transformed(Affine::rotate(std::f64::consts::FRAC_PI_4)),
|
||||
// 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, 1.0, &EUCLIDEAN)),
|
||||
DotShape::Elliptical => PathData::from_bezpath(&spot(t, s / 2.0, elliptical_ratio(), &ELLIPTICAL)),
|
||||
DotShape::Euclidean => PathData::from_bezpath(&spot(t, s / 2.0, SpotKind::Euclidean)),
|
||||
DotShape::Elliptical => PathData::from_bezpath(&spot(t, s / 2.0, SpotKind::Elliptical)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The Elliptical dot's height over its width: the ratio at which a dot exactly as wide as its
|
||||
/// cell, clipped to the cell's diamond, covers 40% of the cell.
|
||||
fn elliptical_ratio() -> f64 {
|
||||
static R: OnceLock<f64> = OnceLock::new();
|
||||
*R.get_or_init(|| {
|
||||
let (mut lo, mut hi) = (0.0, 1.0);
|
||||
for _ in 0..60 {
|
||||
let mid = (lo + hi) / 2.0;
|
||||
if quadrant_area(1.0, mid, 1.0) < 0.4 { lo = mid } else { hi = mid }
|
||||
}
|
||||
(lo + hi) / 2.0
|
||||
})
|
||||
}
|
||||
/// The Elliptical dot's height over its width: a dot as wide as its cell covers 40% of it
|
||||
/// (π × ratio / 4 = 0.4 of a quadrant).
|
||||
const ELLIPTICAL_RATIO: f64 = 1.6 / std::f64::consts::PI;
|
||||
|
||||
/// The Elliptical dot's middle-tone cut in a quadrant of half side 1, the line
|
||||
/// `ELLIPTICAL_CUT.0 × x + y = ELLIPTICAL_CUT.1`. It meets the side x = 1 at y = 0.25 and the y
|
||||
/// axis at 0.75 and runs through the quadrant's center, so the dot it cuts at 50% is its own
|
||||
/// complement: the dark bridges across the x side from 40%, and the y side stays light until 60%.
|
||||
const ELLIPTICAL_CUT: (f64, f64) = (0.5, 0.75);
|
||||
|
||||
/// Steps of the table of dot sizes per tone ([`dot_sizes`]) over tones 0..0.5.
|
||||
const SIZE_STEPS: usize = 512;
|
||||
@@ -128,27 +124,124 @@ const SIZE_STEPS: usize = 512;
|
||||
static EUCLIDEAN: OnceLock<Vec<f64>> = OnceLock::new();
|
||||
static ELLIPTICAL: OnceLock<Vec<f64>> = OnceLock::new();
|
||||
|
||||
/// For tones `k / SIZE_STEPS × 0.5`, the width `a` of the ellipse with height `ratio × a` whose part
|
||||
/// inside the diamond |x| + |y| ≤ 1 covers that tone of the cell [-1, 1]².
|
||||
fn dot_sizes(ratio: f64) -> Vec<f64> {
|
||||
/// The two spot-function dots.
|
||||
#[derive(Clone, Copy)]
|
||||
enum SpotKind {
|
||||
Euclidean,
|
||||
Elliptical,
|
||||
}
|
||||
|
||||
impl SpotKind {
|
||||
/// The first quadrant of the dot of width `a` in a quadrant of half side 1.
|
||||
fn quadrant(self, a: f64) -> Vec<CubicBez> {
|
||||
match self {
|
||||
SpotKind::Euclidean => quadrant(a, a, 1.0),
|
||||
SpotKind::Elliptical => elliptical_quadrant(a),
|
||||
}
|
||||
}
|
||||
|
||||
/// The width at which the dot covers half the cell.
|
||||
fn max_size(self) -> f64 {
|
||||
match self {
|
||||
SpotKind::Euclidean => 1.0,
|
||||
SpotKind::Elliptical => ELLIPTICAL_CUT.1 / ELLIPTICAL_RATIO,
|
||||
}
|
||||
}
|
||||
|
||||
fn table(self) -> &'static OnceLock<Vec<f64>> {
|
||||
match self {
|
||||
SpotKind::Euclidean => &EUCLIDEAN,
|
||||
SpotKind::Elliptical => &ELLIPTICAL,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// For tones `k / SIZE_STEPS × 0.5`, the width `a` of the dot of `kind` that covers that tone of the
|
||||
/// cell [-1, 1]².
|
||||
fn dot_sizes(kind: SpotKind) -> Vec<f64> {
|
||||
(0..=SIZE_STEPS)
|
||||
.map(|k| {
|
||||
// A quadrant of the cell has area 1.
|
||||
let target = k as f64 / SIZE_STEPS as f64 * 0.5;
|
||||
let (mut lo, mut hi) = (0.0, 1.0 / ratio);
|
||||
let (mut lo, mut hi) = (0.0, kind.max_size());
|
||||
for _ in 0..50 {
|
||||
let mid = (lo + hi) / 2.0;
|
||||
if quadrant_area(mid, mid * ratio, 1.0) < target { lo = mid } else { hi = mid }
|
||||
if curves_area(&kind.quadrant(mid)) < target { lo = mid } else { hi = mid }
|
||||
}
|
||||
(lo + hi) / 2.0
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The area of the first quadrant of the ellipse with semi-axes `a`, `b` clipped to x + y ≤ `h`.
|
||||
fn quadrant_area(a: f64, b: f64, h: f64) -> f64 {
|
||||
/// The area between the origin and `curves`, which run from the x axis to the y axis.
|
||||
fn curves_area(curves: &[CubicBez]) -> f64 {
|
||||
use vectorcraft_geom::kurbo::ParamCurveArea;
|
||||
quadrant(a, b, h).iter().map(|c| c.signed_area()).sum()
|
||||
curves.iter().map(|c| c.signed_area()).sum()
|
||||
}
|
||||
|
||||
/// Cubic curves along the ellipse with semi-axes `a`, `b` from angle `t0` to `t1`.
|
||||
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 d = (t1 - t0) / n;
|
||||
let k = 4.0 / 3.0 * (d / 4.0).tan();
|
||||
for i in 0..n as usize {
|
||||
let (u0, u1) = (t0 + d * i as f64, t0 + d * (i as f64 + 1.0));
|
||||
let (p0, p3) = (at(u0), at(u1));
|
||||
let c1 = p0 + k * vectorcraft_geom::Vec2::new(-a * u0.sin(), b * u0.cos());
|
||||
let c2 = p3 - k * vectorcraft_geom::Vec2::new(-a * u1.sin(), b * u1.cos());
|
||||
out.push(CubicBez::new(p0, c1, c2, p3));
|
||||
}
|
||||
}
|
||||
|
||||
/// The straight segment from `p` to `q` as a cubic curve.
|
||||
fn segment(p: Point, q: Point) -> CubicBez {
|
||||
CubicBez::new(p, p.lerp(q, 1.0 / 3.0), p.lerp(q, 2.0 / 3.0), q)
|
||||
}
|
||||
|
||||
/// The first quadrant of the Elliptical dot of width `a` (height `ELLIPTICAL_RATIO × a`) in a
|
||||
/// quadrant of half side 1, from the x axis to the y axis. Up to width 1 (40%) it is the whole
|
||||
/// ellipse. Wider, the ellipse is clipped to the side x = 1, which makes a bridge to the next dot
|
||||
/// along x, and to the cut line [`ELLIPTICAL_CUT`], which keeps it off the y side.
|
||||
fn elliptical_quadrant(a: f64) -> Vec<CubicBez> {
|
||||
use std::f64::consts::FRAC_PI_2;
|
||||
let b = a * ELLIPTICAL_RATIO;
|
||||
let mut out = vec![];
|
||||
if a <= 1.0 {
|
||||
ellipse_arc(&mut out, a, b, 0.0, FRAC_PI_2);
|
||||
return out;
|
||||
}
|
||||
let at = |th: f64| Point::new(a * th.cos(), b * th.sin());
|
||||
let (m, c) = ELLIPTICAL_CUT;
|
||||
// The ellipse crosses the side x = 1 at angle t0.
|
||||
let t0 = (1.0 / a).acos();
|
||||
// Between angles ts and te the ellipse lies beyond the cut line.
|
||||
let r = (m * a).hypot(b);
|
||||
let (ts, te) = if r > c {
|
||||
let (phi, alpha) = (b.atan2(m * a), (c / r).acos());
|
||||
(phi - alpha, phi + alpha)
|
||||
} else {
|
||||
(FRAC_PI_2, FRAC_PI_2)
|
||||
};
|
||||
let end = if te < FRAC_PI_2 { at(te) } else { Point::new(0.0, c) };
|
||||
out.push(segment(Point::new(1.0, 0.0), if t0 < ts || t0 >= te { at(t0) } else { Point::new(1.0, c - m) }));
|
||||
if t0 >= te {
|
||||
ellipse_arc(&mut out, a, b, t0, FRAC_PI_2);
|
||||
return out;
|
||||
}
|
||||
if t0 < ts {
|
||||
ellipse_arc(&mut out, a, b, t0, ts.min(FRAC_PI_2));
|
||||
if ts >= FRAC_PI_2 {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
let from = out.last().map_or(Point::new(1.0, 0.0), |q| q.p3);
|
||||
out.push(segment(from, end));
|
||||
ellipse_arc(&mut out, a, b, te, FRAC_PI_2);
|
||||
out
|
||||
}
|
||||
|
||||
/// The first quadrant of the ellipse with semi-axes `a`, `b` clipped to the diamond side x + y ≤
|
||||
@@ -202,22 +295,23 @@ fn mapped(curves: &[CubicBez], f: impl Fn(Point) -> Point) -> Vec<CubicBez> {
|
||||
curves.iter().map(|c| CubicBez::new(f(c.p0), f(c.p1), f(c.p2), f(c.p3))).collect()
|
||||
}
|
||||
|
||||
/// The quadrant of the spot for tone `u` (0..0.5) in a cell of half side `h`, its dot `ratio` as
|
||||
/// tall as wide, its sizes from `table` ([`dot_sizes`]).
|
||||
fn spot_quadrant(u: f64, h: f64, ratio: f64, table: &OnceLock<Vec<f64>>) -> Vec<CubicBez> {
|
||||
let sizes = table.get_or_init(|| dot_sizes(ratio));
|
||||
/// The quadrant of the `kind` spot for tone `u` (0..0.5) in a cell of half side `h`, its size from
|
||||
/// the kind's table ([`dot_sizes`]).
|
||||
fn spot_quadrant(u: f64, h: f64, kind: SpotKind) -> Vec<CubicBez> {
|
||||
let sizes = kind.table().get_or_init(|| dot_sizes(kind));
|
||||
let x = (u / 0.5).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 a = lo + (hi - lo) * (x - i as f64);
|
||||
quadrant(a * h, a * ratio * h, h)
|
||||
mapped(&kind.quadrant(a), |p| Point::new(p.x * h, p.y * h))
|
||||
}
|
||||
|
||||
/// A spot-function dot for tone `t` (0..1) in a cell of half side `h` centered on the origin: up to
|
||||
/// half tone, an ellipse (`ratio` as tall as wide) clipped to the cell's diamond, with the tone's
|
||||
/// area; past half, 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, ratio: f64, table: &OnceLock<Vec<f64>>) -> BezPath {
|
||||
/// half tone, the `kind` dot with the tone's area (Euclidean: a circle clipped to the cell's
|
||||
/// diamond; Elliptical: an ellipse clipped to the cell's sides and its middle-tone cut); past half,
|
||||
/// 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 {
|
||||
let mut bp = BezPath::new();
|
||||
let mut add = |curves: Vec<CubicBez>, first: bool| {
|
||||
for (k, c) in curves.iter().enumerate() {
|
||||
@@ -228,14 +322,14 @@ fn spot(t: f64, h: f64, ratio: f64, table: &OnceLock<Vec<f64>>) -> BezPath {
|
||||
}
|
||||
};
|
||||
if t <= 0.5 {
|
||||
let q1 = spot_quadrant(t, h, ratio, table);
|
||||
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, ratio, table);
|
||||
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);
|
||||
@@ -653,12 +747,16 @@ mod tests {
|
||||
assert!(!covers(s, 0.38, (0.495, 0.0)) && !covers(s, 0.38, (0.0, 0.45)));
|
||||
// From 40% they reach their neighbors along x, making chains, but not along y.
|
||||
assert!(covers(s, 0.42, (0.495, 0.0)) && !covers(s, 0.42, (0.0, 0.45)));
|
||||
// The chains are bridges as tall as the tone allows across the shared side, not single
|
||||
// points: off the axis on the side x = 0.5, and on the cell's other side by symmetry.
|
||||
assert!(covers(s, 0.46, (0.499, 0.03)) && covers(s, 0.46, (-0.499, -0.03)) && !covers(s, 0.46, (0.499, 0.2)));
|
||||
assert!(covers(s, 0.5, (0.499, 0.12)) && !covers(s, 0.5, (0.499, 0.13)) && !covers(s, 0.5, (0.0, 0.49)));
|
||||
// Up to 60% the light still runs along the top edge between the corner holes.
|
||||
assert!(!covers(s, 0.55, (0.02, 0.499)) && covers(s, 0.55, (0.0, 0.3)));
|
||||
// Past 60% the dark joins across the top edge too, leaving elongated holes.
|
||||
assert!(covers(s, 0.65, (0.02, 0.499)) && covers(s, 0.65, (0.495, 0.0)) && !covers(s, 0.65, (0.49, 0.49)));
|
||||
// The holes are longer along x than along y.
|
||||
assert!(!covers(s, 0.8, (0.3, 0.49)) && covers(s, 0.8, (0.49, 0.3)), "ratio {}", elliptical_ratio());
|
||||
assert!(!covers(s, 0.8, (0.3, 0.49)) && covers(s, 0.8, (0.49, 0.3)), "ratio {ELLIPTICAL_RATIO}");
|
||||
// Today's Ellipse stays as it was: an ellipse 1.69 times as wide as tall.
|
||||
let b = DotShape::Ellipse.path(0.3, 1.0).control_bounds().unwrap();
|
||||
assert!((b.width() / b.height() - 1.69).abs() < 1e-6, "{b:?}");
|
||||
|
||||
Reference in new issue
Block a user