hatch_gallery/
hatch_gallery.rs1use aoer_plotty_rs::context::{typography::Typography, Context};
2use aoer_plotty_rs::geo_types::svg::Arrangement;
3use aoer_plotty_rs::prelude::{
4 CircleHatch, CrossHatch, FastHexHatch, HatchPattern, LineHatch, NoHatch, RadiusHatch,
5 SpiralDirection, SpiralHatch,
6};
7use geo_types::{coord, Rect};
8use std::path::Path;
9use std::sync::Arc;
10
11fn main() {
12 let mut ctx = Context::new();
13
14 let fills: Vec<Arc<Box<dyn HatchPattern>>> = vec![
15 (Arc::new(Box::new(LineHatch {}))),
16 (Arc::new(Box::new(CrossHatch {}))),
17 (Arc::new(Box::new(RadiusHatch { x: 160., y: 32.0 }))),
18 (Arc::new(Box::new(CircleHatch {}))),
19 (Arc::new(Box::new(FastHexHatch {}))),
20 (Arc::new(Box::new(RadiusHatch { x: 160., y: 32.0 }))),
21 (Arc::new(Box::new(SpiralHatch {
22 x: 32.0,
23 y: 3. * 60.0,
24 direction: SpiralDirection::Widdershins,
25 }))),
26 (Arc::new(Box::new(SpiralHatch {
27 x: 64. + 32.0,
28 y: 3. * 60.0,
29 direction: SpiralDirection::Deasil,
30 }))),
31 ];
32
33 for (i, pattern) in fills.iter().enumerate() {
34 ctx.stroke("black")
35 .fill("black")
36 .pen(0.25)
37 .pattern(pattern.clone())
38 .hatch_scale(Some(3.))
39 .hatch(0.)
40 .poly(
41 vec![
42 ((i % 3) as f64 * 64. + 4., (i / 3) as f64 * 72. + 4.),
43 ((i % 3) as f64 * 64. + 4. + 56., (i / 3) as f64 * 72. + 4.),
44 (
45 (i % 3) as f64 * 64. + 56. + 4.,
46 (i / 3) as f64 * 72. + 56. + 4.,
47 ),
48 ((i % 3) as f64 * 64. + 4., (i / 3) as f64 * 72. + 4. + 56.),
49 ],
50 vec![],
51 )
52 .pen(0.2)
53 .pattern(Arc::new(Box::new(NoHatch {})))
54 .typography(
55 &format!("{:?}", pattern),
56 (i % 3) as f64 * 64. + 4.,
57 (i / 3) as f64 * 72. + 8. + 56.,
58 &Typography::new().size(1.2),
59 );
60 }
61
62 let svg = ctx
63 .to_svg(&Arrangement::<f64>::unit(&Rect::<f64>::new(
64 coord! {x:0.0, y:0.0},
65 coord! {x:500.0, y:500.0},
66 )))
67 .unwrap();
68 let fname = Path::new(file!()).file_stem().unwrap().to_str().unwrap();
71 svg::save(format!("images/{}.svg", fname), &svg).unwrap();
72}