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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
use super::{GraphMaker, StrError};
use crate::AsMatrix;
use std::fmt::Write;

/// Defines the poly-curve code
///
/// Reference: [Matplotlib](https://matplotlib.org/stable/api/path_api.html)
#[derive(Clone, Copy, Debug)]
pub enum PolyCode {
    /// Move to coordinate (first point)
    ///
    /// Matplotlib: Pick up the pen and move to the given vertex.
    MoveTo,

    /// Segment (next point, need 2 points)
    ///
    /// Matplotlib: Draw a line from the current position to the given vertex.
    LineTo,

    /// Quadratic Bezier (next point, need 3 control points with the first and last points on the curve)
    ///
    /// Matplotlib: Draw a quadratic Bezier curve from the current position, with the given control point, to the given end point.
    Curve3,

    /// Cubic Bezier (next point, need 4 control points with the first and last points on the curve)
    ///
    /// Matplotlib: Draw a cubic Bezier curve from the current position, with the given control points, to the given end point.
    Curve4,
}

/// Implements functions to draw 2D and 3D features, including poly-lines and Bezier curves
///
/// # Examples
///
/// ## Drawing functions with polyline set by an array
///
/// ```
/// use plotpy::{Canvas, Plot};
///
/// fn main() -> Result<(), &'static str> {
///     // canvas object and common options
///     let mut canvas = Canvas::new();
///     canvas.set_line_width(3.0).set_edge_color("#cd0000").set_face_color("#eeea83");
///
///     // draw arc
///     canvas.draw_arc(0.5, 0.5, 0.4, 195.0, -15.0);
///
///     // draw arrow
///     canvas.set_arrow_scale(50.0).set_arrow_style("fancy");
///     canvas.draw_arrow(0.4, 0.3, 0.6, 0.5);
///
///     // draw circle
///     canvas.set_face_color("None").set_edge_color("#1f9c25").set_line_width(6.0);
///     canvas.draw_circle(0.5, 0.5, 0.5);
///
///     // draw polyline
///     canvas.set_line_width(3.0).set_edge_color("blue");
///     let a = 0.2;
///     let c = f64::sqrt(3.0) / 2.0;
///     let p = &[[0.1, 0.5], [0.1 + a, 0.5], [0.1 + a / 2.0, 0.5 + a * c]];
///     let q = &[[0.9, 0.5], [0.9 - a, 0.5], [0.9 - a / 2.0, 0.5 + a * c]];
///     canvas.draw_polyline(p, true);
///     canvas.draw_polyline(q, false);
///
///     // add canvas to plot
///     let mut plot = Plot::new();
///     plot.set_hide_axes(true)
///         .set_equal_axes(true)
///         .set_range(-0.05, 1.05, -0.05, 1.05)
///         .add(&canvas);
///
///     // save figure
///     plot.save("/tmp/plotpy/doc_tests/doc_canvas.svg")?;
///     Ok(())
/// }
/// ```
///
/// ![doc_canvas.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_canvas.svg)
///
/// ## Cubic Bezier and use of begin/end functions
///
/// ```
/// use plotpy::{Canvas, Plot, PolyCode, StrError};
///
/// fn main() -> Result<(), StrError> {
///     // codes
///     let data = [
///         (3.0, 0.0, PolyCode::MoveTo),
///         (1.0, 1.5, PolyCode::Curve4),
///         (0.0, 4.0, PolyCode::Curve4),
///         (2.5, 3.9, PolyCode::Curve4),
///         (3.0, 3.8, PolyCode::LineTo),
///         (3.5, 3.9, PolyCode::LineTo),
///         (6.0, 4.0, PolyCode::Curve4),
///         (5.0, 1.5, PolyCode::Curve4),
///         (3.0, 0.0, PolyCode::Curve4),
///     ];
///
///     // polycurve
///     let mut canvas = Canvas::new();
///     canvas.set_face_color("#f88989").set_edge_color("red");
///     canvas.polycurve_begin();
///     for (x, y, code) in data {
///         canvas.polycurve_add(x, y, code);
///     }
///     canvas.polycurve_end(true);
///
///     // add canvas to plot
///     let mut plot = Plot::new();
///     plot.add(&canvas);
///
///     // save figure
///     plot.set_range(1.0, 5.0, 0.0, 4.0)
///         .set_frame_borders(false)
///         .set_hide_axes(true)
///         .set_equal_axes(true)
///         .set_show_errors(true);
///     plot.save("/tmp/plotpy/doc_tests/doc_canvas_polycurve.svg")?;
///     Ok(())
/// }
/// ```
///
/// ![doc_canvas_polycurve.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_canvas_polycurve.svg)
///
/// See also integration tests in the [tests directory](https://github.com/cpmech/plotpy/tree/main/tests)
pub struct Canvas {
    // features
    edge_color: String,  // Edge color (shared)
    face_color: String,  // Face color (shared)
    line_width: f64,     // Line width of edge (shared)
    arrow_scale: f64,    // Arrow scale
    arrow_style: String, // Arrow style

    // text
    text_color: String,            // Text color
    text_align_horizontal: String, // Horizontal alignment
    text_align_vertical: String,   // Vertical alignment
    text_fontsize: f64,            // Font size
    text_rotation: f64,            // Text rotation

    // alternative text
    alt_text_color: String,            // Text color
    alt_text_align_horizontal: String, // Horizontal alignment
    alt_text_align_vertical: String,   // Vertical alignment
    alt_text_fontsize: f64,            // Font size
    alt_text_rotation: f64,            // Text rotation

    // options
    stop_clip: bool, // Stop clipping features within margins

    // buffer
    buffer: String, // buffer
}

impl Canvas {
    pub fn new() -> Self {
        Canvas {
            // features
            edge_color: "#427ce5".to_string(),
            face_color: String::new(),
            line_width: 0.0,
            arrow_scale: 0.0,
            arrow_style: String::new(),
            // text
            text_color: "#a81414".to_string(),
            text_align_horizontal: String::new(),
            text_align_vertical: String::new(),
            text_fontsize: 8.0,
            text_rotation: 45.0,
            // alternative text
            alt_text_color: "#343434".to_string(),
            alt_text_align_horizontal: "center".to_string(),
            alt_text_align_vertical: "center".to_string(),
            alt_text_fontsize: 10.0,
            alt_text_rotation: 0.0,
            // options
            stop_clip: false,
            // buffer
            buffer: String::new(),
        }
    }

    /// Draws arc (2D only)
    pub fn draw_arc<T>(&mut self, xc: T, yc: T, r: T, ini_angle: T, fin_angle: T)
    where
        T: std::fmt::Display,
    {
        let opt = self.options_shared();
        write!(
            &mut self.buffer,
            "p=pat.Arc(({},{}),2*{},2*{},theta1={},theta2={},angle=0{})\n\
             plt.gca().add_patch(p)\n",
            xc, yc, r, r, ini_angle, fin_angle, &opt
        )
        .unwrap();
    }

    /// Draws arrow (2D only)
    pub fn draw_arrow<T>(&mut self, xi: T, yi: T, xf: T, yf: T)
    where
        T: std::fmt::Display,
    {
        let opt_shared = self.options_shared();
        let opt_arrow = self.options_arrow();
        write!(
            &mut self.buffer,
            "p=pat.FancyArrowPatch(({},{}),({},{})\
                    ,shrinkA=0,shrinkB=0\
                    ,path_effects=[pff.Stroke(joinstyle='miter')]\
                    {}{})\n\
             plt.gca().add_patch(p)\n",
            xi, yi, xf, yf, &opt_shared, &&opt_arrow,
        )
        .unwrap();
    }

    /// Draws circle (2D only)
    pub fn draw_circle<T>(&mut self, xc: T, yc: T, r: T)
    where
        T: std::fmt::Display,
    {
        let opt = self.options_shared();
        write!(
            &mut self.buffer,
            "p=pat.Circle(({},{}),{}{})\n\
             plt.gca().add_patch(p)\n",
            xc, yc, r, &opt
        )
        .unwrap();
    }

    /// Begins drawing a polycurve (straight segments, quadratic Bezier, and cubic Bezier) (2D only)
    ///
    /// # Warning
    ///
    /// You must call [Canvas::polycurve_add] next, followed by [Canvas::polycurve_end] when finishing adding points.
    /// Otherwise, Python/Matplotlib will fail.
    pub fn polycurve_begin(&mut self) -> &mut Self {
        write!(&mut self.buffer, "dat=[",).unwrap();
        self
    }

    /// Adds point to a polycurve (straight segments, quadratic Bezier, and cubic Bezier) (2D only)
    ///
    /// # Warning
    ///
    /// You must call [Canvas::polycurve_begin] first, otherwise Python/Matplotlib will fail.
    /// Afterwards, you must call [Canvas::polycurve_end] when finishing adding points.
    pub fn polycurve_add<T>(&mut self, x: T, y: T, code: PolyCode) -> &mut Self
    where
        T: std::fmt::Display,
    {
        let keyword = match code {
            PolyCode::MoveTo => "MOVETO",
            PolyCode::LineTo => "LINETO",
            PolyCode::Curve3 => "CURVE3",
            PolyCode::Curve4 => "CURVE4",
        };
        write!(&mut self.buffer, "[pth.Path.{},({},{})],", keyword, x, y).unwrap();
        self
    }

    /// Ends drawing a polycurve (straight segments, quadratic Bezier, and cubic Bezier) (2D only)
    ///
    /// # Warning
    ///
    /// This function must be the last one called after [Canvas::polycurve_begin] and [Canvas::polycurve_add].
    /// Otherwise, Python/Matplotlib will fail.
    pub fn polycurve_end(&mut self, closed: bool) -> &mut Self {
        if closed {
            write!(&mut self.buffer, "[pth.Path.CLOSEPOLY,(None,None)]").unwrap();
        }
        let opt = self.options_shared();
        write!(
            &mut self.buffer,
            "]\n\
            cmd,pts=zip(*dat)\n\
            h=pth.Path(pts,cmd)\n\
            p=pat.PathPatch(h{})\n\
            plt.gca().add_patch(p)\n",
            &opt
        )
        .unwrap();
        self
    }

    /// Draws polyline with straight segments, quadratic Bezier, or cubic Bezier (2D only)
    ///
    /// **Note:** The first and last commands are ignored.
    pub fn draw_polycurve<'a, T, U>(&mut self, points: &'a T, codes: &[PolyCode], closed: bool) -> Result<(), StrError>
    where
        T: AsMatrix<'a, U>,
        U: 'a + std::fmt::Display,
    {
        let (npoint, ndim) = points.size();
        if npoint < 3 {
            return Err("npoint must be ≥ 3");
        }
        if ndim != 2 {
            return Err("ndim must be equal to 2");
        }
        if codes.len() != npoint {
            return Err("codes.len() must be equal to npoint");
        }
        write!(
            &mut self.buffer,
            "dat=[[pth.Path.MOVETO,({},{})]",
            points.at(0, 0),
            points.at(0, 1)
        )
        .unwrap();
        for i in 1..npoint {
            let keyword = match codes[i] {
                PolyCode::MoveTo => "MOVETO",
                PolyCode::LineTo => "LINETO",
                PolyCode::Curve3 => "CURVE3",
                PolyCode::Curve4 => "CURVE4",
            };
            write!(
                &mut self.buffer,
                ",[pth.Path.{},({},{})]",
                keyword,
                points.at(i, 0),
                points.at(i, 1)
            )
            .unwrap();
        }
        if closed {
            write!(&mut self.buffer, ",[pth.Path.CLOSEPOLY,(None,None)]").unwrap();
        }
        let opt = self.options_shared();
        write!(
            &mut self.buffer,
            "]\n\
            cmd,pts=zip(*dat)\n\
            h=pth.Path(pts,cmd)\n\
            p=pat.PathPatch(h{})\n\
            plt.gca().add_patch(p)\n",
            &opt
        )
        .unwrap();
        Ok(())
    }

    /// Begins adding points to a 3D polyline
    ///
    /// # Warning
    ///
    /// This function must be followed by [Canvas::polyline_3d_add] and [Canvas::polyline_3d_end],
    /// otherwise Python/Matplotlib will fail
    pub fn polyline_3d_begin(&mut self) -> &mut Self {
        write!(&mut self.buffer, "xyz=np.array([").unwrap();
        self
    }

    /// Adds point to a 3D polyline
    ///
    /// # Warning
    ///
    /// This function must be called after [Canvas::polyline_3d_begin] and must be followed by [Canvas::polyline_3d_end],
    /// otherwise Python/Matplotlib will fail.
    pub fn polyline_3d_add<T>(&mut self, x: T, y: T, z: T) -> &mut Self
    where
        T: std::fmt::Display,
    {
        write!(&mut self.buffer, "[{},{},{}],", x, y, z).unwrap();
        self
    }

    /// Ends adding points to a 3D polyline
    ///
    /// # Warning
    ///
    /// This function must be called after [Canvas::polyline_3d_begin] and [Canvas::polyline_3d_add],
    /// otherwise Python/Matplotlib will fail.
    pub fn polyline_3d_end(&mut self) -> &mut Self {
        let opt = self.options_line_3d();
        write!(
            &mut self.buffer,
            "])\nax3d().plot(xyz[:,0],xyz[:,1],xyz[:,2]{})\n",
            &opt
        )
        .unwrap();
        self
    }

    /// Draws polyline (2D or 3D)
    pub fn draw_polyline<'a, T, U>(&mut self, points: &'a T, closed: bool)
    where
        T: AsMatrix<'a, U>,
        U: 'a + std::fmt::Display,
    {
        let (npoint, ndim) = points.size();
        if npoint < 2 {
            return;
        }
        if ndim == 2 {
            write!(
                &mut self.buffer,
                "dat=[[pth.Path.MOVETO,({},{})]",
                points.at(0, 0),
                points.at(0, 1)
            )
            .unwrap();
            for i in 1..npoint {
                write!(
                    &mut self.buffer,
                    ",[pth.Path.LINETO,({},{})]",
                    points.at(i, 0),
                    points.at(i, 1)
                )
                .unwrap();
            }
            if closed {
                write!(&mut self.buffer, ",[pth.Path.CLOSEPOLY,(None,None)]").unwrap();
            }
            let opt = self.options_shared();
            write!(
                &mut self.buffer,
                "]\n\
                cmd,pts=zip(*dat)\n\
                h=pth.Path(pts,cmd)\n\
                p=pat.PathPatch(h{})\n\
                plt.gca().add_patch(p)\n",
                &opt
            )
            .unwrap();
        }
        if ndim == 3 {
            self.polyline_3d_begin();
            for i in 0..npoint {
                self.polyline_3d_add(points.at(i, 0), points.at(i, 1), points.at(i, 2));
            }
            if closed && npoint > 2 {
                self.polyline_3d_add(points.at(0, 0), points.at(0, 1), points.at(0, 2));
            }
            self.polyline_3d_end();
        }
    }

    /// Draws a 2D or 3D grid
    ///
    /// # Input
    ///
    /// * `xmin, xmax` -- min and max coordinates (len = 2 or 3 == ndim)
    /// * `ndiv` -- number of divisions along each dimension (len = 2 or 3 == ndim)
    pub fn draw_grid(
        &mut self,
        xmin: &[f64],
        xmax: &[f64],
        ndiv: &[usize],
        with_point_ids: bool,
        with_cell_ids: bool,
    ) -> Result<(), StrError> {
        // check input
        let ndim = ndiv.len();
        if ndim < 2 || ndim > 3 {
            return Err("len(ndiv) == ndim must be 2 or 3");
        }
        if xmin.len() != ndim {
            return Err("size of xmin must equal ndim == len(ndiv)");
        }
        if xmax.len() != ndim {
            return Err("size of xmax must equal ndim == len(ndiv)");
        }

        // compute delta
        let mut npoint = [1; 3];
        let mut delta = [0.0; 3];
        for i in 0..ndim {
            npoint[i] = ndiv[i] + 1;
            delta[i] = xmax[i] - xmin[i];
            if delta[i] <= 0.0 {
                return Err("xmax must be greater than xmin");
            }
            delta[i] /= ndiv[i] as f64;
        }

        // auxiliary points
        let mut a = [0.0; 3];
        let mut b = [0.0; 3];

        // loop over lines
        if ndim == 2 {
            write!(&mut self.buffer, "dat=[\n").unwrap();
        }
        let opt = self.options_shared();
        let mut id_point = 0;
        for k in 0..npoint[2] {
            if ndim == 3 {
                a[2] = xmin[2] + delta[2] * (k as f64);
                b[2] = a[2];
            }

            // vertical lines
            a[1] = xmin[1];
            b[1] = xmax[1];
            for i in 0..npoint[0] {
                a[0] = xmin[0] + delta[0] * (i as f64);
                b[0] = a[0];
                self.line(ndim, &a, &b);
            }

            // horizontal lines
            a[0] = xmin[0];
            b[0] = xmax[0];
            for j in 0..npoint[1] {
                a[1] = xmin[1] + delta[1] * (j as f64);
                b[1] = a[1];
                self.line(ndim, &a, &b);
            }

            // add patch
            if ndim == 2 {
                write!(
                    &mut self.buffer,
                    "]\n\
                    cmd,pts=zip(*dat)\n\
                    h=pth.Path(pts,cmd)\n\
                    p=pat.PathPatch(h{})\n\
                    plt.gca().add_patch(p)\n",
                    &opt
                )
                .unwrap();
            }

            // labels
            if with_point_ids {
                for j in 0..npoint[1] {
                    a[1] = xmin[1] + delta[1] * (j as f64);
                    for i in 0..npoint[0] {
                        a[0] = xmin[0] + delta[0] * (i as f64);
                        let txt = format!("{}", id_point);
                        self.text(ndim, &a, &txt, false);
                        id_point += 1;
                    }
                }
            }
        }

        // cell ids
        if with_cell_ids {
            let mut id_cell = 0;
            let nz = if ndim == 2 { 1 } else { ndiv[2] };
            for k in 0..nz {
                if ndim == 3 {
                    a[2] = xmin[2] + delta[2] * (k as f64);
                    b[2] = a[2] + delta[2] / 2.0;
                }
                for j in 0..ndiv[1] {
                    a[1] = xmin[1] + delta[1] * (j as f64);
                    b[1] = a[1] + delta[1] / 2.0;
                    for i in 0..ndiv[0] {
                        a[0] = xmin[0] + delta[0] * (i as f64);
                        b[0] = a[0] + delta[0] / 2.0;
                        let txt = format!("{}", id_cell);
                        self.text(ndim, &b, &txt, true);
                        id_cell += 1;
                    }
                }
            }
        }

        // z-lines
        if ndim == 3 {
            a[2] = xmin[2];
            b[2] = xmax[2];
            for j in 0..npoint[1] {
                a[1] = xmin[1] + delta[1] * (j as f64);
                b[1] = a[1];
                for i in 0..npoint[0] {
                    a[0] = xmin[0] + delta[0] * (i as f64);
                    b[0] = a[0];
                    self.line(ndim, &a, &b);
                }
            }
        }

        // adjust limits
        self.limits(ndim, xmin, xmax);

        // done
        Ok(())
    }

    /// Sets the edge color (shared among features)
    pub fn set_edge_color(&mut self, color: &str) -> &mut Self {
        self.edge_color = String::from(color);
        self
    }

    /// Sets the face color (shared among features)
    pub fn set_face_color(&mut self, color: &str) -> &mut Self {
        self.face_color = String::from(color);
        self
    }

    /// Sets the line width of edge (shared among features)
    pub fn set_line_width(&mut self, width: f64) -> &mut Self {
        self.line_width = width;
        self
    }

    /// Sets the arrow scale
    pub fn set_arrow_scale(&mut self, scale: f64) -> &mut Self {
        self.arrow_scale = scale;
        self
    }

    /// Sets the arrow style
    ///
    /// Options:
    ///
    /// * "`-`"      -- Curve         : None
    /// * "`->`"     -- CurveB        : head_length=0.4,head_width=0.2
    /// * "`-[`"     -- BracketB      : widthB=1.0,lengthB=0.2,angleB=None
    /// * "`-|>`"    -- CurveFilledB  : head_length=0.4,head_width=0.2
    /// * "`<-`"     -- CurveA        : head_length=0.4,head_width=0.2
    /// * "`<->`"    -- CurveAB       : head_length=0.4,head_width=0.2
    /// * "`<|-`"    -- CurveFilledA  : head_length=0.4,head_width=0.2
    /// * "`<|-|>`"  -- CurveFilledAB : head_length=0.4,head_width=0.2
    /// * "`]-`"     -- BracketA      : widthA=1.0,lengthA=0.2,angleA=None
    /// * "`]-[`"    -- BracketAB     : widthA=1.0,lengthA=0.2,angleA=None,widthB=1.0,lengthB=0.2,angleB=None
    /// * "`fancy`"  -- Fancy         : head_length=0.4,head_width=0.4,tail_width=0.4
    /// * "`simple`" -- Simple        : head_length=0.5,head_width=0.5,tail_width=0.2
    /// * "`wedge`"  -- Wedge         : tail_width=0.3,shrink_factor=0.5
    /// * "`|-|`"    -- BarAB         : widthA=1.0,angleA=None,widthB=1.0,angleB=None
    /// * As defined in <https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.FancyArrowPatch.html>
    pub fn set_arrow_style(&mut self, style: &str) -> &mut Self {
        self.arrow_style = String::from(style);
        self
    }

    /// Sets the text color
    pub fn set_text_color(&mut self, color: &str) -> &mut Self {
        self.text_color = String::from(color);
        self
    }

    /// Sets the text horizontal alignment
    ///
    /// Options: "center", "left", "right"
    pub fn set_text_align_horizontal(&mut self, option: &str) -> &mut Self {
        self.text_align_horizontal = String::from(option);
        self
    }

    /// Sets the text vertical alignment
    ///
    /// Options: "center", "top", "bottom", "baseline", "center_baseline"
    pub fn set_text_align_vertical(&mut self, option: &str) -> &mut Self {
        self.text_align_vertical = String::from(option);
        self
    }

    /// Sets the text font size
    pub fn set_text_fontsize(&mut self, fontsize: f64) -> &mut Self {
        self.text_fontsize = fontsize;
        self
    }

    /// Sets the text rotation
    pub fn set_text_rotation(&mut self, rotation: f64) -> &mut Self {
        self.text_rotation = rotation;
        self
    }

    /// Sets the alternative text color
    pub fn set_alt_text_color(&mut self, color: &str) -> &mut Self {
        self.alt_text_color = String::from(color);
        self
    }

    /// Sets the alternative text horizontal alignment
    ///
    /// Options: "center", "left", "right"
    pub fn set_alt_text_align_horizontal(&mut self, option: &str) -> &mut Self {
        self.alt_text_align_horizontal = String::from(option);
        self
    }

    /// Sets the alternative text vertical alignment
    ///
    /// Options: "center", "top", "bottom", "baseline", "center_baseline"
    pub fn set_alt_text_align_vertical(&mut self, option: &str) -> &mut Self {
        self.alt_text_align_vertical = String::from(option);
        self
    }

    /// Sets the alternative text font size
    pub fn set_alt_text_fontsize(&mut self, fontsize: f64) -> &mut Self {
        self.alt_text_fontsize = fontsize;
        self
    }

    /// Sets the alternative text rotation
    pub fn set_alt_text_rotation(&mut self, rotation: f64) -> &mut Self {
        self.alt_text_rotation = rotation;
        self
    }

    /// Sets the flag to stop clipping features within margins
    pub fn set_stop_clip(&mut self, flag: bool) -> &mut Self {
        self.stop_clip = flag;
        self
    }

    /// Returns shared options
    fn options_shared(&self) -> String {
        let mut opt = String::new();
        if self.edge_color != "" {
            write!(&mut opt, ",edgecolor='{}'", self.edge_color).unwrap();
        }
        if self.face_color != "" {
            write!(&mut opt, ",facecolor='{}'", self.face_color).unwrap();
        }
        if self.line_width > 0.0 {
            write!(&mut opt, ",linewidth={}", self.line_width).unwrap();
        }
        if self.stop_clip {
            write!(&mut opt, ",clip_on=False").unwrap();
        }
        opt
    }

    /// Returns options for arrows
    fn options_arrow(&self) -> String {
        let mut opt = String::new();
        if self.arrow_scale > 0.0 {
            write!(&mut opt, ",mutation_scale={}", self.arrow_scale).unwrap();
        }
        if self.arrow_style != "" {
            write!(&mut opt, ",arrowstyle='{}'", self.arrow_style).unwrap();
        }
        opt
    }

    /// Returns options for text
    fn options_text(&self) -> String {
        let mut opt = String::new();
        if self.text_color != "" {
            write!(&mut opt, ",color='{}'", self.text_color).unwrap();
        }
        if self.text_align_horizontal != "" {
            write!(&mut opt, ",ha='{}'", self.text_align_horizontal).unwrap();
        }
        if self.text_align_vertical != "" {
            write!(&mut opt, ",va='{}'", self.text_align_vertical).unwrap();
        }
        if self.text_fontsize > 0.0 {
            write!(&mut opt, ",fontsize={}", self.text_fontsize).unwrap();
        }
        if self.text_rotation > 0.0 {
            write!(&mut opt, ",rotation={}", self.text_rotation).unwrap();
        }
        opt
    }

    /// Returns options for alternative text
    fn options_alt_text(&self) -> String {
        let mut opt = String::new();
        if self.alt_text_color != "" {
            write!(&mut opt, ",color='{}'", self.alt_text_color).unwrap();
        }
        if self.alt_text_align_horizontal != "" {
            write!(&mut opt, ",ha='{}'", self.alt_text_align_horizontal).unwrap();
        }
        if self.alt_text_align_vertical != "" {
            write!(&mut opt, ",va='{}'", self.alt_text_align_vertical).unwrap();
        }
        if self.alt_text_fontsize > 0.0 {
            write!(&mut opt, ",fontsize={}", self.alt_text_fontsize).unwrap();
        }
        if self.alt_text_rotation > 0.0 {
            write!(&mut opt, ",rotation={}", self.alt_text_rotation).unwrap();
        }
        opt
    }

    /// Returns options for 3D line
    fn options_line_3d(&self) -> String {
        let mut opt = String::new();
        if self.edge_color != "" {
            write!(&mut opt, ",color='{}'", self.edge_color).unwrap();
        }
        if self.line_width > 0.0 {
            write!(&mut opt, ",linewidth={}", self.line_width).unwrap();
        }
        opt
    }

    /// Draws 2D or 3D line
    fn line(&mut self, ndim: usize, a: &[f64; 3], b: &[f64; 3]) {
        if ndim == 2 {
            write!(
                &mut self.buffer,
                "    [pth.Path.MOVETO,({},{})],[pth.Path.LINETO,({},{})],\n",
                a[0], a[1], b[0], b[1]
            )
            .unwrap();
        } else {
            let opt = self.options_line_3d();
            write!(
                &mut self.buffer,
                "ax3d().plot([{},{}],[{},{}],[{},{}]{})\n",
                a[0], b[0], a[1], b[1], a[2], b[2], opt,
            )
            .unwrap();
        }
    }

    /// Draws 2D or 3D text
    fn text(&mut self, ndim: usize, a: &[f64; 3], txt: &str, alternative: bool) {
        let opt = if alternative {
            self.options_alt_text()
        } else {
            self.options_text()
        };
        if ndim == 2 {
            write!(&mut self.buffer, "plt.text({},{},'{}'{})\n", a[0], a[1], txt, &opt).unwrap();
        } else {
            write!(
                &mut self.buffer,
                "ax3d().text({},{},{},'{}'{})\n",
                a[0], a[1], a[2], txt, &opt
            )
            .unwrap();
        }
    }

    /// Adjust 2D or 3D limits
    fn limits(&mut self, ndim: usize, xmin: &[f64], xmax: &[f64]) {
        const FACTOR: f64 = 0.1;
        let mut gap = [0.0; 3];
        for i in 0..ndim {
            gap[i] = (xmax[i] - xmin[i]) * FACTOR;
        }
        if ndim == 2 {
            write!(
                &mut self.buffer,
                "plt.axis([{},{},{},{}])\n",
                xmin[0] - gap[0],
                xmax[0] + gap[0],
                xmin[1] - gap[1],
                xmax[1] + gap[1]
            )
            .unwrap();
        } else {
            write!(
                &mut self.buffer,
                "ax3d().set_xlim3d({},{})\n\
                 ax3d().set_ylim3d({},{})\n\
                 ax3d().set_zlim3d({},{})\n",
                xmin[0] - gap[0],
                xmax[0] + gap[0],
                xmin[1] - gap[1],
                xmax[1] + gap[1],
                xmin[2] - gap[2],
                xmax[2] + gap[2]
            )
            .unwrap();
        }
    }
}

impl GraphMaker for Canvas {
    fn get_buffer<'a>(&'a self) -> &'a String {
        &self.buffer
    }
    fn clear_buffer(&mut self) {
        self.buffer.clear();
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::Canvas;
    use crate::{GraphMaker, PolyCode};

    #[test]
    fn derive_works() {
        let code = PolyCode::Curve3;
        let clone = code.clone();
        let correct = "Curve3";
        assert_eq!(format!("{:?}", code), correct);
        assert_eq!(format!("{:?}", clone), correct);
    }

    #[test]
    fn new_works() {
        let canvas = Canvas::new();
        assert_eq!(canvas.edge_color.len(), 7);
        assert_eq!(canvas.face_color.len(), 0);
        assert_eq!(canvas.line_width, 0.0);
        assert_eq!(canvas.arrow_scale, 0.0);
        assert_eq!(canvas.arrow_style.len(), 0);
        assert_eq!(canvas.text_color.len(), 7);
        assert_eq!(canvas.text_align_horizontal.len(), 0);
        assert_eq!(canvas.text_align_vertical.len(), 0);
        assert_eq!(canvas.text_fontsize, 8.0);
        assert_eq!(canvas.text_rotation, 45.0);
        assert_eq!(canvas.buffer.len(), 0);
    }

    #[test]
    fn options_shared_works() {
        let mut canvas = Canvas::new();
        canvas
            .set_edge_color("red")
            .set_face_color("blue")
            .set_line_width(2.5)
            .set_stop_clip(true);
        let opt = canvas.options_shared();
        assert_eq!(
            opt,
            ",edgecolor='red'\
             ,facecolor='blue'\
             ,linewidth=2.5\
             ,clip_on=False"
        );
    }

    #[test]
    fn options_arrow_works() {
        let mut canvas = Canvas::new();
        canvas.set_arrow_scale(25.0).set_arrow_style("fancy");
        let opt = canvas.options_arrow();
        assert_eq!(
            opt,
            ",mutation_scale=25\
             ,arrowstyle='fancy'"
        );
    }

    #[test]
    fn options_text_works() {
        let mut canvas = Canvas::new();
        canvas
            .set_text_color("red")
            .set_text_align_horizontal("center")
            .set_text_align_vertical("center")
            .set_text_fontsize(8.0)
            .set_text_rotation(45.0);
        let opt = canvas.options_text();
        assert_eq!(
            opt,
            ",color='red'\
             ,ha='center'\
             ,va='center'\
             ,fontsize=8\
             ,rotation=45"
        );
    }

    #[test]
    fn options_alt_text_works() {
        let mut canvas = Canvas::new();
        canvas
            .set_alt_text_color("blue")
            .set_alt_text_align_horizontal("right")
            .set_alt_text_align_vertical("bottom")
            .set_alt_text_fontsize(10.0)
            .set_alt_text_rotation(30.0);
        let opt = canvas.options_alt_text();
        assert_eq!(
            opt,
            ",color='blue'\
             ,ha='right'\
             ,va='bottom'\
             ,fontsize=10\
             ,rotation=30"
        );
    }

    #[test]
    fn options_line_3d_works() {
        let mut canvas = Canvas::new();
        canvas.set_edge_color("red");
        let opt = canvas.options_line_3d();
        assert_eq!(opt, ",color='red'");

        let mut canvas = Canvas::new();
        canvas.set_edge_color("red").set_line_width(5.0);
        let opt = canvas.options_line_3d();
        assert_eq!(opt, ",color='red',linewidth=5");
    }

    #[test]
    fn line_works() {
        let mut canvas = Canvas::new();
        let a = [0.0; 3];
        let b = [0.0; 3];
        canvas.line(2, &a, &b);
        canvas.line(3, &a, &b);
        assert_eq!(
            canvas.buffer,
            "\x20\x20\x20\x20[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(0,0)],\n\
             ax3d().plot([0,0],[0,0],[0,0],color='#427ce5')\n"
        );
        canvas.clear_buffer();
        assert_eq!(canvas.buffer, "");
    }

    #[test]
    fn text_works() {
        let mut canvas = Canvas::new();
        let a = [0.0; 3];
        canvas.text(2, &a, "hello", false);
        canvas.text(3, &a, "hello", true);
        assert_eq!(
            canvas.buffer,
            "plt.text(0,0,'hello',color='#a81414',fontsize=8,rotation=45)\n\
             ax3d().text(0,0,0,'hello',color='#343434',ha='center',va='center',fontsize=10)\n"
        );
    }

    #[test]
    fn limits_works() {
        let mut canvas = Canvas::new();
        let xmin = [0.0; 3];
        let xmax = [0.0; 3];
        canvas.limits(2, &xmin, &xmax);
        canvas.limits(3, &xmin, &xmax);
        assert_eq!(
            canvas.buffer,
            "plt.axis([0,0,0,0])\n\
            ax3d().set_xlim3d(0,0)\n\
            ax3d().set_ylim3d(0,0)\n\
            ax3d().set_zlim3d(0,0)\n"
        );
    }

    #[test]
    fn arc_works() {
        let mut canvas = Canvas::new();
        canvas.draw_arc(0.0, 0.0, 1.0, 30.0, 60.0);
        let b: &str = "p=pat.Arc((0,0),2*1,2*1,theta1=30,theta2=60,angle=0,edgecolor='#427ce5')\n\
                       plt.gca().add_patch(p)\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn arrow_woks() {
        let mut canvas = Canvas::new();
        canvas.draw_arrow(0.0, 0.0, 1.0, 1.0);
        let b: &str =
            "p=pat.FancyArrowPatch((0,0),(1,1),shrinkA=0,shrinkB=0,path_effects=[pff.Stroke(joinstyle='miter')],edgecolor='#427ce5')\n\
             plt.gca().add_patch(p)\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn circle_works() {
        let mut canvas = Canvas::new();
        canvas.draw_circle(0.0, 0.0, 1.0);
        let b: &str = "p=pat.Circle((0,0),1,edgecolor='#427ce5')\n\
                       plt.gca().add_patch(p)\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn polycurve_methods_work() {
        // note the following sequence of codes won't work in Matplotlib because Curve3 and Curve4 are wrong
        let mut canvas = Canvas::new();
        canvas.polycurve_begin();
        assert_eq!(canvas.buffer, "dat=[");
        canvas.polycurve_add(0, 0, PolyCode::MoveTo);
        assert_eq!(canvas.buffer, "dat=[[pth.Path.MOVETO,(0,0)],");
        canvas.polycurve_add(1, 0, PolyCode::LineTo);
        assert_eq!(canvas.buffer, "dat=[[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(1,0)],");
        canvas.polycurve_add(2, 0, PolyCode::Curve3);
        assert_eq!(
            canvas.buffer,
            "dat=[[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(1,0)],[pth.Path.CURVE3,(2,0)],"
        );
        canvas.polycurve_add(3, 0, PolyCode::Curve4);
        assert_eq!(
            canvas.buffer,
            "dat=[[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(1,0)],[pth.Path.CURVE3,(2,0)],[pth.Path.CURVE4,(3,0)],"
        );
        canvas.polycurve_end(true);
        assert_eq!(
            canvas.buffer,
            "dat=[[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(1,0)],[pth.Path.CURVE3,(2,0)],[pth.Path.CURVE4,(3,0)],[pth.Path.CLOSEPOLY,(None,None)]]\n\
            cmd,pts=zip(*dat)\n\
            h=pth.Path(pts,cmd)\n\
            p=pat.PathPatch(h,edgecolor='#427ce5')\n\
            plt.gca().add_patch(p)\n"
        );
    }

    #[test]
    fn polycurve_capture_errors() {
        let mut canvas = Canvas::new();
        assert_eq!(
            canvas.draw_polycurve(&[[0, 0]], &[PolyCode::MoveTo], true).err(),
            Some("npoint must be ≥ 3")
        );
        assert_eq!(
            canvas
                .draw_polycurve(
                    &[[0], [0], [0]],
                    &[PolyCode::MoveTo, PolyCode::LineTo, PolyCode::LineTo],
                    true
                )
                .err(),
            Some("ndim must be equal to 2")
        );
        assert_eq!(
            canvas
                .draw_polycurve(&[[0, 0], [0, 0], [0, 0]], &[PolyCode::MoveTo], true)
                .err(),
            Some("codes.len() must be equal to npoint")
        );
    }

    #[test]
    fn polycurve_works() {
        let mut canvas = Canvas::new();
        let points = &[[0, 0], [1, 0], [1, 1]];
        let codes = &[PolyCode::MoveTo, PolyCode::Curve3, PolyCode::Curve3];
        canvas.draw_polycurve(points, codes, true).unwrap();
        let b: &str = "dat=[[pth.Path.MOVETO,(0,0)],[pth.Path.CURVE3,(1,0)],[pth.Path.CURVE3,(1,1)],[pth.Path.CLOSEPOLY,(None,None)]]\n\
                       cmd,pts=zip(*dat)\n\
                       h=pth.Path(pts,cmd)\n\
                       p=pat.PathPatch(h,edgecolor='#427ce5')\n\
                       plt.gca().add_patch(p)\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn polyline_works_2d() {
        let mut canvas = Canvas::new();
        let points = &[[1.0, 1.0], [2.0, 1.0], [1.5, 1.866]];
        canvas.draw_polyline(points, true);
        let b: &str = "dat=[[pth.Path.MOVETO,(1,1)],[pth.Path.LINETO,(2,1)],[pth.Path.LINETO,(1.5,1.866)],[pth.Path.CLOSEPOLY,(None,None)]]\n\
                       cmd,pts=zip(*dat)\n\
                       h=pth.Path(pts,cmd)\n\
                       p=pat.PathPatch(h,edgecolor='#427ce5')\n\
                       plt.gca().add_patch(p)\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn polyline_3d_methods_work() {
        let mut canvas = Canvas::new();
        canvas
            .polyline_3d_begin()
            .polyline_3d_add(1, 2, 3)
            .polyline_3d_add(4, 5, 6)
            .polyline_3d_end();
        let b: &str = "\
            xyz=np.array([[1,2,3],[4,5,6],])\n\
            ax3d().plot(xyz[:,0],xyz[:,1],xyz[:,2],color='#427ce5')\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn polyline_works_3d() {
        let mut nothing = Canvas::new();
        nothing.draw_polyline(&[[0.0, 0.0]], true);
        assert_eq!(nothing.buffer, "");

        #[rustfmt::skip]
        let points = &[
            [2.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, 1.0, 3.0],
            [2.0, 1.0, 3.0],
        ];

        let mut open = Canvas::new();
        open.draw_polyline(points, false);
        let b: &str = "\
            xyz=np.array([[2,1,0],[0,1,0],[0,1,3],[2,1,3],])\n\
            ax3d().plot(xyz[:,0],xyz[:,1],xyz[:,2],color='#427ce5')\n";
        assert_eq!(open.buffer, b);

        let mut closed = Canvas::new();
        closed.draw_polyline(points, true);
        let b: &str = "\
            xyz=np.array([[2,1,0],[0,1,0],[0,1,3],[2,1,3],[2,1,0],])\n\
            ax3d().plot(xyz[:,0],xyz[:,1],xyz[:,2],color='#427ce5')\n";
        assert_eq!(closed.buffer, b);

        #[rustfmt::skip]
        let points = &[
            [2.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ];

        let mut closed_few_points = Canvas::new();
        closed_few_points.draw_polyline(points, true);
        let b: &str = "\
            xyz=np.array([[2,1,0],[0,1,0],])\n\
            ax3d().plot(xyz[:,0],xyz[:,1],xyz[:,2],color='#427ce5')\n";
        assert_eq!(closed_few_points.buffer, b);
    }

    #[test]
    fn grid_fails_on_wrong_input() {
        let mut canvas = Canvas::new();
        let res = canvas.draw_grid(&[0.0, 0.0], &[1.0, 1.0], &[1], true, false);
        assert_eq!(res, Err("len(ndiv) == ndim must be 2 or 3"));
        let res = canvas.draw_grid(&[0.0], &[1.0, 1.0], &[1, 1], true, false);
        assert_eq!(res, Err("size of xmin must equal ndim == len(ndiv)"));
        let res = canvas.draw_grid(&[0.0, 0.0], &[1.0], &[1, 1], true, false);
        assert_eq!(res, Err("size of xmax must equal ndim == len(ndiv)"));
        let res = canvas.draw_grid(&[0.0, 0.0], &[0.0, 1.0], &[1, 1], true, false);
        assert_eq!(res, Err("xmax must be greater than xmin"));
    }

    #[test]
    fn grid_no_ids_works() {
        let mut canvas = Canvas::new();
        canvas
            .draw_grid(&[0.0, 0.0], &[1.0, 1.0], &[1, 1], false, false)
            .unwrap();
        let b: &str = "dat=[\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(0,1)],\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(1,0)],[pth.Path.LINETO,(1,1)],\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(1,0)],\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(0,1)],[pth.Path.LINETO,(1,1)],\n\
                      ]\n\
                      cmd,pts=zip(*dat)\n\
                      h=pth.Path(pts,cmd)\n\
                      p=pat.PathPatch(h,edgecolor='#427ce5')\n\
                      plt.gca().add_patch(p)\n\
                      plt.axis([-0.1,1.1,-0.1,1.1])\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn grid_2d_works() {
        let mut canvas = Canvas::new();
        canvas.draw_grid(&[0.0, 0.0], &[1.0, 1.0], &[1, 1], true, true).unwrap();
        let b: &str = "dat=[\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(0,1)],\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(1,0)],[pth.Path.LINETO,(1,1)],\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(0,0)],[pth.Path.LINETO,(1,0)],\n\
                      \x20\x20\x20\x20[pth.Path.MOVETO,(0,1)],[pth.Path.LINETO,(1,1)],\n\
                      ]\n\
                      cmd,pts=zip(*dat)\n\
                      h=pth.Path(pts,cmd)\n\
                      p=pat.PathPatch(h,edgecolor='#427ce5')\n\
                      plt.gca().add_patch(p)\n\
                      plt.text(0,0,'0',color='#a81414',fontsize=8,rotation=45)\n\
                      plt.text(1,0,'1',color='#a81414',fontsize=8,rotation=45)\n\
                      plt.text(0,1,'2',color='#a81414',fontsize=8,rotation=45)\n\
                      plt.text(1,1,'3',color='#a81414',fontsize=8,rotation=45)\n\
                      plt.text(0.5,0.5,'0',color='#343434',ha='center',va='center',fontsize=10)\n\
                      plt.axis([-0.1,1.1,-0.1,1.1])\n";
        assert_eq!(canvas.buffer, b);
    }

    #[test]
    fn grid_3d_works() {
        let mut canvas = Canvas::new();
        canvas
            .draw_grid(&[0.0, 0.0, 0.0], &[1.0, 1.0, 1.0], &[1, 1, 1], true, true)
            .unwrap();
        let b: &str = "\
                       ax3d().plot([0,0],[0,1],[0,0],color='#427ce5')\n\
                       ax3d().plot([1,1],[0,1],[0,0],color='#427ce5')\n\
                       ax3d().plot([0,1],[0,0],[0,0],color='#427ce5')\n\
                       ax3d().plot([0,1],[1,1],[0,0],color='#427ce5')\n\
                       ax3d().text(0,0,0,'0',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().text(1,0,0,'1',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().text(0,1,0,'2',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().text(1,1,0,'3',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().plot([0,0],[0,1],[1,1],color='#427ce5')\n\
                       ax3d().plot([1,1],[0,1],[1,1],color='#427ce5')\n\
                       ax3d().plot([0,1],[0,0],[1,1],color='#427ce5')\n\
                       ax3d().plot([0,1],[1,1],[1,1],color='#427ce5')\n\
                       ax3d().text(0,0,1,'4',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().text(1,0,1,'5',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().text(0,1,1,'6',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().text(1,1,1,'7',color='#a81414',fontsize=8,rotation=45)\n\
                       ax3d().text(0.5,0.5,0.5,'0',color='#343434',ha='center',va='center',fontsize=10)\n\
                       ax3d().plot([0,0],[0,0],[0,1],color='#427ce5')\n\
                       ax3d().plot([1,1],[0,0],[0,1],color='#427ce5')\n\
                       ax3d().plot([0,0],[1,1],[0,1],color='#427ce5')\n\
                       ax3d().plot([1,1],[1,1],[0,1],color='#427ce5')\n\
                       ax3d().set_xlim3d(-0.1,1.1)\n\
                       ax3d().set_ylim3d(-0.1,1.1)\n\
                       ax3d().set_zlim3d(-0.1,1.1)\n";
        assert_eq!(canvas.buffer, b);
    }
}