context_basic/
context_basic.rs

1use aoer_plotty_rs::context::Context;
2use aoer_plotty_rs::geo_types::svg::Arrangement;
3use aoer_plotty_rs::prelude::Hatches;
4use geo_types::{coord, Rect};
5use std::path::Path;
6
7fn main() {
8    let mut ctx = Context::new();
9    ctx.stroke("black")
10        .fill("red")
11        .pen(0.5)
12        .outline(Some(5.0))
13        .hatch(45.0)
14        .poly(
15            vec![(0.0, 0.0), (25.0, 0.0), (25.0, 25.0), (0.0, 25.0)],
16            vec![],
17        )
18        .outline(None)
19        .pattern(Hatches::line())
20        .hatch(135.0)
21        .stroke("blue")
22        .fill("yellow")
23        .circle(12.5, 12.5, 5.0)
24        .push()
25        .hatch(180.0)
26        .stroke("red")
27        .fill("green")
28        .circle(17.5, 12.5, 2.5)
29        .pop()
30        .unwrap()
31        .hatch(0.0)
32        .pattern(Hatches::none())
33        .clip(true)
34        .circle(7.5, 12.5, 2.5)
35        .clip(false)
36        .stroke("brown")
37        .pen(1.0)
38        .line(0.0, 0.0, 3.0, 3.0)
39        .pen(0.1)
40        .outline(Some(1.0))
41        .stroke("pink")
42        .line(3.0, 0.0, 0.0, 3.0)
43        .stroke("purple")
44        .spline(
45            &vec![
46                (0.0, 25.0),
47                (0.0, 25.0),
48                (10.0, 20.0),
49                (20.0, 25.0),
50                (25.0, 25.0),
51            ],
52            8,
53            0.5,
54        )
55        .push() // Prepare for this transformation stuff...
56        .transform(Some(
57            &(Context::translate_matrix(25.0, 25.0)
58                * Context::rotate_matrix(45.0)
59                * Context::scale_matrix(1.0, 0.5)),
60        )) // Holy crap we can multiply these?! ;)
61        .stroke("cyan")
62        .circle(0.0, 0.0, 8.0)
63        .pop()
64        .unwrap() // We're back to purple and regular coords
65        .outline(None)
66        .stroke("green")
67        .regular_poly(8, 80.0, 80.0, 20.0, 0.0)
68        .star_poly(5, 30.0, 80.0, 10.0, 20.0, 0.0)
69        .transform(Some(
70            &(Context::translate_matrix(50.0, 50.0) * Context::scale_matrix(0.02, -0.02)),
71        ))
72        .pattern(Hatches::none())
73        .pattern(Hatches::line())
74        .glyph('Q', false);
75
76    let svg = ctx
77        .to_svg(&Arrangement::<f64>::unit(&Rect::<f64>::new(
78            coord! {x:0.0, y:0.0},
79            coord! {x:100.0, y:100.0},
80        )))
81        .unwrap();
82    // Write it to the images folder, so we can use it as an example!
83    // Write it out to /images/$THIS_EXAMPLE_FILE.svg
84    let fname = Path::new(file!()).file_stem().unwrap().to_str().unwrap();
85    svg::save(format!("images/{}.svg", fname), &svg).unwrap();
86}