mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
PICT: curved fills that coarsen to one line keep their area
When a fill holds more points than one polygon, its curves are flattened more coarsely. A small figure could then come out as points that all lie on one line, which encloses nothing, and the figure lost its filled area. Such a figure now keeps its finer outline, as one that coarsens to two points already did.
This commit is contained in:
1 parent
be9e3cc116
commit
9cb4ee70e2
2 files changed
+35
-3
No files matched your search
@@ -79,6 +79,15 @@ fn i16_of(v: f64) -> i16 {
|
||||
v.round().clamp(-MAX_I16, MAX_I16) as i16
|
||||
}
|
||||
|
||||
/// Whether every point of `fig` lies on one line, so that it encloses no area.
|
||||
fn collinear(fig: &[[i16; 2]]) -> bool {
|
||||
let Some(&[x0, y0]) = fig.first() else { return true };
|
||||
let (x0, y0) = (i64::from(x0), i64::from(y0));
|
||||
let Some(&[x1, y1]) = fig.iter().find(|p| **p != fig[0]) else { return true };
|
||||
let (dx, dy) = (i64::from(x1) - x0, i64::from(y1) - y0);
|
||||
fig.iter().all(|&[x, y]| dx * (i64::from(y) - y0) == dy * (i64::from(x) - x0))
|
||||
}
|
||||
|
||||
struct Writer {
|
||||
pic: PictWriter,
|
||||
/// Document space to device units.
|
||||
@@ -229,9 +238,10 @@ impl Writer {
|
||||
.flat_map(|(range, _)| {
|
||||
let subpath = BezPath::from_vec(path.elements()[range].to_vec());
|
||||
let coarse = self.figures(&subpath, flatness);
|
||||
// A small curved figure can collapse to two points. Keep its finer outline
|
||||
// rather than leave it out; splitting and thinning still bound polygon size.
|
||||
if coarse.is_empty() {
|
||||
// A small curved figure can collapse to two points, or to points on one line.
|
||||
// Keep its finer outline rather than lose its area; splitting and thinning
|
||||
// still bound polygon size.
|
||||
if coarse.is_empty() || coarse.iter().any(|fig| collinear(fig)) {
|
||||
self.figures(&subpath, FLATNESS)
|
||||
} else {
|
||||
coarsened |= subpath.elements().iter().any(|el| matches!(el, PathEl::QuadTo(..) | PathEl::CurveTo(..)));
|
||||
|
||||
@@ -377,6 +377,28 @@ fn pict_coarse_flattening_keeps_small_curved_figures() {
|
||||
assert!(out.warnings.iter().any(|w| w.contains("several polygons")), "{:?}", out.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pict_coarse_flattening_keeps_figures_that_become_one_line() {
|
||||
// At the coarsest flatness the curve becomes a line, and its three points lie on one line.
|
||||
let bumps: Vec<BezPath> = (0..2200)
|
||||
.map(|i| {
|
||||
let (x, y) = (f64::from(i % 50) * 1.5 + 1.0, f64::from(i / 50) * 1.5 + 1.0);
|
||||
let mut bp = BezPath::new();
|
||||
bp.move_to((x, y));
|
||||
bp.quad_to((x + 0.5, y - 0.5), (x + 1.0, y));
|
||||
bp.line_to((x + 0.5, y));
|
||||
bp.close_path();
|
||||
bp
|
||||
})
|
||||
.collect();
|
||||
let out = pict(&doc_with(100.0, 100.0, vec![compound(&bumps, FillRule::EvenOdd)]));
|
||||
let pic = read(&out);
|
||||
for i in [0_u32, 1100, 2199] {
|
||||
let (x, y) = (f64::from(i % 50) * 1.5 + 1.0, f64::from(i / 50) * 1.5 + 1.0);
|
||||
assert!(painted(&pic, (x + 0.5, y - 0.1)), "bump {i} was lost");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fill_past_one_polygon_is_split_or_thinned() {
|
||||
// 3,000 separate squares: more points than one polygon holds.
|
||||
|
||||
Reference in new issue
Block a user