rsille 2.3.1

A full feature braille code art lib
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// TODO: remove the extra things for animation in Turtle

use std::f64::consts::PI;

use crate::{
    canvas::Paint,
    utils::{RsilleErr, MIN_DIFFERENCE},
    Canvas,
};

use crate::color::Color;

/// The turtle impl of braille code in Rust
///
/// All the api is similar to turtle in Python.
/// It support both paint directly and animation.
/// If you don't know how to use, you can search turtle on the internet,
/// then you can paste those code you find and use it!
///
/// ## Example
///
/// just paint it
/// ```
/// use rsille::{extra::Turtle, Canvas};
/// let mut canvas = Canvas::new();
/// let mut t = Turtle::new();
/// let mut length = 1.0;
/// for _ in 0..150 {
///     t.forward(length);
///     t.right(10.0);
///     length += 0.05;
/// }
/// canvas.paint(&t, 0, 0).unwrap();
/// canvas.print();
/// ```
///
/// or a animation
/// ```no_run
/// use rsille::{extra::Turtle, Animation};
/// let mut anime = Animation::new();
/// let mut t = Turtle::new();
/// let mut length = 1.0;
/// for _ in 0..150 {
///     t.forward(length);
///     t.right(10.0);
///     length += 0.05;
/// }
/// t.anime(); // transfrom the turtle to animation
/// anime.push(t, move |t| t.update(), (50, -50));
/// anime.run();
/// ```
/// The (50, -50) isn't fixed, you can try to paint on other place.
///
/// ## NOTE:
///
/// There isn't position or heading function,
/// because the position and heading can't know until paint it!
/// When you call forward or circle or any other method, it will only record the procedure.
/// Then in the paint function, it will do those procedures and paint the canvas.
#[derive(Debug, Clone, PartialEq)]
pub struct Turtle {
    procedures: Vec<Procedure>,
    anime_proc: Option<Vec<Procedure>>,
    anime_step: f64,
    frame_count: usize,
}

impl Turtle {
    /// Return a new Turtle
    pub fn new() -> Self {
        Self {
            procedures: Vec::new(),
            anime_proc: None,
            anime_step: 10.0,
            frame_count: 0,
        }
    }

    /// Pen up, the turtle won't draw when moving
    pub fn penup(&mut self) {
        self.add_procedure(Procedure::PenUp);
    }

    /// alias: [`penup`](struct.Turtle.html#method.penup)
    pub fn up(&mut self) {
        self.penup();
    }

    /// Pen down, the turtle will draw when moving
    pub fn pendown(&mut self) {
        self.add_procedure(Procedure::PenDown);
    }

    /// alias: [`pendown`](struct.Turtle.html#method.pendown)
    pub fn down(&mut self) {
        self.pendown();
    }

    /// Move the turtle forward by the specified distance, in the direction the turtle is headed.
    /// * `step` - the distance
    pub fn forward<T>(&mut self, step: T)
    where
        T: Into<f64>,
    {
        self.add_procedure(Procedure::Forward(step.into()));
    }

    /// alias: [`forward`](struct.Turtle.html#method.forward)
    pub fn fd<T>(&mut self, step: T)
    where
        T: Into<f64>,
    {
        self.forward(step);
    }

    /// Move the turtle backward by distance, opposite to the direction the turtle is headed.
    /// * `step` - the distance
    ///
    /// It won't change the turtle’s heading.
    pub fn backward<T>(&mut self, step: T)
    where
        T: Into<f64>,
    {
        self.forward(-step.into());
    }

    /// alias: [`backward`](struct.Turtle.html#method.backward)
    pub fn back<T>(&mut self, step: T)
    where
        T: Into<f64>,
    {
        self.backward(step);
    }

    /// alias: [`backward`](struct.Turtle.html#method.backward)
    pub fn bk<T>(&mut self, step: T)
    where
        T: Into<f64>,
    {
        self.backward(step);
    }

    /// Turn turtle right by angle units.
    /// * `angle` - the degree
    pub fn right<T>(&mut self, angle: T)
    where
        T: Into<f64>,
    {
        self.add_procedure(Procedure::Right(angle.into()));
    }

    /// alias: [`right`](struct.Turtle.html#method.right)
    pub fn rt<T>(&mut self, angle: T)
    where
        T: Into<f64>,
    {
        self.right(angle);
    }

    /// Turn turtle left by angle units.
    /// * `angle` - the degree
    pub fn left<T>(&mut self, angle: T)
    where
        T: Into<f64>,
    {
        self.right(-angle.into());
    }

    /// alias: [`left`](struct.Turtle.html#method.left)
    pub fn lt<T>(&mut self, angle: T)
    where
        T: Into<f64>,
    {
        self.left(angle);
    }

    /// Draw a circle with given radius.
    /// * `extent` – an angle – determines which part of the circle is drawn. If extent is not a full circle, one endpoint of the arc is the current pen position.
    /// * `radius` - Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent.
    ///
    /// The center is radius units left of the turtle;
    pub fn circle<T>(&mut self, radius: T, extent: T)
    where
        T: Into<f64>,
    {
        self.add_procedure(Procedure::Circle(radius.into(), extent.into(), 100));
    }

    /// Draw a circle with given radius.
    /// The center is radius units left of the turtle;
    /// * `extent` – an angle – determines which part of the circle is drawn. If extent is not a full circle, one endpoint of the arc is the current pen position.
    /// * `radius` - Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent.
    /// * `steps` - Suggest 100. As the circle is approximated by an inscribed regular polygon, steps determines the number of steps to use.
    ///
    /// Suggest use [`circle`](struct.Turtle.html#method.circle)
    pub fn circle_with_steps<T>(&mut self, radius: T, extent: T, steps: usize)
    where
        T: Into<f64>,
    {
        self.add_procedure(Procedure::Circle(radius.into(), extent.into(), steps));
    }

    /// Move turtle to an absolute position. If the pen is down, draw line.
    /// * `x` - the position of x
    /// * `y` - the position of y
    pub fn goto<T>(&mut self, x: T, y: T)
    where
        T: Into<f64>,
    {
        self.add_procedure(Procedure::Goto(x.into(), y.into()));
    }

    /// Move turtle to an absolute position and a line will not be drawn.
    /// * `x` - the position of x
    /// * `y` - the position of y
    pub fn teleport<T>(&mut self, x: T, y: T)
    where
        T: Into<f64>,
    {
        self.add_procedure(Procedure::Teleport(x.into(), y.into()));
    }

    /// Move turtle to the origin
    /// * – coordinates (0,0)
    /// * – set its heading to its start-orientation
    pub fn home(&mut self) {
        self.add_procedure(Procedure::Home);
    }

    /// Set the color of turtle
    pub fn colorful(&mut self, color: Color) {
        self.add_procedure(Procedure::Colorful(color));
    }

    /// alias: [`colorful`](struct.Turtle.html#method.colorful)
    pub fn color(&mut self, color: Color) {
        self.colorful(color);
    }

    /// Build the Turtle for animation
    ///
    /// If you don't need the animation, then don't call this method
    pub fn anime(&mut self) {
        use Procedure::*;
        let mut anime_proc = Vec::new();
        let astep = self.anime_step;
        for p in &self.procedures {
            match p {
                PenDown | PenUp | Right(_) | Teleport(_, _) | Home => {
                    anime_proc.push(*p);
                }
                Forward(step) => {
                    let mut step = *step;
                    while step > astep {
                        anime_proc.push(Forward(astep));
                        step -= astep;
                    }
                    // forbide the lost of f64
                    if (step - astep).abs() < MIN_DIFFERENCE {
                        anime_proc.push(Forward(astep));
                    } else {
                        anime_proc.push(Forward(step));
                    }
                }
                Goto(_, _) => {
                    anime_proc.push(*p);
                }
                Circle(radius, extent, steps) => {
                    let mut extent = *extent;
                    if PI * radius * extent <= 180.0 * astep {
                        anime_proc.push(*p);
                    } else {
                        let e = 180.0 * astep / (PI * radius);
                        while extent > e {
                            anime_proc.push(Circle(*radius, e, *steps));
                            extent -= e;
                        }
                        // forbide the lost of f64
                        if (extent - e).abs() < MIN_DIFFERENCE {
                            anime_proc.push(Circle(*radius, e, *steps));
                        } else {
                            anime_proc.push(Circle(*radius, extent, *steps));
                        }
                    }
                }
                _ => anime_proc.push(*p),
            }
        }
        self.anime_proc = Some(anime_proc);
    }

    /// Generate the next frame of the animation
    ///
    /// Return true if the animation is over
    ///
    /// If you don't need the animation, don't need to call this method.
    /// And you should call [`anime`](struct.Turtle.html#method.anime) first
    pub fn update(&mut self) -> bool {
        use Procedure::*;
        if let Some(procs) = &self.anime_proc {
            if self.frame_count >= procs.len() {
                return true;
            }
            while let Some(p) = procs.get(self.frame_count) {
                match p {
                    Forward(_) | Goto(_, _) | Circle(_, _, _) => {
                        self.frame_count += 1;
                        break;
                    }
                    _ => {
                        self.frame_count += 1;
                    }
                }
            }
        }
        false
    }

    /// Set the step of the animation
    ///
    /// The default of Turtle is 10.0, if you want to change it, you can call this method
    ///
    /// For example:
    /// * *forward(100.0) -> forward(10.0) * 10*
    /// * but *forward(5.0) -> forward(5.0)*
    ///
    /// Beacuse like *forward(5.0), right(10.0), forward(5.0)* can't fold to *forward(10.0), right(10.0)*
    pub fn set_anime_step<T>(&mut self, step: T)
    where
        T: Into<f64>,
    {
        self.anime_step = step.into();
    }

    fn add_procedure(&mut self, p: Procedure) {
        self.procedures.push(p);
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
enum Procedure {
    PenDown,
    PenUp,
    Forward(f64), // step
    // Backward(f64), == - Forward
    Right(f64), // angle
    // Left(f64), == - Right
    Teleport(f64, f64), // (x, y)
    Home,
    Goto(f64, f64),          // (x, y)
    Circle(f64, f64, usize), // (radius, extent, steps)
    Colorful(Color),
}

fn forward(
    canvas: &mut Canvas,
    x: f64,
    y: f64,
    heading: f64,
    pen: bool,
    step: f64,
    color: Color,
) -> (f64, f64) {
    let (sr, cr) = heading.to_radians().sin_cos();
    let txy = (x + cr * step, y + sr * step);
    if pen {
        canvas.line_colorful((x, y), txy, color);
    }
    txy
}

impl Paint for Turtle {
    fn paint<T>(&self, canvas: &mut Canvas, x: T, y: T) -> Result<(), RsilleErr>
    where
        T: Into<f64>,
    {
        use Procedure::*;
        let (x, y) = (x.into(), y.into());
        let (home_x, home_y) = (x, y);
        let (mut pen, mut heading, mut x, mut y) = (true, 0.0, x, y);
        let mut color = Color::Reset;
        let procs = if let Some(procs) = &self.anime_proc {
            &procs[0..self.frame_count]
        } else {
            &self.procedures
        };

        for p in procs {
            match p {
                PenDown => {
                    pen = true;
                }
                PenUp => {
                    pen = false;
                }
                Forward(step) => {
                    (x, y) = forward(canvas, x, y, heading, pen, *step, color);
                }
                Right(angle) => {
                    heading -= angle;
                }
                Teleport(tx, ty) => {
                    x = *tx;
                    y = *ty;
                }
                Home => {
                    (x, y) = (home_x, home_y);
                }
                Goto(tx, ty) => {
                    if pen {
                        canvas.line_colorful((x, y), (*tx, *ty), color);
                    }
                    x = *tx;
                    y = *ty;
                }
                Circle(radius, extent, steps) => {
                    let angle = extent / *steps as f64;
                    for _ in 0..*steps {
                        (x, y) = forward(
                            canvas,
                            x,
                            y,
                            heading,
                            pen,
                            2.0 * radius * (angle / 2.0).to_radians().sin(),
                            color,
                        );
                        heading -= angle;
                    }
                }
                Colorful(c) => {
                    color = *c;
                }
            }
        }
        Ok(())
    }
}