summaryrefslogtreecommitdiff
path: root/graphics/metapost/contrib/macros/mptrees/mptrees.mp
blob: f31080b61c929f38a58f9652cdaf9dbe52986f05 (plain)
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
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                        mptrees.mp                          %%
%%               Probability trees with MetaPost              %%
%%                    o.peault@posteo.net                     %%
%%                Version 24.02 (February 2024)               %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
string mptreesversion;
mptreesversion:="24.02";
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3 of this license
% or (at your option) any later version.  The latest version of this
% license is in http://www.latex-project.org/lppl.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
message "mptrees version " & mptreesversion;

if not known mplib: input latexmp fi;

boolean arbres_latexmp;
arbres_latexmp:=true;


boolean mpt_begtr; % is begintree used ?
mpt_begtr:=false;
numeric mpt_passe;
mpt_passe:=1;


% begintree permet de stocker les instructions de la figure dans mpt_tree pour calculs éventuels

def begintree=
 mpt_begtr:=true;
 scantokens "def mpt_tree="
enddef;

let endtree = enddef;

numeric mpt_tmpdirtree;


% à la fin de begintree, on exécute deux fois mpt_tree
extra_endfig:="if mpt_begtr: mpt_tree currentpicture:=nullpicture; mpt_passe:=2; AdjustNodes; mpt_tree; mpt_begtr:=false;mpt_passe:=1 fi;" & extra_endfig;
%extra_endfig:="if mpt_begtr: mpt_tree drawoptions(withcolor blue); mpt_passe:=2;  AdjustNodes; mpt_tree; mpt_begtr:=false;mpt_passe:=1 fi;" & extra_endfig;


numeric posprob;   % position de l'étiquette sur la branche
posprob:=-1;

numeric scaleprob;
scaleprob:=0.85;   % échelle de l'étiquette sur la branche

numeric scaleev;
scaleev:=1;   % échelle de l'évènement

numeric dirtree,dirlabel; % direction de l'arbre
dirtree:=0;
dirlabel:=0;

numeric proboffset;  % redéfinition temporaire de labeloffset pour affichage probas
proboffset:=labeloffset;

numeric endedgeshift;
endedgeshift:=0;

numeric brokenlineratio;
brokenlineratio:=0.2;

numeric linewidth;
linewidth:=0.5bp;

color linecolor;
linecolor:=black;

numeric nodelinewidth; % épaisseur de la ligne autour des noeuds
nodelinewidth:=0.5bp;

pair Orig_arbre[][];  % sommet de l'arbre i,j
Orig_arbre[1][1]:=origin;

pair mpt_dec_ori[][]; % Décalage avec la fin de la branche précédente
mpt_dec_ori[1][1]:=(0,0);

pair mpt_tmp_ori[][];  % Branche i,j,k

pair mpt_br[][][];  % Branche i,j,k

boolean exist.arbre[][]; % L'arbre i,j est-il dessiné ?
exist.arbre[0][1]:=true;


numeric colonne_cour,ligne_cour;
numeric mptLargeur[][],mptHauteur[][];  % diff largeur au rang i, diff hauteur rang i

numeric countligne[];  % nombre de ligne pour le niveau i
for i=0 upto 128: countligne[i]:=0; endfor

%numeric typedec;  % 0 origine dépend de la taille de l'évènement, 1 taille fixée
numeric shiftev;   % taille du décalage dans le cas où typedec=1
shiftev:=-1; % dans ce cas, typedec =0
%extra_beginfig:=extra_beginfig & "shiftev:=-1;"; % remise à zéro en début de figure


numeric typeprob; % affichage de la proba sur la branche
typeprob:=1;

boolean abscoord; % Coordonnée absolues
abscoord=false;

string branchtype,endlabeltype;
branchtype="segment";
endlabeltype="none";

boolean edgearrow;
edgearrow:=false;

numeric endlabelspace;
endlabelspace:=1cm;


% Pour rendre les paramètres "locaux" à chaque figure
extra_beginfig:=extra_beginfig & "init_loc_var;";



% Étiquette au début de l'arbre

picture mptreestartlabel;
mptreestartlabel:=nullpicture;

vardef startlabel(expr s)=
  colonne_cour:=0;
  mptreestartlabel:=labelarbres(s);
  if dirtree<>0: dirlabel:=dirtree fi;
   if dirlabel>0:
     theevlabel(formatnodeev(labelarbres(s)) scaled scaleev,origin,-180+dirlabel)
   else:
     theevlabel(formatnodeev(labelarbres(s)) scaled scaleev,origin,180+dirlabel)
   fi
enddef;


% Étiquettes de fin d'arbre
vardef colonne.endlabel[]=
  @
enddef;

vardef endlabel[][](expr s)=
  save i,j,tmpbranchtype,tmpabscoord,tmps;
  numeric i,j;
  string tmpbranchtype,tmps;
  boolean tmpabscoord;
  i:=colonne.#@;
  j:=@;
  if string s: tmps:= "\strut \phantom{.}" & s else: picture tmps;tmps:=s fi;
  tmpbranchtype:=branchtype;
  branchtype:=endlabeltype;
  tmpabscoord:=abscoord;
  abscoord:=false;
  tree[i][j]((endlabelspace,0) rotated (dirlabel-dirtree))(tmps," ")
  hide(branchtype:=tmpbranchtype;abscoord:=tmpabscoord;)
enddef;


vardef labelarbres(expr s)=
  save p; picture p;
  if picture s:  p=s
  elseif path s: p=image(draw s)
%  elseif string s: p=scantokens("btex" & s & "etex");
  else: p=textext(s)
  fi;
  p
enddef;


% longueur du segment qui traverse la figure p selon l'angle inc
vardef longdir(expr p,inc)=
  if urcorner p = llcorner p:
    0
  elseif (abs inc <= abs angle (urcorner p -llcorner p)) or (abs inc > 180 - abs angle (urcorner p -llcorner p)):
    abs((lrcorner p - llcorner p)/cosd(inc))
  else:
    abs((ulcorner p - llcorner p)/sind(inc))
  fi
enddef;

%%%%%%%%%%%%%%
%% format nodes, leaves and probabilities
%%%%%%%%%%%%%%

% Colors
color nodelinecolor,nodebgcolor,nodefgcolor;
nodelinecolor:=black;
nodebgcolor:=white;
nodefgcolor:=black;

color leavelinecolor,leavebgcolor,leavefgcolor;
leavelinecolor:=black;
leavebgcolor:=white;
leavefgcolor:=black;

color problinecolor,probbgcolor,probfgcolor;
problinecolor:=black;
probbgcolor:=white;
probfgcolor:=black;


% Superellipse
numeric superellipseparam;
superellipseparam:=0.85;

vardef superellipsebox(expr p)=
   save a,b,c,d,l;
   pair a,b,c,d;
   path l;
   l := bbox p;
   a=0.5[lrcorner l,urcorner l];
   b=0.5[urcorner l,ulcorner l];
   c=0.5[ulcorner l,llcorner l];
   d=0.5[llcorner l,lrcorner l];
   if (mpt_isgraph and (not naturalwidth) and (not mpt_isprob)):
      l:=superellipse(1/2 right, 1/2 up, 1/2 left, 1/2 down, .8);
      l:=l scaled nodewidth
   elseif naturalwidth:
      l:=superellipse(a,b,c,d,superellipseparam);
      l:=l shifted -center l
   else:
      l:=superellipse(a,b,c,d,superellipseparam)
   fi;
   l
enddef;

vardef supellformat(expr p,cl,cb,cf)=
image(%
 if (cb<>white) or (mpt_bgc): fill superellipsebox(p) withcolor cb mpt_opbg_ fi;
 draw superellipsebox(p) withcolor cl withpen pencircle scaled nodelinewidth mpt_po_ ;
 if arbrebool or mpt_isprob: draw p withcolor cf mpt_po_  fi;
)
enddef;

% Circle
vardef circlebox(expr p)=
  save a,b,c;
  pair a,b,c;
  a:=urcorner bbox p;
  b:=llcorner bbox p;
  c:=0.5[a,b];
  if (mpt_isgraph and (not naturalwidth) and (not mpt_isprob)):
     fullcircle scaled nodewidth
  elseif naturalwidth:
     fullcircle scaled (abs(b-a)-2)
  else:
     fullcircle scaled (abs(b-a)-2) shifted c
  fi
enddef;

vardef circleformat(expr p,cl,cb,cf)=
image(%
 if (cb<>white) or (mpt_isprobover) or (mpt_bgc): fill circlebox(p) withcolor cb mpt_opbg_ fi;
 draw circlebox(p) withcolor cl withpen pencircle scaled nodelinewidth mpt_po_;
 if arbrebool or mpt_isprob: draw p withcolor cf mpt_po_ fi;
)
enddef;

vardef circleprobformat(expr p,cf)=
image(%
 fill circlebox(p) withcolor background;
 draw p withcolor cf mpt_po_;
)
enddef;

% square
vardef squarebox(expr p)=
  save H,L;
  numeric H,L;
  L=abs((urcorner bbox p) - (ulcorner bbox p));
  H=abs((urcorner bbox p) - (lrcorner bbox p));
  if H>L: L:= H fi;
  if (mpt_isgraph and (not naturalwidth) and (not mpt_isprob)):
    unitsquare shifted (-0.5,-0.5) scaled nodewidth
  elseif naturalwidth:
     unitsquare shifted (-0.5,-0.5) scaled L
  else:
     unitsquare shifted (-0.5,-0.5) scaled L shifted center bbox p
  fi
enddef;

vardef squareformat(expr p,cl,cb,cf)=
image(%
 if (cb<>white) or (mpt_bgc): fill squarebox(p) withcolor cb mpt_opbg_ fi;
 draw squarebox(p) withcolor cl withpen pencircle scaled nodelinewidth mpt_po_;
 if arbrebool or mpt_isprob: draw p withcolor cf mpt_po_  fi;
)
enddef;

% bbox
vardef bboxformat(expr p,cl,cb,cf)=
image(%
 if (cb<>white) or (mpt_bgc): fill bbox p withcolor cb mpt_opbg_ fi;
 draw bbox p withcolor cl withpen pencircle scaled nodelinewidth mpt_po_;
 if arbrebool or mpt_isprob: draw p withcolor cf mpt_po_  fi;
)
enddef;


% Format nodes

string nodeformat,leaveformat,probformat;  %How nodes and leaves should be printed
nodeformat:="";
leaveformat:="";
probformat:="";




vardef formatprob(expr p)=
 save cl,cb,cf;
 color cl,cb,cf;
 cl:=problinecolor;
 cb:=probbgcolor;
 cf:=probfgcolor;
 if probformat="superellipse":
   supellformat(p,cl,cb,cf)
 elseif probformat="circle":
   circleformat(p,cl,cb,cf)
 elseif probformat="square":
   squareformat(p,cl,cb,cf)
 elseif probformat="bbox":
   bboxformat(p,cl,cb,cf)
 elseif mpt_isprobover and (probformat=""):
   circleprobformat(p,cf)
 else:
   image (draw p withcolor cf mpt_po_)
 fi
enddef;

vardef formatnodeev(expr p)=
 save cl,cb,cf;
 color cl,cb,cf;
 cl:=nodelinecolor;
 cb:=nodebgcolor;
 cf:=nodefgcolor;
 if nodeformat="superellipse":
   supellformat(p,cl,cb,cf)
 elseif nodeformat="circle":
   circleformat(p,cl,cb,cf)
 elseif (mpt_isgraph) and (nodeformat=""):
   circleformat(p,cl,cb,cf)
 elseif nodeformat="square":
   squareformat(p,cl,cb,cf)
 elseif nodeformat="bbox":
   bboxformat(p,cl,cb,cf)
 else:
   image (draw p withcolor cf mpt_po_)
 fi
enddef;

vardef formatnodeleave(expr p)=
 save cl,cb,cf;
 color cl,cb,cf;
 cl:=leavelinecolor;
 cb:=leavebgcolor;
 cf:=leavefgcolor;
 if leaveformat="superellipse":
   supellformat(p,cl,cb,cf)
 elseif leaveformat="circle":
   circleformat(p,cl,cb,cf)
 elseif leaveformat="bbox":
   bboxformat(p,cl,cb,cf)
 elseif leaveformat="none":
   image (draw p withcolor cf)
 else:
   formatnodeev(p)
 fi
enddef;


vardef formatnode(expr p)=
  if known exist.arbre[colonne_cour+1][counttmp]:
    formatnodeev(p)
  else:
    formatnodeleave(p)
  fi
enddef;


%%% Prints events

vardef theevlabel(expr s,z,inc)=
  save d; numeric d;
%  save p;picture p;
%  p:=formatnode(s);
  d=0.5*longdir(s,inc);
  s shifted (z - center s + (d+labeloffset)*dir(inc))
enddef;

def evlabel = draw theevlabel enddef;



vardef thelabelbranchehaut(expr Ori,Fin,Fig)=
   interim labeloffset:=proboffset;
   save M,pp,tt;
   pair M; path pp;numeric tt;
   pp=dessinbranche(Ori,Fin);
   tt = arctime posprob*(arclength  pp) of pp;
   M= point tt of pp;
   M:=M + 0.5(llcorner Fig - lrcorner Fig);
   if (angle(direction tt of pp) <= 90) and (angle(direction tt of pp) > -90):
      M:=M+proboffset*unitvector((direction tt of pp) rotated 90)
   else:
      M:=M-proboffset*unitvector((direction tt of pp) rotated 90)
   fi;
   Fig shifted M
enddef;


vardef thelabelbranchehautrot(expr Ori,Fin,Fig)=
   interim labeloffset:=proboffset;
   save M,pp,tt;
   pair M; path pp;numeric tt;
   pp=dessinbranche(Ori,Fin);
   tt = arctime posprob*(arclength  pp) of pp;
   M= point tt of pp;
   if  xpart Ori <= xpart Fin:
      Fig shifted ( - 0.5*lrcorner Fig- 0.5* llcorner Fig+(0,proboffset)) rotated angle(direction tt of pp) shifted M
   else:
      Fig shifted ( - 0.5*lrcorner Fig- 0.5* llcorner Fig+(0,proboffset)) rotated (-180+angle(direction tt of pp)) shifted M
   fi
enddef;



vardef thelabelbranchesuper(expr Ori,Fin,Fig)=
   save figtmp,M,pp;
   pair M; path pp;
   picture figtmp;
   pp=dessinbranche(Ori,Fin);
   M= point (arctime posprob*(arclength  pp) of pp) of pp;
   figtmp:=Fig shifted (M-0.5*(lrcorner Fig + ulcorner Fig));
   image(fill bbox figtmp withcolor background;
         draw figtmp)
enddef;


vardef thelabelbranchesuperrot(expr Ori,Fin,Fig)=
   save figtmp;
   picture figtmp;
   save M,pp,tt;
   pair M; path pp;numeric tt;
   pp=dessinbranche(Ori,Fin);
   tt = arctime posprob*(arclength  pp) of pp;
   M= point tt of pp;
   figtmp:=Fig shifted (-0.5*(lrcorner Fig + ulcorner Fig)) rotated angle(direction tt of pp) shifted M;
   image(fill bbox figtmp withcolor background;
         draw figtmp)
enddef;



vardef thelabelbranche(expr Ori,Fin,Fig)=
  if typeprob=1:
     thelabelbranchehaut(Ori,Fin,Fig)
  elseif typeprob=2:
     thelabelbranchesuper(Ori,Fin,Fig)
  elseif typeprob=3:
     thelabelbranchehautrot(Ori,Fin,Fig)
  elseif typeprob=4:
     thelabelbranchesuperrot(Ori,Fin,Fig)
  fi
enddef;

vardef brokenline(expr Ori,Fin)=
   save B,M;
   pair B,M;
   B=(xpart (Fin rotated -dirlabel), ypart (Ori rotated -dirlabel));
   M = (brokenlineratio*abs(B-(Ori rotated -dirlabel)),0) rotated dirlabel;
   Ori -- (Ori + M) -- (Fin - M) -- Fin
enddef;

numeric tenscurve;
tenscurve:=0;

vardef curveline(expr Ori,Fin)=
  save tmp_angle;
  numeric tmp_angle;
    tmp_angle:=dirlabel+tenscurve*angle((Fin-Ori) rotated -dirlabel);
  Ori{dir tmp_angle}..{dir tmp_angle}Fin
enddef;



%%%%%%%% Dessin de la branche
vardef dessinbranche(expr Ori,Fin)=
  if mpt_isgraph:
    mpt_tmp_branche
  elseif branchtype="segment":
    Ori--Fin
  elseif branchtype="curve":
    curveline(Ori,Fin)
  elseif branchtype="broken":
    brokenline(Ori,Fin)
  else:
    Ori--Fin
  fi
enddef;


%%%%%%%% Dessin complet de la branche (avec proba, évènement)

vardef branche_abs(expr Ori,Fin,Eve,Pro)=
   save figtmp,thelab,figlab,tmpshft;
   picture figtmp,thelab,figlab;
   numeric tmpshft;
   thelab:=labelarbres(Pro) scaled scaleprob;
   if ypart (Ori rotated -dirtree) > ypart (Fin rotated -dirtree):  %le décalage se fait en fonction de la direction de la branche
      tmpshft:=endedgeshift
   else:
      tmpshft:=-endedgeshift
   fi;
   figlab:=thelabelbranche(Ori,(Fin+ (0,tmpshft)),thelab);
   figtmp=image(%
      if branchtype<>"none":
         if edgearrow:
            drawarrow
         else:
            draw
         fi
         dessinbranche(Ori,(Fin + (0,tmpshft))) withpen pencircle scaled linewidth withcolor linecolor;
      fi
      draw figlab;
      evlabel(Eve,Fin,dirlabel)
      );
   figtmp
enddef;



vardef branche_rel(expr Ori,dx,dy,Eve,Pro)=
  branche_abs(Ori,Ori+(dx,dy),Eve,Pro)
enddef;

pair mptFinBranche[][]; % Branche qui mène au noeud [][]
mptFinBranche[1][1]:=origin;

pair mptFinBrancheRel[][][]; % k-ieme branche qui part de  i,j
mptFinBrancheRel[0][1][1]:=origin;

numeric mptLargBranche[][], mptHautBranche[][]; % Branche qui mène au noeud [][]
numeric mptNbBranches[][]; % Nombre de branches de l'arbre [][]
for i=0 upto 10: for j=0 upto 100: mptNbBranches[i][j]:=1; endfor endfor
mptNbBranches[0][1]:=1;


numeric mptAbs[]; % Abscisse noeud i
mptAbs[1]:=0;

pair mptchild[][][];
mptchild[0][1][1]:=(1,1);

color mptparent[][];
mptparent[1][1]:=(0,1,1);


vardef _arbre_(expr Ori)(text t)(text r)= % (pt)(pt1,pt2...)(ev,proba,ev,proba...)
  save figtmp,n,Fintmp,labeltmp,counttmp,countlab,tmpcountligne;
  picture figtmp,labeltmp[];
  numeric counttmp,countlab,tmpcountligne;
  pair Fintmp[];
  mpt_isgraph:=false;
  if dirtree<>0: dirlabel:=dirtree fi;
  if colonne_cour=1:
       countligne[1]:=1;countligne[0]:=1;
       for i=2 upto 128: countligne[i]:=0; endfor
  fi
  tmpcountligne:=countligne[colonne_cour+1];
  counttmp:=tmpcountligne;
  for i=t:
      counttmp:=counttmp+1;
      countligne[colonne_cour+1]:=countligne[colonne_cour+1]+1;
      mptFinBrancheRel[colonne_cour][ligne_cour][counttmp-tmpcountligne]:=i rotatedaround(Ori,dirtree);
      mptFinBranche[colonne_cour+1][counttmp]:=i rotatedaround(Ori,dirtree);
      mptLargBranche[colonne_cour+1][counttmp]:= xpart i - xpart Ori;
      mptHautBranche[colonne_cour+1][counttmp]:= ypart i - ypart Ori;
      mptchild[colonne_cour][ligne_cour][counttmp-tmpcountligne]:=(colonne_cour+1,counttmp);
      mptparent[colonne_cour+1][counttmp]:=(colonne_cour,ligne_cour,counttmp-tmpcountligne);
  endfor;
  mptAbs[colonne_cour+1]:= xpart mptFinBranche[colonne_cour+1][counttmp];
  mptNbBranches[colonne_cour][ligne_cour]:=counttmp-tmpcountligne;
  figtmp=image(%
    counttmp:=tmpcountligne;countlab:=0;
    for i=r:
      countlab:=countlab+1;
      labeltmp[countlab]:=labelarbres(i);
      if countlab=2:
          counttmp:=counttmp+1;
          labeltmp[1]:=formatnode(labeltmp[1]);
          labeltmp[2]:=formatprob(labeltmp[2]);
          draw branche_abs(Ori,mptFinBranche[colonne_cour+1][counttmp],labeltmp[1] scaled scaleev,labeltmp[2]);
          Orig_arbre[colonne_cour+1][counttmp]:=mptFinBranche[colonne_cour+1][counttmp] shifted
              if shiftev<0: ((longdir(labeltmp[1] scaled scaleev,dirlabel) + 2labeloffset)*(1,0))
              else: (shiftev,0)
              fi
                  rotatedaround(mptFinBranche[colonne_cour+1][counttmp],dirlabel);
         if mpt_passe=1:
           mpt_dec_ori[colonne_cour+1][counttmp]:= (Orig_arbre[colonne_cour+1][counttmp] rotatedaround(Ori,-dirtree))- mptFinBranche[colonne_cour+1][counttmp]
         fi;
         countlab:=0
      fi;
    endfor
      );
   figtmp
enddef;





vardef arbre_decv(expr Ori,horiz,Ymax)(text t)(text r)=
          % (pt,dec horiz)(dy1,dy2...)(ev,proba,ev,proba...)
  save Fin,compt,Ytmp;
  numeric compt,Ytmp;
  pair Fin[];
  if abscoord:
    Fin[0]:=(horiz,Ymax)
  else:
    Fin[0]:=Ori+(horiz,Ymax)
  fi;
  compt:=0;Ytmp:=Ymax;
  for i=t:
    compt:=compt+1;Ytmp:=i;
    if abscoord:
      Fin[compt]:=(horiz,Ytmp)
    else:
      Fin[compt]:=Ori+(horiz,Ytmp)
    fi;
  endfor
  def ptarr=
    Fin[0]
    for i=1 upto compt:
     ,Fin[i]
    endfor
  enddef;
  _arbre_(Ori)(ptarr)(r)
enddef;


vardef arbre_fixe(expr Ori,horiz,Ymax,decy)(text r)=
  save Fin,compt,compt_par;
  numeric compt,compt_par;
  pair Fin[];
  if abscoord:
    Fin[0]:=(horiz,Ymax + ypart Ori)
  else:
    Fin[0]:=Ori+(horiz,Ymax)
  fi;
  compt:=0;compt_par:=0;
  for i=r:
    compt:=compt+1;compt_par:=compt_par+1;
    if compt_par=2:
       compt_par:=0;
       if abscoord:
         Fin[compt/2]:=(0, ypart Ori) + (horiz,Ymax-decy*compt/2)
       else:
         Fin[compt/2]:=Ori+(horiz,Ymax-decy*compt/2)
       fi;
    fi
  endfor
  def ptarr=
    Fin[0]
    for i=1 upto compt/2-1:
     ,Fin[i]
    endfor
  enddef;
  _arbre_(Ori)(ptarr)(r)
enddef;


vardef colonne.reg.arbre[]=
  @
enddef;

vardef colonne.comp.arbre[]=
  @
enddef;

vardef colonne.dec.arbre[]=
  @
enddef;

vardef colonne.calc.arbre[]=
  @
enddef;

vardef colonne.arbre[]=
  @
enddef;

vardef reg.arbre[][](text t)(text r)=
 save compt,decal;
 numeric compt,decal[];
 colonne_cour:=colonne.#@;
 ligne_cour:=@;
 compt:=0;
   for i=t:
    if numeric i:
       compt:=compt+1;
       decal[compt]:=i
    fi;
   endfor
 compt:=0;
   for i=r:
    compt:=compt+1;
   endfor
 arbre_fixe(Orig_arbre[colonne_cour][ligne_cour],decal[1],(compt/2-1)*decal[2]/2,decal[2])(r)
enddef;



vardef comp.arbre[][](text t)(text r)=
  colonne_cour:=colonne.#@;
  ligne_cour:=@;
  save tmp_compt;
  numeric tmp_compt;
  tmp_compt:=0;
  if abscoord:
    def tmp_text=t enddef
  else:
  def tmp_text= % Si abscoord tmp_text=t
    for i=t:
      hide(tmp_compt:=tmp_compt+1;)
      if (tmp_compt>1) and (not string i):
         ,Orig_arbre[colonne_cour][ligne_cour]+i
      elseif not string i:
         Orig_arbre[colonne_cour][ligne_cour]+i
      fi
    endfor
  enddef
  fi;
  _arbre_(Orig_arbre[colonne_cour][ligne_cour])(tmp_text)(r)
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%% Modif 10/16 - Décalage par rapport à l'origine
vardef dec.arbre[][](text t)(text r)=
  save decal;
  numeric decal[];
  colonne_cour:=colonne.#@;
  ligne_cour:=@;
  save tmp_compt;
  numeric tmp_compt;
  tmp_compt:=0;
  for i=t:
    if numeric i:
       tmp_compt:=tmp_compt+1;
       decal[tmp_compt]:=i
    fi;
  endfor
  def tmp_text=
    decal[3]
    for i=4 upto tmp_compt:
      ,decal[i]
    endfor
  enddef;
  arbre_decv(Orig_arbre[colonne_cour][ligne_cour],decal[1],decal[2])(tmp_text)(r)
enddef;


vardef calc.arbre[][](text r)=
  colonne_cour:=colonne.#@;
  ligne_cour:=@;
  save mpt_O,mpt_dec_calc;
  pair mpt_O;
  numeric mpt_dec_calc[];
   for i=1 upto mptNbBranches[colonne_cour][ligne_cour]:
     mpt_dec_calc[i]:=mpt_decbranche[colonne_cour][ligne_cour][i];
   endfor
   mpt_dec_calc[0]:=widthbranch*(scalebranch**colonne_cour);
  def tmp_text_calc=
    mpt_dec_calc[0]
   for i=1 upto mptNbBranches[colonne_cour][ligne_cour]:
     ,mpt_dec_calc[i]
   endfor
  enddef;
   mpt_O:= mpt_tmp_ori[colonne_cour][ligne_cour];
   dec.arbre[colonne_cour][ligne_cour](tmp_text_calc)(r)
%   _arbre_(mpt_O)(tmp_text_calc)(r)
enddef;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macro  principale - Modification Mars 2022 (variables locales)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Conversion des ' en " pour les variables locales.

vardef singletodoublequote(expr chdb)=
  save chsi;
  string chsi;
  chsi:="";
  for i=1 upto length(chdb):
    if ASCII(substring(i-1,i) of chdb) = 39:
      chsi := chsi & char(34)
    else:
      chsi := chsi & substring(i-1,i) of chdb
    fi;
  endfor
  chsi
enddef;

% Initialisation variables locales

def init_loc_var=
  numeric tmp_posprob, tmp_shiftev, tmp_typeprob, tmp_scaleev, tmp_scaleprob, tmp_proboffset, tmp_endedgeshift;
  boolean tmp_edgearrow;
  string tmp_branchtype;
  tmp_posprob := posprob;
  tmp_typeprob := typeprob;
  tmp_shiftev:= shiftev;
  tmp_scaleev := scaleev;
  tmp_scaleprob := scaleprob;
  tmp_proboffset := proboffset;
  tmp_endedgeshift := endedgeshift;
  tmp_edgearrow := edgearrow;
  tmp_branchtype := branchtype;
  save posprob, shiftev, typeprob, scaleev, scaleprob, proboffset, endedgeshift, edgearrow, branchtype;
  numeric posprob, shiftev, typeprob, scaleev, scaleprob, proboffset, endedgeshift;
  boolean edgearrow;
  string branchtype;
  posprob:=tmp_posprob;
  typeprob:=tmp_typeprob;
  shiftev:=tmp_shiftev;
  scaleev := tmp_scaleev;
  scaleprob := tmp_scaleprob;
  proboffset := tmp_proboffset;
  endedgeshift := tmp_endedgeshift;
  edgearrow := tmp_edgearrow;
  branchtype := tmp_branchtype;
enddef;


pair current_tree;

boolean arbrebool; % est-on à l'intérieur d'un arbre ?
arbrebool:=false;

vardef arbre[][](text t)(text r)=
  arbrebool:=true;
  naturalwidth:=false;
  init_loc_var;
  if posprob=-1: posprob:=0.6 fi;
  save tmp_type,tmp_compt,tmp_chdb;
  numeric tmp_type,tmp_compt,tmp_comptii;
  string tmp_chdb;
  tmp_compt:=0;tmp_comptii:=0;
  for i=t:
    tmp_comptii:=tmp_comptii+1;
    if numeric i:
       tmp_compt:=tmp_compt+1
    elseif string i:
       tmp_chdb:=singletodoublequote(i);
       scantokens tmp_chdb;
    fi;
  endfor
  exist.#@.@:=true;
  current_tree:=(colonne.#@,@);
  if tmp_comptii=0:
   if mpt_passe=1:
    reg.#@.@(widthbranch*(scalebranch**colonne_cour),gapnode*(2**(3-colonne_cour)))(r)
   else:
    calc.#@.@(r)
   fi
  else:
     Orig_arbre[1][1]:=origin;
     if tmp_compt=0: % Arbre défini par des points (dec_horiz,dec_vert)
        comp.#@.@(t)(r)
     elseif tmp_compt=2: % Arbre régulier, on donne largeur et esp. vert.
        reg.#@.@(t)(r)
     else:
        dec.#@.@(t)(r) % Arbre semi-régulier, largeur fixe, decalages vert.
     fi
  fi
enddef;


def tree = arbre enddef;

%%%%% stree (simple tree) Arbre sans les probas

vardef colonne.stree[]=
  @
enddef;

vardef stree[][](text t)(text r)=
 save cc,ll;
 numeric cc,ll;
 cc:=colonne.#@;
 ll:=@;
 def tmptxt=
    for i=r:
      i,"",
    endfor
 enddef;
 tree[cc][ll](t)(tmptxt)
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% "Calculated" trees
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef ymean(expr i,j)=
  (0
   for k=1 upto mptNbBranches[i][j]:
    + ypart (mptFinBrancheRel[i][j][k] rotated -dirtree)
  endfor)/mptNbBranches[i][j]
enddef;


vardef barycentre(expr A,a,B,b)=
  (a*A+b*B)/(a+b)
enddef;

numeric mpt_fact_gap;
mpt_fact_gap:=1.5;

vardef exlignes(expr col,lig)=
% renvoie (x,y) tel que x<lig<y, x et y ont des child, 0 sinon
  save tmpcl,tmpx,tmpy,ytrue;
  pair tmpcl;
  numeric tmpx,tmpy;
  boolean ytrue;
  tmpx:=0;tmpy:=0;ytrue:=false;
  for i=1 upto countligne[col+1]:
    if known exist.arbre[col+1][i]:
       if i<lig:
           tmpx:=i
       else:
           tmpy:=i;ytrue:=true;
       fi
    fi;
  exitif ytrue;
  endfor
  (tmpx,tmpy)
enddef;


vardef mptCalcIsol(expr a,b,c,l)=
  save tmpmin,tmpmax;
  numeric tmpmin,tmpmax;
  tmpmin:=xpart exlignes(a,l);
  tmpmax:=ypart exlignes(a,l);
  if tmpmin=0:
    mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmax]][bluepart mptparent[a+1][tmpmax]] + (tmpmax-l)*gapnode*mpt_fact_gap*((0,1) rotated dirtree)
  elseif tmpmax=0:
    mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmin]][bluepart mptparent[a+1][tmpmin]] - (l-tmpmin)*gapnode*mpt_fact_gap*((0,1) rotated dirtree)
  else:
    barycentre(mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmin]][bluepart mptparent[a+1][tmpmin]],tmpmax-l,mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmax]][bluepart mptparent[a+1][tmpmax]],l-tmpmin)
  fi
enddef;


numeric tmplig,tmpligii;

numeric mpt_decbranche[][][];



def AdjustNodes=
  tmplig:=0;
  for i=1 upto countligne[colonne_cour]:
    for j= 1 upto mptNbBranches[colonne_cour][i]:
      mptFinBrancheRel[colonne_cour][i][j]:=mptFinBranche[colonne_cour+1][1] -tmplig*gapnode*((0,1) rotated dirtree);
      tmplig:=tmplig+1;
    endfor
  endfor
 for cmpttmp:=1 upto colonne_cour-1:  % plusieurs  tours pour tout régler...
  for col=colonne_cour-1 downto 0:
    tmplig:=0;
    for i=1 upto countligne[col]:
     if known mptNbBranches[col][i]:
      for j= 1 upto mptNbBranches[col][i]:
         if known exist.arbre[col][i]:
             tmplig:=tmplig+1;
           if known exist.arbre[col+1][tmplig]:
              mptFinBrancheRel[col][i][j]:=
                       (xpart (mptFinBrancheRel[col][i][j] rotated -dirtree),ymean(col+1,tmplig)) rotated dirtree;
           else:
              mptFinBrancheRel[col][i][j]:=mptCalcIsol(col,i,j,tmplig);
           fi;
           Orig_arbre[col+1][tmplig]:=(xpart (Orig_arbre[col+1][tmplig] rotated -dirtree),ypart (mptFinBrancheRel[col][i][j] rotated -dirtree)) rotated dirtree;
         fi;
      endfor
     fi
    endfor
  endfor
%  adjustii;
 endfor
  for col=1 upto colonne_cour:
    for i=1 upto countligne[col]:
      if known exist.arbre[col][i]:
        for j=1 upto mptNbBranches[col][i]:
           mpt_decbranche[col][i][j]:=ypart (mptFinBrancheRel[col][i][j] rotated -dirtree) - ypart (Orig_arbre[col][i] rotated -dirtree);
        endfor
      fi
    endfor
  endfor
  Orig_arbre[1][1]:=origin;
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% regular trees
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

numeric scalebranch;
scalebranch := 0.8;

numeric widthbranch,gapnode;
widthbranch:=3.5cm;
gapnode:=0.7cm;

vardef regulartree(expr n)(text t)(text r)=
  save tmp,ll,shiftev,tmpstr,count,cc,tmp_compt,h;
  numeric ll,count,cc,tmp_compt,h;
  string tmpstr[][];
  count:=0;cc:=1;
  for i = r:
    count := count + cc;
    cc := 1 - cc;
    tmpstr[count][cc] := i;
  endfor
  shiftev:=-1;
  for j=1 upto count:
    if shiftev < longdir(labelarbres(tmpstr[j][0]),dirtree):
      shiftev := longdir(labelarbres(tmpstr[j][0]),dirtree)
    fi;
  endfor
  shiftev := shiftev + 2*labeloffset;
  tmp_compt:=1;
  for i=t:
    if (not string i) and (tmp_compt=1):
       ll := i; tmp_compt:=2
    elseif (not string i) and (tmp_compt=2):
       h := i
    else:
      scantokens i
    fi;
  endfor
  picture tmp;
  tmp := image(%
  for i := 1 upto n:
    for j := 1 upto (count**(i-1)):
      draw tree[i][j](ll,h*(count**(n-i)))(r);
    endfor
    ll := ll * scalebranch;
  endfor
  );
  tmp
enddef;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Binomial
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

string bernoullisuccessevent, bernoullifailureevent, bernoullisuccessprob, bernoullifailureprob;
bernoullisuccessevent := "$S$";
bernoullifailureevent := "$\overline{S}$";
bernoullisuccessprob := "$p$";
bernoullifailureprob := "$q$";
numeric bernoulliscalebranch;
bernoulliscalebranch := 0.8;

vardef bernoulliprocess(expr n)(text t)(text r)=
  save tmp_ct,tmp_ch;
  numeric tmp_ct;
  string tmp_ch[];
  tmp_ch[1]:=bernoullisuccessevent;
  tmp_ch[2]:=bernoullisuccessprob;
  tmp_ch[3]:=bernoullifailureevent;
  tmp_ch[4]:=bernoullifailureprob;
  tmp_ct:=0;
  for i=r:
    tmp_ct:=tmp_ct+1;
    if string i:
      tmp_ch[tmp_ct]:=i
    fi;
  endfor
  regulartree(n)(t)(tmp_ch[1],tmp_ch[2],tmp_ch[3],tmp_ch[4])
enddef;



vardef bernoulliprocessL(expr n)(text t)(text r)=
  save L,H,l,hh,lpic,hpic;
  numeric L,H,l,hh,lpic,hpic;
  save tmp_ct,tmp_chi,tmp_b;
  numeric tmp_ct;
  string tmp_chi[];
  boolean tmp_b;
  tmp_chi[1]:=bernoullisuccessevent;
  tmp_chi[2]:=bernoullisuccessprob;
  tmp_chi[3]:=bernoullifailureevent;
  tmp_chi[4]:=bernoullifailureprob;
  tmp_b:=true;
  for i=t:
    if numeric i:
      if tmp_b: L:=i; tmp_b := false else: H:=i fi
    fi;
  endfor
  tmp_ct:=0;
  for i=r:
    tmp_ct:=tmp_ct+1;
    if string i:
      tmp_chi[tmp_ct]:=i
    fi;
  endfor
  hpic:=0.5*longdir(labelarbres(tmp_chi[1]),90+dirtree) + 0.5*longdir(labelarbres(tmp_chi[3]),90+dirtree);
  hh := (H-hpic)/((2**n)-1);
  lpic:=longdir(mptreestartlabel,dirtree) + n*max(longdir(labelarbres(tmp_chi[1]),dirtree) , longdir(labelarbres(tmp_chi[3]),dirtree)) + (2*n-1)*labeloffset;
  if bernoulliscalebranch<>1:
    l:= (L-lpic)*(1-bernoulliscalebranch)/(1-bernoulliscalebranch**n)
  else:
    l:= (L-lpic)/n
  fi;
  def tmp_text=
    l,hh
      for i=t:
        if string i: ,i fi
      endfor
  enddef;
  regulartree(n)(tmp_text)(tmp_chi[1],tmp_chi[2],tmp_chi[3],tmp_chi[4])
enddef;


vardef binomialtree(expr n)(expr l,h)=
  save tmp,ll,posprob,shiftev,bernoulliscalebranch;
  numeric ll;
  posprob:=0.5;bernoulliscalebranch:=1;
  shiftev:=longdir(labelarbres("3"),dirtree)+2*labeloffset;
  ll := l;
  picture tmp;
  tmp := image(%
  draw tree[1][1](ll,h)("$1$", bernoullisuccessprob,  "$0$", bernoullifailureprob);
  if n>1:
    for i := 2 upto n:
      for j := 1 upto (i-1):
        draw tree[i][2*j-1](ll,h)(decimal(i-j+1), bernoullisuccessprob,  " ", bernoullifailureprob);
      endfor
      draw tree[i][2*i-2](ll,h)("$1$", bernoullisuccessprob,  "$0$", bernoullifailureprob);
    endfor
    ll := ll * bernoulliscalebranch;
  fi
  );
  tmp
enddef;

vardef binomialtreeL(expr n)(expr L,H)=
  save l,h,lpic,hpic;
  numeric l,h,lpic,hpic;
  hpic:=longdir(labelarbres("0"),90+dirtree);
  h := (H-hpic)/n;
  lpic:=longdir(mptreestartlabel,dirtree) + n*max(longdir(labelarbres("0"),dirtree) , longdir(labelarbres("0"),dirtree)) + (2*n-1)*labeloffset;
  l:= (L-lpic)/n;
  binomialtree(n)(l,h)
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                         GRAPHES
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% Outils
vardef isdigit primary d =
    ("0"<=d)and(d<="9")
enddef ;

% Convertit un suffixe en chaine avec les chiffres en indice
vardef _chaine(suffix s)=
  save c,d;
  string c,d;
  d:= str s;
  if length(d)=1:
     c:=d
     elseif isdigit (substring (1,2) of d):
            c:= (substring (0,1) of d) & "_{" & substring (1,infinity) of d & "}"
            else: c:=str s
  fi;
  c
enddef;

vardef _chainec(expr d)=
  save c;
  string c;
  if length(d)=1:
     c:=d
  elseif isdigit (substring (1,2) of d):
     c:= (substring (0,1) of d) & "_{" & substring (1,infinity) of d & "}"
  elseif isdigit (substring (2,3) of d):
     c:= (substring (0,2) of d) & "_{" & substring (2,infinity) of d & "}"
  else:
     c:=d
  fi;
  c
enddef;

% longueur d'un paramètre text
vardef long_texte(text t)=
  save i;
  numeric i;
  i:=0;
  for n=t: i:=i+1; endfor
  i
enddef;


% Placement fin des étiquettes
vardef thelabelang@#(expr s,z)=
     save tmpang,tmpfig,tmppt,tmppath,tmpstr;
     string tmpstr;
     numeric tmpang;
     pair tmppt;
     path tmppath;
     save p; picture p;
     tmpstr := str @#;
     if picture s:  p=s
     else:    p = textext(s)
     fi;
     tmppath := llcorner p -- lrcorner p -- urcorner p -- ulcorner p -- cycle;
     if ASCII tmpstr = 91:
        tmpang := scantokens substring(1,length(tmpstr)-1) of tmpstr
     else:
        tmpang := scantokens tmpstr
     fi;
     tmppt := tmppath intersectionpoint ((0.5urcorner p+0.5llcorner p)-- (0.5urcorner p +0.5llcorner p+ 100 * (dir (180+tmpang))));
      p shifted (-tmppt + z + labeloffset*(cosd(tmpang),sind(tmpang)) )
enddef;

def labelang = draw thelabelang enddef;


%%%%%%
%% Paramètres
%%%%%


numeric nodewidth,edgelinewidth,edgearrowpos;
nodewidth:=0.6cm;
edgelinewidth:=1;
nodelinewidth:=1;
edgearrowpos:=1;

boolean naturalwidth;
naturalwidth:=false;

numeric nodelabeloffset, nodeedgeoffset; % pour la position des étiquettes
nodelabeloffset:=2;
%nodeedgeoffset:=nodelinewidth/2+edgelinewidth/2;
nodeedgeoffset:=-101;



numeric edgeangle,loopangle,loopsize,loopstartangle;
edgeangle:=0;
loopangle:=90;
loopsize:=-1;
loopstartangle:=30;


%%% Format du nom des sommets
string defnodename;
defnodename:="A";
string nodename;
nodename:="array";
numeric startnodename;
startnodename=1;


boolean printnodename;
printnodename:=true;



numeric mpt_numtotal; % nombre total de sommets
mpt_numtotal:=0;

pair mpt_nodecoord[]; % coordonnées du noeud i
string mpt_nodename[]; % nom du noeud i
path mpt_nodepath[]; % chemin autour du noeud i
string mpt_nodelpart[],mpt_nodenumpart[]; % parties alphanumérique et numérique du nom du noeud

color gridcolor;
gridcolor:=0.7white;

numeric gridlinewidth;
gridlinewidth:=0.5;



%%%% Variables privées
boolean mpt_dir;  %arête orientée ou non
mpt_dir:=false;

boolean mpt_isgraph;
mpt_isgraph:=false;

picture mpt_tmp_fig_cbg;
path mpt_tmp_branche;
string mpt_tmp_prob;

path mpt_tmp_path;

boolean mpt_isprob,mpt_isprobover;
mpt_isprob:=false;
mpt_isprobover:=false;


boolean mpt_bgc;
mpt_bgc:=false;





%%%%%%%%%%%%%%%%%%%%%%%%%%%% Noeuds

vardef mptlpart(expr tt)=
  save tmp;
  string tmp;
  tmp:="";
  for i =1 upto length tt:
    if not isdigit substring(i-1,i) of tt:
      tmp:=tmp & substring(i-1,i) of tt
    fi;
  endfor
  tmp
enddef;

vardef mptnumpart(expr tt)=
  save tmp;
  string tmp;
  tmp:="";
  for i =1 upto length tt:
    if isdigit substring(i-1,i) of tt:
      tmp:=tmp & substring(i-1,i) of tt
    fi;
  endfor
  tmp
enddef;

% définit le noeud et certains attributs
def defnode(suffix ptA)(expr A)=
  save tmpchcomp,tmpchlet,tmpchnum;
  string tmpchcomp,tmpchlet,tmpchnum;
   mpt_isgraph:=true;
  tmpchcomp:= str ptA;
  tmpchlet:=mptlpart(str ptA);
  tmpchnum:=mptnumpart(str ptA);
  if tmpchnum="":
     pair ptA;
     ptA:=A;
  else:
     scantokens("if not known " & tmpchlet & ": pair " & tmpchlet & "[] fi;" & tmpchlet & tmpchnum & ":=("& decimal(xpart A)&","& decimal(ypart A) & ");")
  fi;
  mpt_numtotal:=mpt_numtotal+1;
  mpt_nodecoord[mpt_numtotal]:=A;
  mpt_nodename[mpt_numtotal]:= tmpchcomp;
  mpt_nodelpart[mpt_numtotal]:= tmpchlet;
  mpt_nodenumpart[mpt_numtotal]:= tmpchnum;
  mpt_nodepath[mpt_numtotal]:= formatnodepath(labelarbres(str ptA) scaled scaleev) shifted A;
enddef;


def defnodestr(expr ptA)(expr A)=   % Quand le premier paramètre est une chaine simple
  save tmpchcomp,tmpchlet,tmpchnum;
  string tmpchcomp,tmpchlet,tmpchnum;
  mpt_isgraph:=true;
  tmpchcomp:= ptA;
  tmpchlet:=ptA;
  tmpchnum:="";
  scantokens("pair " & tmpchcomp & ";" & tmpchcomp & ":=("& decimal(xpart A)&","& decimal(ypart A) & ");")
  mpt_numtotal:=mpt_numtotal+1;
  mpt_nodecoord[mpt_numtotal]:=A;
  mpt_nodename[mpt_numtotal]:= tmpchcomp;
  mpt_nodelpart[mpt_numtotal]:= tmpchlet;
  mpt_nodenumpart[mpt_numtotal]:= tmpchnum;
  mpt_nodepath[mpt_numtotal]:= formatnodepath(labelarbres(tmpchcomp) scaled scaleev) shifted A;
enddef;


def defnodes(text t)=   % Pour définir plusieurs noeuds à la fois
 if (nodename="array") or (nodename="arabic"):
  for i=t:
    mpt_numtotal:=mpt_numtotal+1;
    if mpt_numtotal=1:
       scantokens("pair " & defnodename & "[];")
    fi;
    scantokens(defnodename & decimal(mpt_numtotal) & ":=("& decimal(xpart i)&","& decimal(ypart i) & ");");
    mpt_nodecoord[mpt_numtotal]:=i;
    mpt_nodename[mpt_numtotal]:= defnodename & decimal(mpt_numtotal);
    mpt_nodelpart[mpt_numtotal]:= defnodename;
    mpt_nodenumpart[mpt_numtotal]:= decimal(mpt_numtotal);
    mpt_nodepath[mpt_numtotal]:= formatnodepath(labelarbres(mpt_nodename[mpt_numtotal]) scaled scaleev) shifted mpt_nodecoord[mpt_numtotal];
  endfor
 elseif (nodename="Alph") or (nodename="alph"):
  save tmp_str,tmp_nn;
  string tmp_str;
  numeric tmp_nn;
  tmp_nn:= if nodename="Alph": 64 else: 96 fi;
  for i=t:
    tmp_str:=char(mpt_numtotal+startnodename+tmp_nn);
    defnodestr(tmp_str)(i);
  endfor
 fi
enddef;


% Renvoie le rang d'un noeud par suffixe
vardef searchnode(suffix ptA)=
  save val,st;
  numeric val;
  string st;
  st= str ptA;
  for i=1 upto mpt_numtotal:
     if mpt_nodename[i]=st:
       val=i
     fi;
     exitif known val;
  endfor
  val
enddef;

% Renvoie le rang d'un noeud suivi d'une chaine
vardef searchnodech(suffix s)(expr c)=
  searchnode(s)
enddef;

% Renvoie le path d'un noeud
vardef nodepath(suffix ptA)=
  save nn;
  numeric nn;
  nn:=searchnode(ptA);
  mpt_nodepath[nn]
enddef;

% Forme du chemin autour du noeud
vardef formatnodepath(expr p)=
 if nodeformat="superellipse":
   superellipsebox(p)
 elseif (nodeformat="circle") or (nodeformat=""):
   circlebox(p)
 elseif nodeformat="square":
   squarebox(p)
 else:
   bbox p shifted -center bbox p
 fi
enddef;


% Dessin du noeud
vardef thenode@#(text t)=
  save tmp_fig,tmpstr;
  picture tmp_fig;
  string tmp_str;
  if long_texte(t)=1:
     tmp_fig:=thenodenum@#(searchnode(t))
  else:
     for i=t:
       if string i: tmp_fig:=thenodenum@#(searchnodech(t),i) fi;
     endfor
  fi;
  tmp_fig
enddef;

vardef thenodenum@#(text t)=
   save tmp_pt,ch,dess,latch,num;
   numeric num;
   pair tmp_pt;
   string ch,latch;
   picture dess;
   mpt_isgraph:=true;
   interim labeloffset:=nodelabeloffset+nodewidth/2;
   if ((ASCII str @# < 58) or (ASCII str @# = 91)) and (str @#<>""):
       def mylabel = labelang enddef
   else:
       def mylabel = label enddef
   fi;
   if long_texte(t)=1:
        num:=t;
        dess:=image(
          ch:="$" & _chainec(mpt_nodename[num]) & "$";
          if nodename="arabic": ch := "$" & decimal(num) & "$" fi;
          mpt_nodepath[num]:=formatnodepath(labelarbres(ch) scaled scaleev) shifted mpt_nodecoord[num];
          label(formatnodeev(labelarbres(ch) scaled scaleev),mpt_nodecoord[num]);
          if printnodename:
               mylabel@#(textext(ch) scaled scaleev,mpt_nodecoord[num]) withcolor nodefgcolor mpt_po_
          fi)
   else:
      dess:=image(%
      for PP=t:
        if numeric PP:
           num:=PP;
           tmp_pt:=mpt_nodecoord[num];
        else:
          mpt_nodepath[num]:=formatnodepath(labelarbres(PP) scaled scaleev) shifted mpt_nodecoord[num];
           label(formatnodeev(labelarbres(PP) scaled scaleev),tmp_pt);
          if printnodename:
           if string PP:
              mylabel@#(textext(PP),tmp_pt) withcolor nodefgcolor mpt_po_;
           else:
              mylabel@#(PP scaled defaultscale,tmp_pt) withcolor nodefgcolor mpt_po_;
           fi
          fi
        fi;
      endfor)
   fi;
   dess
enddef;

vardef node@#(text s)=
  save mpt_boolnum;
  boolean mpt_boolnum;
  mpt_boolnum:=false;
  for i=s:
    if numeric i:
       mpt_boolnum:=true
    fi;
  endfor
  if mpt_boolnum:
    thenodenum@#(s)
  else:
    thenode@#(s)
  fi
enddef;


vardef firstnode@#(suffix s)(text t) text r=
 drawnode@#(s) r;
 def tmp_text= t enddef;
enddef;


vardef nodes@#(text t) text s=  % Dessin des noeuds à partir de leur rang
  save tmp_pt;
  pair tmp_pt;
  save mpt_boolnum;
  boolean mpt_boolnum;
  mpt_boolnum:=false;
  for i=t:
    if numeric i:
       mpt_boolnum:=true
    fi;
  endfor
  image(
  if long_texte(t)=0:
     for i=1 upto mpt_numtotal:
       drawnode@#(i) s;
     endfor
  elseif mpt_boolnum:
    for i=t:
      drawnode@#(i) s;
    endfor
  else:
    save n;numeric n;
    n=long_texte(t);
    def tmp_text= t enddef;
    for i=1 upto n-1:
      firstnode@#(tmp_text) s;
    endfor
    drawnode@#(tmp_text) s;
  fi
  )
enddef;



vardef drawnode@#(text p) text t =
   mpt_drop(t);
   mpt_tmp_fig_cbg:=image(draw (0,0) mpt_po_);
   draw node@#(p);
   mpt_bgc:=false;
   mpt_drop();
enddef;

def drawnodes = draw nodes enddef;


%%%%%%%%%%%%%
%% Arêtes
%%%%%%%%%%%%%
% chemin entre les noeuds de rangs n et m, angles en param
vardef theedgeAB(expr m,n)=
  save an,ta,pb,pathab,A,B,C,tmpoffset;
  numeric an,tmpoffset;
  numeric ta,tb;
  pair A,B,C;
  if nodeedgeoffset=-101: tmpoffset:=0
  else:                   tmpoffset:=nodeedgeoffset
  fi;
  tmpoffset:=tmpoffset+nodelinewidth/2+edgelinewidth/2;
  A:=mpt_nodecoord[m];
  B:=mpt_nodecoord[n];
  path pathab;
  if m<>n:
     an:=angle(B-A);
     pathab:=A{dir (an+edgeangleaa[0])}..{dir (an-edgeangleaa[1])}B;
    ta=xpart(pathab intersectiontimes mpt_nodepath[m]);
    tb=xpart(pathab intersectiontimes mpt_nodepath[n]);
    pathab:= subpath(ta,tb) of pathab;
    ta:= arctime tmpoffset of pathab;
    tb:= arctime ((arclength pathab)-tmpoffset) of pathab;
    pathab:= subpath (ta, tb) of pathab;
  else:
    pathab:=theloopA(n)
  fi;
  pathab
enddef;



% boucle sur le noeud n
vardef theloopA(expr n)=
  save pathaa,aapath,A,B,C,tmpoffset,ta,tb;
  pair A,B,C;
  numeric tmpoffset,ta,tb;
  path pathaa,aapath;
  if nodeedgeoffset=-101: tmpoffset:=0
  else:                   tmpoffset:=nodeedgeoffset
  fi;
  tmpoffset:=tmpoffset+nodelinewidth/2+edgelinewidth/2;
  C:=mpt_nodecoord[n];
  pathaa:=C{dir (-tmp_loopangle[1]+tmp_loopangle[0])}..
          (C+((0.5loopsize,0.77loopsize) rotated (tmp_loopangle[0]-90))){dir (tmp_loopangle[0])}..
          (C+((loopsize,0) rotated (tmp_loopangle[0]))){dir (tmp_loopangle[0]+90)}..
          (C+((-0.5loopsize,0.7loopsize) rotated (tmp_loopangle[0]-90))){dir (tmp_loopangle[0]+180)}..
          {dir (tmp_loopangle[1]+tmp_loopangle[0]+180)}C;
  aapath:= reverse pathaa;
  ta=xpart(subpath(0,0.5*length pathaa) of pathaa intersectiontimes mpt_nodepath[n]);
  tb=xpart(subpath(0,0.5*length aapath) of aapath intersectiontimes mpt_nodepath[n]);
  pathaa:= subpath (ta,(length pathaa) - tb) of pathaa;
  ta:= arctime tmpoffset of pathaa;
  tb:= arctime ((arclength pathaa)-tmpoffset) of pathaa;
  pathaa:= subpath (ta, tb) of pathaa;
  pathaa
enddef;

% Chemin entre les noeuds A et B, angles dans p
vardef theedge(suffix A,B)(text p)=
  save nA,nB,tmp_fig;
  numeric nA,nB;
  picture tmp_fig;
  nA:=searchnode(A);
  nB:=searchnode(B);
  theedgenum(nA,nB)(p)
enddef;

% Chemin entre les noeuds nA et nB, angles dans p
vardef theedgenum(expr nA,nB)(text p)=
  save tmp_fig;
  picture tmp_fig;
  numeric edgeangleaa[],tmp_loopangle[];
  edgeangleaa[0]=edgeangleaa[1]=edgeangle;
  tmp_loopangle[0]:=loopangle;tmp_loopangle[1]:=loopstartangle;
  mpt_isgraph:=true;
  nn:=0;
  for i=p:
    if numeric i:
       if nn=0: edgeangleaa[0]:=i;edgeangleaa[1]:=i;tmp_loopangle[0]:=i
       else: edgeangleaa[1]:=-i;tmp_loopangle[1]:=i
       fi;
       nn:=nn+1;
    elseif string i:
       mpt_tmp_prob:=i;
    fi
  endfor
  theedgeAB(nA,nB)
enddef;

% Figure représentant le chemin avec (ou non) la flèche
vardef theedgenumfig(expr nA,nB)(text p)=
  save tmppath;
  path tmppath;
  tmppath:=theedgenum(nA,nB)(p);
  image(draw tmppath withpen pencircle scaled edgelinewidth mpt_po_;
       if mpt_dir:
         filldraw arrowhead subpath(0, edgearrowpos*length mpt_tmp_path) of mpt_tmp_path mpt_po_
       fi;
       )
enddef;

vardef edges@#(text s)(text p) text t=  % dessine plusieurs arêtes simultanément
  save a,b,isp;
  numeric a,b;
  boolean isp;
  isp:=false;
  image(
    for i=s:
    if pair i:
      if isp: drawedge@#(a,b)(p) t; fi;
      a:= xpart i;b:= ypart i;
      isp:=true;
    else:
       drawedge@#(a,b)(i,p) t;
       isp:=false
    fi;
  endfor
  if isp: drawedge(a,b)(p) t; fi
  )
enddef;






vardef theedgepond@#(suffix A,B)(text p)=
  save nA,nB;
  numeric nA,nB;
  nA:=searchnode(A);
  nB:=searchnode(B);
  theedgepondnum@#(nA,nB)(p)
enddef;

vardef theedgepondnum@#(expr nA,nB)(text p)=
  save tmpfig,tmppath,tmppt,mpt_isprob,mpt_isprobover;
  boolean mpt_isprob,mpt_isprobover;
  mpt_isprob:=true;
  path tmppath;
  picture tmpfig;
  pair tmppt;
  if str @#="": mpt_isprobover:=true else: mpt_isprobover:=false fi;
  if ((ASCII str @# < 58) or (ASCII str @# = 91)) and (str @#<>""):
      def mylabel = labelang enddef
  else:
      def mylabel = label enddef
  fi;
  tmppath:=theedgenum(nA,nB)(p);
  tmppt:=point (arctime posprob*(arclength  tmppath) of tmppath) of tmppath;
  image(draw theedgenum(nA,nB)(p) withpen pencircle scaled edgelinewidth mpt_po_;
        if mpt_dir:
           filldraw arrowhead subpath(0, edgearrowpos*length mpt_tmp_path) of mpt_tmp_path mpt_po_
        fi;
        mylabel@#(formatprob(labelarbres(mpt_tmp_prob) scaled scaleprob),tmppt))
enddef;

vardef edge@#(text s)(text p)=
  save mpt_boolprob,mpt_boolnum;
  boolean mpt_boolprob,mpt_boolnum;
  mpt_boolprob:=false;
  mpt_boolnum:=false;
  if posprob=-1: posprob:=0.5 fi;
  if loopsize=-1:
       loopsize:=1.5nodewidth;
       if loopsize<0.6cm: loopsize:=0.6cm fi;
  fi;
  for i=p:
    if string i:
      mpt_boolprob:=true
    fi;
  endfor
  for i=s:
    if numeric i:
       mpt_boolnum:=true
    fi;
  endfor
  if mpt_boolnum:
     mpt_tmp_path:=theedgenum(s)(p);
     if mpt_boolprob:
        theedgepondnum@#(s)(p)
     else:
        theedgenumfig(s)(p)
     fi
  else:
     mpt_tmp_path:=theedge(s)(p);
     if mpt_boolprob:
        theedgepond@#(s)(p)
     else:
        theedge(s)(p)
     fi
  fi
enddef;

%%%%%%%%%% Drawing edges

vardef drawedge@#(text s)(text p) text t=
   mpt_drop(t);
   draw edge@#(s)(p) withpen pencircle scaled edgelinewidth;
   mpt_drop();
enddef;

vardef drawedges@#(text s)(text p) text t=
  draw edges@#(s)(p) t
enddef;


vardef drawdiredge@#(text s)(text p) text t=
  save mpt_dir;
  boolean mpt_dir;
  mpt_dir:=true;
  drawedge@#(s)(p) t;
enddef;

vardef drawdiredges@#(text s)(text p) text t=
  save mpt_dir;
  boolean mpt_dir;
  mpt_dir:=true;
  drawedges@#(s)(p) t;
enddef;


vardef drawgraph@#(text s)(text p) text t=  % dessine les noeuds et les arêtes
  drawnodes@#() t;
  drawedges@#(s)(p) t;
enddef;

vardef drawdirgraph@#(text s)(text p) text t=  % dessine les noeuds et les arêtes
  drawnodes@#() t;
  drawdiredges@#(s)(p) t;
enddef;


%%%%%%%%%%%%%% Managing colors

def mpt_drop(text t) =
  def mpt_po_ = t enddef;
enddef;

mpt_drop();
def withbgcolor expr c=
  hide(mpt_bgc:=true; def mpt_opbg_ = withcolor c enddef)
enddef;

def mpt_opbg_ = enddef;

%%%%%%%%%%%%%% Grids

vardef thegridcomp(expr xmin,ymin,xmax,ymax,pas,u)=
  image(
  for i=xmin step pas until xmax:
     draw (i*u,ymin*u)--(i*u,ymax*u) withcolor gridcolor withpen pencircle scaled gridlinewidth;
     label.bot(textext("$" & decimal(i)& "$") scaled 0.6,(i*u,ymin*u)) withcolor gridcolor;
   endfor
  for i=ymin step pas until ymax:
     draw (xmin*u,i*u)--(xmax*u,i*u) withcolor gridcolor withpen pencircle scaled gridlinewidth;
     label.lft(textext("$" & decimal(i)& "$") scaled 0.6,(xmin*u,i*u)) withcolor gridcolor;
   endfor
  )
enddef;


vardef grid(text t)(text s)=
  save minmax,pasun,c;
  numeric minmax[],pasun[],c;
  c:=0;
  pasun[1]:=1cm;
  pasun[2]:=1;
  for i=s:
    c:=c+1;
    pasun[c]:=i;
  endfor
  c:=0;
  for i=t:
    c:=c+1;
    minmax[c]:=i;
  endfor
  if c=4:
    thegridcomp(minmax[1],minmax[2],minmax[3],minmax[4],pasun[2],pasun[1])
  else:
    thegridcomp(0,0,minmax[1],minmax[2],pasun[2],pasun[1])
  fi
enddef;

def drawgrid= draw grid enddef;