pub fn degrees(deg: f64) -> f64Expand description
Helper function to convert degrees to radians
Examples found in repository?
examples/gosper_lsys_turtle_nannou.rs (line 44)
24fn model(_app: &App) -> Model {
25 // Create a turtle
26 let mut t = Turtle::new();
27 // And put its pen down so that it is drawing.
28 t = t.pen_down();
29
30 // Create a new LSystem, which defines a Gosper curve. We'll be expanding this
31 // into a path next.
32 let gosper = LSystem {
33 axiom: "A".to_string(),
34 rules: HashMap::from([
35 ('A', "A-B--B+A++AA+B-".to_string()),
36 ('B', "+A-BB--B-A++A+B".to_string())]),
37 };
38
39 // Create a MultiLineString via the Turtle
40 let tlines = t
41 // Use the turtle's TurtleTrait to walk an LPath, which is given by...
42 .walk_lpath(
43 // Expanding the gosper system we just created, on the 4th order
44 &gosper.expand(4), degrees(60.0), 8.0)
45 // And convert to multiline
46 .to_multiline();
47
48 // Find the center of the drawing
49 let bc = tlines.bounding_rect().unwrap().bounding_rect().center();
50
51 // Center it
52 let tlines = tlines.translate(-bc.x, -bc.y);
53
54 // We're done. Save it in the model.
55 Model {
56 loops: 0,
57 tlines
58 }
59}More examples
examples/gosper_svg.rs (line 29)
8fn main() {
9 // First, we create a new Turtle, which is capable of drawing things.
10 let mut t = Turtle::new();
11
12 // And put its pen down so that it is drawing.
13 t = t.pen_down();
14
15 // Create a new LSystem, which defines a Gosper curve. We'll be expanding this
16 // into a path next.
17 let gosper = LSystem {
18 axiom: "A".to_string(),
19 rules: HashMap::from([
20 ('A', "A-B--B+A++AA+B-".to_string()),
21 ('B', "+A-BB--B-A++A+B".to_string())]),
22 };
23
24 // Create a MultiLineString via the Turtle
25 let tlines = t
26 // Use the turtle's TurtleTrait to walk an LPath, which is given by...
27 .walk_lpath(
28 // Expanding the gosper system we just created, on the 4th order
29 &gosper.expand(3), degrees(60.0), 8.0)
30 // And convert to multiline
31 .to_multiline();
32
33 // Define our viewbox/canvas (in mm)
34 let viewbox = Rect::new(
35 coord! {x:0f64, y:0f64},
36 coord! {x: 300f64, y:400f64});
37
38 // Define an arrangement where we center the Gosper Curve in the center
39 // of the page, with the viewbox of 300mmx400mm as the canvas.
40 let arrangement = Arrangement::FitCenter(viewbox, true);
41
42 // Now we create the base SVG document from our arrangement,
43 // and use the to_path feature to create a path for the lines.
44 // Note that you can call that add multiple times, but if you
45 // do, you should use a set transformation instead of just fit/center
46 // which could misalign separate path sets (or just use MultiLineString
47 // with several LineStrings inside to consistently draw and align).
48 let svg = arrangement.create_svg_document().unwrap()
49 .add(tlines.to_path(&arrangement))
50 .set("fill", "none")
51 .set("stroke", "black")
52 .set("stroke-width", 2)
53 .set("stroke-linejoin", "round")
54 .set("stroke-linecap", "round");
55
56 // Write it to the images folder, so we can use it as an example!
57 svg::save("images/gosper-to-svg-example.svg", &svg).unwrap();
58}