Files
Marcos Paulo 8931790b2f
ci / ci (macos-latest) (push) Has been cancelled
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
fix(whiteboard): hatch fills follow the shape outline (not the bounding box); panels restyled to the app look (segmented controls, round swatches, rounded input, accent Apply); translucent marquee; pinned actions row
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 12:38:41 -03:00

62 lines
2.8 KiB
Rust

//! Hatching is clipped to the shape's own outline, not to its bounding box.
use notescraft_whiteboard::{Element, FillStyle, IdGen, rough::fill_polylines, rough::hatch_polygon};
fn shaped(make: impl FnOnce(&mut IdGen) -> Element, style: FillStyle) -> Element {
let mut ids = IdGen::new();
let mut element = make(&mut ids);
element.fill_style = style;
element.stroke_width = 2.0;
element
}
#[test]
fn a_diamond_is_hatched_only_inside_its_diamond() {
let diamond = shaped(|ids| Element::diamond(ids, 0.0, 0.0, 200.0, 120.0), FillStyle::CrossHatch);
let lines = fill_polylines(&diamond);
assert!(lines.len() > 3, "the shape is hatched");
for point in lines.iter().flatten() {
let inside = (point[0] - 100.0).abs() / 100.0 + (point[1] - 60.0).abs() / 60.0;
assert!(inside <= 1.001, "{point:?} is outside the diamond ({inside})");
}
}
#[test]
fn an_ellipse_is_hatched_only_inside_its_ellipse() {
let ellipse = shaped(|ids| Element::ellipse(ids, 0.0, 0.0, 200.0, 100.0), FillStyle::Hachure);
for point in fill_polylines(&ellipse).iter().flatten() {
let inside = ((point[0] - 100.0) / 100.0).powi(2) + ((point[1] - 50.0) / 50.0).powi(2);
assert!(inside <= 1.02, "{point:?} is outside the ellipse ({inside})");
}
}
#[test]
fn rounded_corners_are_left_unhatched() {
// A polygon with a cut corner stands in for a rounded rectangle: nothing may land in the cut.
let polygon = [[0.0, 0.0], [90.0, 0.0], [100.0, 10.0], [100.0, 100.0], [0.0, 100.0]];
let lines = hatch_polygon(&polygon, FillStyle::CrossHatch, 2.0);
assert!(lines.len() > 3, "the shape is hatched");
for point in lines.iter().flatten() {
// The cut is the triangle above the diagonal (90,0)-(100,10).
assert!(!(point[0] > 90.0 && point[1] < point[0] - 90.0 - 0.001), "{point:?} is in the cut corner");
}
}
#[test]
fn cross_hatch_has_both_directions_and_spacing_follows_the_stroke() {
let square = [[0.0, 0.0], [100.0, 0.0], [100.0, 100.0], [0.0, 100.0]];
let one = hatch_polygon(&square, FillStyle::Hachure, 2.0);
let both = hatch_polygon(&square, FillStyle::CrossHatch, 2.0);
assert!(both.len() > one.len() && both.len() <= one.len() * 2 + 1, "{} vs {}", both.len(), one.len());
assert!(hatch_polygon(&square, FillStyle::Hachure, 8.0).len() < one.len(), "a thicker stroke spaces the lines wider");
}
#[test]
fn hostile_polygons_return_nothing_or_bounded_output() {
assert_eq!(hatch_polygon(&[], FillStyle::Hachure, 2.0).len(), 0);
assert_eq!(hatch_polygon(&[[0.0, 0.0], [1.0, 1.0]], FillStyle::Hachure, 2.0).len(), 0);
assert_eq!(hatch_polygon(&[[f32::NAN, 0.0], [1.0, 1.0], [2.0, 0.0]], FillStyle::Hachure, 2.0).len(), 0);
let huge = [[0.0, 0.0], [1.0e9, 0.0], [1.0e9, 1.0e9], [0.0, 1.0e9]];
assert!(hatch_polygon(&huge, FillStyle::CrossHatch, 1.0).len() <= 8000, "line count is capped");
}