Debian Codegraph @

texmf-dist/metapost/mptrees/mptrees.mp

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %% mptrees.mp %%
3 %% Probability trees with MetaPost %%
4 %% [email protected] %%
5 %% Version 24.04 (April 2024) %%
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 string mptreesversion;
8 mptreesversion:="24.04";
9 %
10 % This work may be distributed and/or modified under the conditions of
11 % the LaTeX Project Public License, either version 1.3 of this license
12 % or (at your option) any later version. The latest version of this
13 % license is in http://www.latex-project.org/lppl.txt
14 %
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17 message "mptrees version " & mptreesversion;
18
19 if not known mplib: input latexmp fi;
20
21 boolean arbres_latexmp;
22 arbres_latexmp:=true;
23
24
25 boolean mpt_begtr; % is begintree used ?
26 mpt_begtr:=false;
27 numeric mpt_passe;
28 mpt_passe:=1;
29
30
31 % begintree permet de stocker les instructions de la figure dans mpt_tree pour calculs éventuels
32
33 def begintree=
34 mpt_begtr:=true;
35 scantokens "def mpt_tree="
36 enddef;
37
38 let endtree = enddef;
39
40 numeric mpt_tmpdirtree;
41
42
43 % à la fin de begintree, on exécute deux fois mpt_tree
44 extra_endfig:="if mpt_begtr: mpt_tree currentpicture:=nullpicture; mpt_passe:=2; AdjustNodes; mpt_tree; mpt_begtr:=false;mpt_passe:=1 fi;" & extra_endfig;
45
46
47 numeric posprob; % position de l'étiquette sur la branche
48 posprob:=-1;
49
50 numeric scaleprob;
51 scaleprob:=0.85; % échelle de l'étiquette sur la branche
52
53 numeric scaleev;
54 scaleev:=1; % échelle de l'évènement
55
56 numeric dirtree,dirlabel; % direction de l'arbre
57 dirtree:=0;
58 dirlabel:=0;
59
60 numeric proboffset; % redéfinition temporaire de labeloffset pour affichage probas
61 proboffset:=labeloffset;
62
63 numeric endedgeshift;
64 endedgeshift:=0;
65
66 numeric brokenlineratio;
67 brokenlineratio:=0.2;
68
69 numeric linewidth;
70 linewidth:=0.5bp;
71
72 color linecolor;
73 linecolor:=black;
74
75 numeric nodelinewidth; % épaisseur de la ligne autour des noeuds
76 nodelinewidth:=-1; % change pour tree et graph
77
78 pair Orig_arbre[][]; % sommet de l'arbre i,j
79 Orig_arbre[1][1]:=origin;
80
81 pair mpt_dec_ori[][]; % Décalage avec la fin de la branche précédente
82 mpt_dec_ori[1][1]:=(0,0);
83
84 pair mpt_tmp_ori[][]; % Branche i,j,k
85
86 pair mpt_br[][][]; % Branche i,j,k
87
88 boolean exist.arbre[][]; % L'arbre i,j est-il dessiné ?
89 exist.arbre[0][1]:=true;
90
91
92 numeric colonne_cour,ligne_cour;
93 numeric mptLargeur[][],mptHauteur[][]; % diff largeur au rang i, diff hauteur rang i
94
95 numeric countligne[]; % nombre de ligne pour le niveau i
96 for i=0 upto 128: countligne[i]:=0; endfor
97
98 %numeric typedec; % 0 origine dépend de la taille de l'évènement, 1 taille fixée
99 numeric shiftev; % taille du décalage dans le cas où typedec=1
100 shiftev:=-1; % dans ce cas, typedec =0
101 %extra_beginfig:=extra_beginfig & "shiftev:=-1;"; % remise à zéro en début de figure
102
103
104 numeric typeprob; % affichage de la proba sur la branche
105 typeprob:=1;
106
107 boolean abscoord; % Coordonnée absolues
108 abscoord=false;
109
110 string branchtype,endlabeltype;
111 branchtype="segment";
112 endlabeltype="none";
113
114 boolean edgearrow;
115 edgearrow:=false;
116
117 numeric endlabelspace;
118 endlabelspace:=1cm;
119
120
121 % Pour rendre les paramètres "locaux" à chaque figure
122 extra_beginfig:=extra_beginfig & "init_loc_var;";
123
124
125
126 % Étiquette au début de l'arbre
127
128 picture mptreestartlabel;
129 mptreestartlabel:=nullpicture;
130
131 vardef startlabel(expr s)=
132 colonne_cour:=0;
133 mptreestartlabel:=labelarbres(s);
134 if dirtree<>0: dirlabel:=dirtree fi;
135 if dirlabel>0:
136 theevlabel(formatnodeev(labelarbres(s)) scaled scaleev,origin,-180+dirlabel)
137 else:
138 theevlabel(formatnodeev(labelarbres(s)) scaled scaleev,origin,180+dirlabel)
139 fi
140 enddef;
141
142
143 % Étiquettes de fin d'arbre
144 vardef colonne.endlabel[]=
145 @
146 enddef;
147
148 vardef endlabel[][](expr s)=
149 save i,j,tmpbranchtype,tmpabscoord,tmps;
150 numeric i,j;
151 string tmpbranchtype,tmps;
152 boolean tmpabscoord;
153 i:=colonne.#@;
154 j:=@;
155 if string s: tmps:= "\strut \phantom{.}" & s else: picture tmps;tmps:=s fi;
156 tmpbranchtype:=branchtype;
157 branchtype:=endlabeltype;
158 tmpabscoord:=abscoord;
159 abscoord:=false;
160 tree[i][j]((endlabelspace,0) rotated (dirlabel-dirtree))(tmps," ")
161 hide(branchtype:=tmpbranchtype;abscoord:=tmpabscoord;)
162 enddef;
163
164
165 vardef labelarbres(expr s)=
166 save p; picture p;
167 if picture s: p=s
168 elseif path s: p=image(draw s)
169 else: p=textext(s)
170 fi;
171 p
172 enddef;
173
174
175 % longueur du segment qui traverse la figure p selon l'angle inc
176 vardef longdir(expr p,inc)=
177 if urcorner p = llcorner p:
178 0
179 elseif (abs inc <= abs angle (urcorner p -llcorner p)) or (abs inc > 180 - abs angle (urcorner p -llcorner p)):
180 abs((lrcorner p - llcorner p)/cosd(inc))
181 else:
182 abs((ulcorner p - llcorner p)/sind(inc))
183 fi
184 enddef;
185
186 %%%%%%%%%%%%%%
187 %% format nodes, leaves and probabilities
188 %%%%%%%%%%%%%%
189
190 % Colors
191 color nodelinecolor,nodebgcolor,nodefgcolor;
192 nodelinecolor:=black;
193 nodebgcolor:=white;
194 nodefgcolor:=black;
195
196 color leavelinecolor,leavebgcolor,leavefgcolor;
197 leavelinecolor:=black;
198 leavebgcolor:=white;
199 leavefgcolor:=black;
200
201 color problinecolor,probbgcolor,probfgcolor;
202 problinecolor:=black;
203 probbgcolor:=white;
204 probfgcolor:=black;
205
206
207 % Superellipse
208 numeric superellipseparam;
209 superellipseparam:=0.85;
210
211 vardef superellipsebox(expr p)=
212 save a,b,c,d,l;
213 pair a,b,c,d;
214 path l;
215 l := bbox p;
216 a=0.5[lrcorner l,urcorner l];
217 b=0.5[urcorner l,ulcorner l];
218 c=0.5[ulcorner l,llcorner l];
219 d=0.5[llcorner l,lrcorner l];
220 if (mpt_isgraph and (not naturalwidth) and (not mpt_isprob)):
221 l:=superellipse(1/2 right, 1/2 up, 1/2 left, 1/2 down, superellipseparam);
222 l:=l scaled nodewidth
223 elseif naturalwidth:
224 l:=superellipse(a,b,c,d,superellipseparam);
225 l:=l shifted -center l
226 else:
227 l:=superellipse(a,b,c,d,superellipseparam)
228 fi;
229 l
230 enddef;
231
232
233 % Circle
234 vardef circlebox(expr p)=
235 save a,b,c;
236 pair a,b,c;
237 a:=urcorner bbox p;
238 b:=llcorner bbox p;
239 c:=0.5[a,b];
240 if (mpt_isgraph and (not naturalwidth) and (not mpt_isprob)):
241 fullcircle scaled nodewidth
242 elseif naturalwidth and (not mpt_isprob):
243 fullcircle scaled (abs(b-a)-2)
244 else:
245 fullcircle scaled (abs(b-a)-2) shifted c
246 fi
247 enddef;
248
249
250 % square
251 vardef squarebox(expr p)=
252 save H,L;
253 numeric H,L;
254 L=abs((urcorner bbox p) - (ulcorner bbox p));
255 H=abs((urcorner bbox p) - (lrcorner bbox p));
256 if H>L: L:= H fi;
257 if (mpt_isgraph and (not naturalwidth) and (not mpt_isprob)):
258 unitsquare shifted (-0.5,-0.5) scaled nodewidth
259 elseif naturalwidth:
260 unitsquare shifted (-0.5,-0.5) scaled L
261 else:
262 unitsquare shifted (-0.5,-0.5) scaled L shifted center bbox p
263 fi
264 enddef;
265
266
267
268
269
270
271 vardef circleprobformat(expr p,cf)=
272 image(%
273 myfill circlebox(p) withcolor background;
274 draw p withcolor cf _op_ mpt_po_;
275 )
276 enddef;
277
278
279
280
281
282 vardef formatprobbox(expr p,cl,cb,cf)=
283 image(%
284 if (cb<>white) or (mpt_isprobover) or (mpt_bgc): myfill probshape(p) withcolor cb mpt_opbg_ fi;
285 draw probshape(p) withcolor cl withpen pencircle scaled nodelinewidth mpt_po_;
286 if arbrebool or mpt_isprob: draw p withcolor cf mpt_po_ fi;
287 )
288 enddef;
289
290 vardef formatnodebox(expr p,cl,cb,cf)=
291 save pp;path pp;
292 if mpt_isgraph: pp:= mpt_nodepath[num] shifted -mpt_nodecoord[num] else: pp:=nodeshape(p) fi;
293 image(%
294 if (cb<>white) or (mpt_isprobover) or (mpt_bgc): fill pp withcolor cb mpt_opbg_ fi;
295 draw pp withcolor cl withpen pencircle scaled nodelinewidth mpt_po_;
296 if arbrebool or mpt_isprob: draw p shifted (center pp-center p) withcolor cf mpt_po_ fi;
297 )
298 enddef;
299
300 vardef formatleavebox(expr p,cl,cb,cf)=
301 image(%
302 if (cb<>white) or (mpt_isprobover) or (mpt_bgc): fill leaveshape(p) withcolor cb mpt_opbg_ fi;
303 draw leaveshape(p) withcolor cl withpen pencircle scaled nodelinewidth mpt_po_;
304 if arbrebool or mpt_isprob: draw p withcolor cf mpt_po_ fi;
305 )
306 enddef;
307
308
309 % Format nodes
310
311 string nodeformat,leaveformat,probformat; %How nodes and leaves should be printed
312 nodeformat:="";
313 leaveformat:="";
314 probformat:="";
315
316
317
318 vardef formatprob(expr p)=
319 save pp;path pp;
320 pp:=probshape(p);
321 save cl,cb,cf;
322 color cl,cb,cf;
323 cl:=problinecolor;
324 cb:=probbgcolor;
325 cf:=probfgcolor;
326 if mpt_isprobover and (probformat="") and mpt_probsh_ori:
327 circleprobformat(p,cf)
328 elseif (probformat="") and mpt_probsh_ori:
329 image (draw p withcolor cf mpt_po_)
330 else:
331 formatprobbox(p,cl,cb,cf)
332 fi
333 enddef;
334
335 vardef formatnodeev(expr p)=
336 save pp;path pp;
337 pp:=nodeshape(p);
338 save cl,cb,cf;
339 color cl,cb,cf;
340 cl:=nodelinecolor;
341 cb:=nodebgcolor;
342 cf:=nodefgcolor;
343 if nodelinewidth=-1:
344 nodelinewidth:= if mpt_isgraph: 1 else: 0.5 fi
345 fi;
346 if (not mpt_isgraph) and (nodeformat="") and mpt_nodesh_ori:
347 image (draw p withcolor cf mpt_po_)
348 else:
349 formatnodebox(p,cl,cb,cf)
350 fi
351 enddef;
352
353
354
355 vardef formatnodeleave(expr p)=
356 save cl,cb,cf;
357 color cl,cb,cf;
358 cl:=leavelinecolor;
359 cb:=leavebgcolor;
360 cf:=leavefgcolor;
361 if leaveformat="none":
362 image (draw p withcolor cf)
363 elseif leaveformat="":
364 formatnodeev(p)
365 else:
366 formatleavebox(p,cl,cb,cf)
367 fi
368 enddef;
369
370
371
372
373 vardef formatnode(expr p)=
374 if known exist.arbre[colonne_cour+1][counttmp]:
375 formatnodeev(p)
376 else:
377 formatnodeleave(p)
378 fi
379 enddef;
380
381
382 boolean mpt_nodesh_ori,mpt_probsh_ori;
383 mpt_nodesh_ori:=false;
384 mpt_probsh_ori:=false;
385
386
387 % Forme du chemin autour de la oproba
388 vardef probshape(expr p)=
389 mpt_probsh_ori:=true;
390 if probformat="superellipse":
391 superellipsebox(p)
392 elseif probformat="circle":
393 circlebox(p)
394 elseif probformat="square":
395 squarebox(p)
396 else:
397 bbox p %shifted -center bbox p
398 fi
399 enddef;
400
401 % Forme du chemin autour du noeud
402 vardef nodeshape(expr p)=
403 mpt_nodesh_ori:=true;
404 if nodeformat="superellipse":
405 superellipsebox(p)
406 elseif (nodeformat="circle") or (nodeformat=""):
407 circlebox(p)
408 elseif nodeformat="square":
409 squarebox(p)
410 else:
411 bbox p %shifted -center bbox p
412 fi
413 enddef;
414
415 vardef leaveshape(expr p)=
416 if leaveformat="superellipse":
417 superellipsebox(p)
418 elseif (leaveformat="circle"):
419 circlebox(p)
420 elseif leaveformat="square":
421 squarebox(p)
422 else:
423 bbox p %shifted -center bbox p
424 fi
425 enddef;
426
427
428
429
430
431 vardef formatnodepath(expr p)=
432 nodeshape(p)
433 enddef;
434
435 %%% Prints events
436
437 vardef theevlabel(expr s,z,inc)=
438 save d; numeric d;
439 d=0.5*longdir(s,inc);
440 s shifted (z - center s + (d+labeloffset)*dir(inc))
441 enddef;
442
443 def evlabel = draw theevlabel enddef;
444
445
446
447 vardef thelabelbranchehaut(expr Ori,Fin,Fig)=
448 interim labeloffset:=proboffset;
449 save M,pp,tt;
450 pair M; path pp;numeric tt;
451 pp=dessinbranche(Ori,Fin);
452 tt = arctime posprob*(arclength pp) of pp;
453 M= point tt of pp;
454 M:=M + 0.5(llcorner Fig - lrcorner Fig);
455 if (angle(direction tt of pp) <= 90) and (angle(direction tt of pp) > -90):
456 M:=M+proboffset*unitvector((direction tt of pp) rotated 90)
457 else:
458 M:=M-proboffset*unitvector((direction tt of pp) rotated 90)
459 fi;
460 Fig shifted M
461 enddef;
462
463
464 vardef thelabelbranchehautrot(expr Ori,Fin,Fig)=
465 interim labeloffset:=proboffset;
466 save M,pp,tt;
467 pair M; path pp;numeric tt;
468 pp=dessinbranche(Ori,Fin);
469 tt = arctime posprob*(arclength pp) of pp;
470 M= point tt of pp;
471 if xpart Ori <= xpart Fin:
472 Fig shifted ( - 0.5*lrcorner Fig- 0.5* llcorner Fig+(0,proboffset)) rotated angle(direction tt of pp) shifted M
473 else:
474 Fig shifted ( - 0.5*lrcorner Fig- 0.5* llcorner Fig+(0,proboffset)) rotated (-180+angle(direction tt of pp)) shifted M
475 fi
476 enddef;
477
478
479
480 vardef thelabelbranchesuper(expr Ori,Fin,Fig)=
481 save figtmp,M,pp;
482 pair M; path pp;
483 picture figtmp;
484 pp=dessinbranche(Ori,Fin);
485 M= point (arctime posprob*(arclength pp) of pp) of pp;
486 figtmp:=Fig shifted (M-0.5*(lrcorner Fig + ulcorner Fig));
487 image(fill bbox figtmp withcolor background;
488 draw figtmp)
489 enddef;
490
491
492 vardef thelabelbranchesuperrot(expr Ori,Fin,Fig)=
493 save figtmp;
494 picture figtmp;
495 save M,pp,tt;
496 pair M; path pp;numeric tt;
497 pp=dessinbranche(Ori,Fin);
498 tt = arctime posprob*(arclength pp) of pp;
499 M= point tt of pp;
500 figtmp:=Fig shifted (-0.5*(lrcorner Fig + ulcorner Fig)) rotated angle(direction tt of pp) shifted M;
501 image(fill bbox figtmp withcolor background;
502 draw figtmp)
503 enddef;
504
505
506
507 vardef thelabelbranche(expr Ori,Fin,Fig)=
508 if typeprob=1:
509 thelabelbranchehaut(Ori,Fin,Fig)
510 elseif typeprob=2:
511 thelabelbranchesuper(Ori,Fin,Fig)
512 elseif typeprob=3:
513 thelabelbranchehautrot(Ori,Fin,Fig)
514 elseif typeprob=4:
515 thelabelbranchesuperrot(Ori,Fin,Fig)
516 fi
517 enddef;
518
519 vardef brokenline(expr Ori,Fin)=
520 save B,M;
521 pair B,M;
522 B=(xpart (Fin rotated -dirlabel), ypart (Ori rotated -dirlabel));
523 M = (brokenlineratio*abs(B-(Ori rotated -dirlabel)),0) rotated dirlabel;
524 Ori -- (Ori + M) -- (Fin - M) -- Fin
525 enddef;
526
527 numeric tenscurve;
528 tenscurve:=0;
529
530 vardef curveline(expr Ori,Fin)=
531 save tmp_angle;
532 numeric tmp_angle;
533 tmp_angle:=dirlabel+tenscurve*angle((Fin-Ori) rotated -dirlabel);
534 Ori{dir tmp_angle}..{dir tmp_angle}Fin
535 enddef;
536
537
538
539 %%%%%%%% Dessin de la branche
540
541 vardef dessinbranche(expr Ori,Fin)=
542 edgeshape(Ori,Fin)
543 enddef;
544
545
546 vardef dessinarete(expr Ori,Fin)=
547 save an,dec;
548 numeric an;pair decdeb,decfin;
549 an:=angle(Fin-Ori);
550 if edgeshift<>0:
551 startedgeshift:=edgeshift;endedgeshift:=edgeshift
552 fi;
553 decdeb:=(0,startedgeshift) rotated an;
554 decfin:=(0,endedgeshift) rotated an;
555 edgeshape(Ori+decdeb,Fin+decfin)
556 enddef;
557
558
559
560 vardef edgeshape(expr Ori,Fin)=
561 save an;
562 numeric an;
563 if mpt_isgraph:
564 an:=angle(Fin-Ori);
565 (Ori){dir (an+edgeangleaa[0])}..{dir (an-edgeangleaa[1])}(Fin)
566 elseif branchtype="segment":
567 Ori--Fin
568 elseif branchtype="curve":
569 curveline(Ori,Fin)
570 elseif branchtype="broken":
571 brokenline(Ori,Fin)
572 else:
573 Ori--Fin
574 fi
575 enddef;
576
577
578 %%%%%%%% Dessin complet de la branche (avec proba, évènement)
579
580 vardef branche_abs(expr Ori,Fin,Eve,Pro)=
581 save figtmp,thelab,figlab,tmpshft;
582 picture figtmp,thelab,figlab;
583 numeric tmpshft;
584 thelab:=labelarbres(Pro) scaled scaleprob;
585 if ypart (Ori rotated -dirtree) > ypart (Fin rotated -dirtree): %le décalage se fait en fonction de la direction de la branche
586 tmpshft:=endedgeshift
587 else:
588 tmpshft:=-endedgeshift
589 fi;
590 figlab:=thelabelbranche(Ori,(Fin+ (0,tmpshft)),thelab);
591 figtmp=image(%
592 if branchtype<>"none":
593 if edgearrow:
594 drawarrow
595 else:
596 draw
597 fi
598 dessinbranche(Ori,(Fin + (0,tmpshft))) withpen pencircle scaled linewidth withcolor linecolor;
599 fi
600 draw figlab;
601 evlabel(Eve,Fin,dirlabel)
602 );
603 figtmp
604 enddef;
605
606
607
608 vardef branche_rel(expr Ori,dx,dy,Eve,Pro)=
609 branche_abs(Ori,Ori+(dx,dy),Eve,Pro)
610 enddef;
611
612 pair mptFinBranche[][]; % Branche qui mène au noeud [][]
613 mptFinBranche[1][1]:=origin;
614
615 pair mptFinBrancheRel[][][]; % k-ieme branche qui part de i,j
616 mptFinBrancheRel[0][1][1]:=origin;
617
618 numeric mptLargBranche[][], mptHautBranche[][]; % Branche qui mène au noeud [][]
619 numeric mptNbBranches[][]; % Nombre de branches de l'arbre [][]
620 for i=0 upto 10: for j=0 upto 100: mptNbBranches[i][j]:=1; endfor endfor
621 mptNbBranches[0][1]:=1;
622
623
624 numeric mptAbs[]; % Abscisse noeud i
625 mptAbs[1]:=0;
626
627 pair mptchild[][][];
628 mptchild[0][1][1]:=(1,1);
629
630 color mptparent[][];
631 mptparent[1][1]:=(0,1,1);
632
633
634 vardef _arbre_(expr Ori)(text t)(text r)= % (pt)(pt1,pt2...)(ev,proba,ev,proba...)
635 save figtmp,n,Fintmp,labeltmp,counttmp,countlab,tmpcountligne;
636 picture figtmp,labeltmp[];
637 numeric counttmp,countlab,tmpcountligne;
638 pair Fintmp[];
639 mpt_isgraph:=false;
640 if dirtree<>0: dirlabel:=dirtree fi;
641 if colonne_cour=1:
642 countligne[1]:=1;countligne[0]:=1;
643 for i=2 upto 128: countligne[i]:=0; endfor
644 fi
645 tmpcountligne:=countligne[colonne_cour+1];
646 counttmp:=tmpcountligne;
647 for i=t:
648 counttmp:=counttmp+1;
649 countligne[colonne_cour+1]:=countligne[colonne_cour+1]+1;
650 mptFinBrancheRel[colonne_cour][ligne_cour][counttmp-tmpcountligne]:=i rotatedaround(Ori,dirtree);
651 mptFinBranche[colonne_cour+1][counttmp]:=i rotatedaround(Ori,dirtree);
652 mptLargBranche[colonne_cour+1][counttmp]:= xpart i - xpart Ori;
653 mptHautBranche[colonne_cour+1][counttmp]:= ypart i - ypart Ori;
654 mptchild[colonne_cour][ligne_cour][counttmp-tmpcountligne]:=(colonne_cour+1,counttmp);
655 mptparent[colonne_cour+1][counttmp]:=(colonne_cour,ligne_cour,counttmp-tmpcountligne);
656 endfor;
657 mptAbs[colonne_cour+1]:= xpart mptFinBranche[colonne_cour+1][counttmp];
658 mptNbBranches[colonne_cour][ligne_cour]:=counttmp-tmpcountligne;
659 figtmp=image(%
660 counttmp:=tmpcountligne;countlab:=0;
661 for i=r:
662 countlab:=countlab+1;
663 labeltmp[countlab]:=labelarbres(i);
664 if countlab=2:
665 counttmp:=counttmp+1;
666 labeltmp[1]:=formatnode(labeltmp[1]);
667 labeltmp[2]:=formatprob(labeltmp[2]);
668 draw branche_abs(Ori,mptFinBranche[colonne_cour+1][counttmp],labeltmp[1] scaled scaleev,labeltmp[2]);
669 Orig_arbre[colonne_cour+1][counttmp]:=mptFinBranche[colonne_cour+1][counttmp] shifted
670 if shiftev<0: ((longdir(labeltmp[1] scaled scaleev,dirlabel) + 2labeloffset)*(1,0))
671 else: (shiftev,0)
672 fi
673 rotatedaround(mptFinBranche[colonne_cour+1][counttmp],dirlabel);
674 if mpt_passe=1:
675 mpt_dec_ori[colonne_cour+1][counttmp]:= (Orig_arbre[colonne_cour+1][counttmp] rotatedaround(Ori,-dirtree))- mptFinBranche[colonne_cour+1][counttmp]
676 fi;
677 countlab:=0
678 fi;
679 endfor
680 );
681 figtmp
682 enddef;
683
684
685
686
687
688 vardef arbre_decv(expr Ori,horiz,Ymax)(text t)(text r)=
689 % (pt,dec horiz)(dy1,dy2...)(ev,proba,ev,proba...)
690 save Fin,compt,Ytmp;
691 numeric compt,Ytmp;
692 pair Fin[];
693 if abscoord:
694 Fin[0]:=(horiz,Ymax)
695 else:
696 Fin[0]:=Ori+(horiz,Ymax)
697 fi;
698 compt:=0;Ytmp:=Ymax;
699 for i=t:
700 compt:=compt+1;Ytmp:=i;
701 if abscoord:
702 Fin[compt]:=(horiz,Ytmp)
703 else:
704 Fin[compt]:=Ori+(horiz,Ytmp)
705 fi;
706 endfor
707 def ptarr=
708 Fin[0]
709 for i=1 upto compt:
710 ,Fin[i]
711 endfor
712 enddef;
713 _arbre_(Ori)(ptarr)(r)
714 enddef;
715
716
717 vardef arbre_fixe(expr Ori,horiz,Ymax,decy)(text r)=
718 save Fin,compt,compt_par;
719 numeric compt,compt_par;
720 pair Fin[];
721 if abscoord:
722 Fin[0]:=(horiz,Ymax + ypart Ori)
723 else:
724 Fin[0]:=Ori+(horiz,Ymax)
725 fi;
726 compt:=0;compt_par:=0;
727 for i=r:
728 compt:=compt+1;compt_par:=compt_par+1;
729 if compt_par=2:
730 compt_par:=0;
731 if abscoord:
732 Fin[compt/2]:=(0, ypart Ori) + (horiz,Ymax-decy*compt/2)
733 else:
734 Fin[compt/2]:=Ori+(horiz,Ymax-decy*compt/2)
735 fi;
736 fi
737 endfor
738 def ptarr=
739 Fin[0]
740 for i=1 upto compt/2-1:
741 ,Fin[i]
742 endfor
743 enddef;
744 _arbre_(Ori)(ptarr)(r)
745 enddef;
746
747
748 vardef colonne.reg.arbre[]=
749 @
750 enddef;
751
752 vardef colonne.comp.arbre[]=
753 @
754 enddef;
755
756 vardef colonne.dec.arbre[]=
757 @
758 enddef;
759
760 vardef colonne.calc.arbre[]=
761 @
762 enddef;
763
764 vardef colonne.arbre[]=
765 @
766 enddef;
767
768 vardef reg.arbre[][](text t)(text r)=
769 save compt,decal;
770 numeric compt,decal[];
771 colonne_cour:=colonne.#@;
772 ligne_cour:=@;
773 compt:=0;
774 for i=t:
775 if numeric i:
776 compt:=compt+1;
777 decal[compt]:=i
778 fi;
779 endfor
780 compt:=0;
781 for i=r:
782 compt:=compt+1;
783 endfor
784 arbre_fixe(Orig_arbre[colonne_cour][ligne_cour],decal[1],(compt/2-1)*decal[2]/2,decal[2])(r)
785 enddef;
786
787
788
789 vardef comp.arbre[][](text t)(text r)=
790 colonne_cour:=colonne.#@;
791 ligne_cour:=@;
792 save tmp_compt;
793 numeric tmp_compt;
794 tmp_compt:=0;
795 if abscoord:
796 def tmp_text=t enddef
797 else:
798 def tmp_text= % Si abscoord tmp_text=t
799 for i=t:
800 hide(tmp_compt:=tmp_compt+1;)
801 if (tmp_compt>1) and (not string i):
802 ,Orig_arbre[colonne_cour][ligne_cour]+i
803 elseif not string i:
804 Orig_arbre[colonne_cour][ligne_cour]+i
805 fi
806 endfor
807 enddef
808 fi;
809 _arbre_(Orig_arbre[colonne_cour][ligne_cour])(tmp_text)(r)
810 enddef;
811
812
813 %%%%%%%%%%%%%%%%%%%%%%%%% Modif 10/16 - Décalage par rapport à l'origine
814 vardef dec.arbre[][](text t)(text r)=
815 save decal;
816 numeric decal[];
817 colonne_cour:=colonne.#@;
818 ligne_cour:=@;
819 save tmp_compt;
820 numeric tmp_compt;
821 tmp_compt:=0;
822 for i=t:
823 if numeric i:
824 tmp_compt:=tmp_compt+1;
825 decal[tmp_compt]:=i
826 fi;
827 endfor
828 def tmp_text=
829 decal[3]
830 for i=4 upto tmp_compt:
831 ,decal[i]
832 endfor
833 enddef;
834 arbre_decv(Orig_arbre[colonne_cour][ligne_cour],decal[1],decal[2])(tmp_text)(r)
835 enddef;
836
837
838 vardef calc.arbre[][](text r)=
839 colonne_cour:=colonne.#@;
840 ligne_cour:=@;
841 save mpt_O,mpt_dec_calc;
842 pair mpt_O;
843 numeric mpt_dec_calc[];
844 for i=1 upto mptNbBranches[colonne_cour][ligne_cour]:
845 mpt_dec_calc[i]:=mpt_decbranche[colonne_cour][ligne_cour][i];
846 endfor
847 mpt_dec_calc[0]:=widthbranch*(scalebranch**colonne_cour);
848 def tmp_text_calc=
849 mpt_dec_calc[0]
850 for i=1 upto mptNbBranches[colonne_cour][ligne_cour]:
851 ,mpt_dec_calc[i]
852 endfor
853 enddef;
854 mpt_O:= mpt_tmp_ori[colonne_cour][ligne_cour];
855 dec.arbre[colonne_cour][ligne_cour](tmp_text_calc)(r)
856 % _arbre_(mpt_O)(tmp_text_calc)(r)
857 enddef;
858
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % Macro principale - Modification Mars 2022 (variables locales)
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862
863
864 % Conversion des ' en " pour les variables locales.
865
866 vardef singletodoublequote(expr chdb)=
867 save chsi;
868 string chsi;
869 chsi:="";
870 for i=1 upto length(chdb):
871 if ASCII(substring(i-1,i) of chdb) = 39:
872 chsi := chsi & char(34)
873 else:
874 chsi := chsi & substring(i-1,i) of chdb
875 fi;
876 endfor
877 chsi
878 enddef;
879
880 % Initialisation variables locales
881
882 def init_loc_var=
883 numeric tmp_posprob, tmp_shiftev, tmp_typeprob, tmp_scaleev, tmp_scaleprob, tmp_proboffset, tmp_endedgeshift;
884 boolean tmp_edgearrow;
885 string tmp_branchtype;
886 tmp_posprob := posprob;
887 tmp_typeprob := typeprob;
888 tmp_shiftev:= shiftev;
889 tmp_scaleev := scaleev;
890 tmp_scaleprob := scaleprob;
891 tmp_proboffset := proboffset;
892 tmp_endedgeshift := endedgeshift;
893 tmp_edgearrow := edgearrow;
894 tmp_branchtype := branchtype;
895 save posprob, shiftev, typeprob, scaleev, scaleprob, proboffset, endedgeshift, edgearrow, branchtype;
896 numeric posprob, shiftev, typeprob, scaleev, scaleprob, proboffset, endedgeshift;
897 boolean edgearrow;
898 string branchtype;
899 posprob:=tmp_posprob;
900 typeprob:=tmp_typeprob;
901 shiftev:=tmp_shiftev;
902 scaleev := tmp_scaleev;
903 scaleprob := tmp_scaleprob;
904 proboffset := tmp_proboffset;
905 endedgeshift := tmp_endedgeshift;
906 edgearrow := tmp_edgearrow;
907 branchtype := tmp_branchtype;
908 enddef;
909
910
911 pair current_tree;
912
913 boolean arbrebool; % est-on à l'intérieur d'un arbre ?
914 arbrebool:=false;
915
916 vardef arbre[][](text t)(text r)=
917 arbrebool:=true;
918 naturalwidth:=false;
919 init_loc_var;
920 if posprob=-1: posprob:=0.6 fi;
921 save tmp_type,tmp_compt,tmp_chdb;
922 numeric tmp_type,tmp_compt,tmp_comptii;
923 string tmp_chdb;
924 tmp_compt:=0;tmp_comptii:=0;
925 for i=t:
926 tmp_comptii:=tmp_comptii+1;
927 if numeric i:
928 tmp_compt:=tmp_compt+1
929 elseif string i:
930 tmp_chdb:=singletodoublequote(i);
931 scantokens tmp_chdb;
932 fi;
933 endfor
934 exist.#@.@:=true;
935 current_tree:=(colonne.#@,@);
936 if tmp_comptii=0:
937 if mpt_passe=1:
938 reg.#@.@(widthbranch*(scalebranch**colonne_cour),gapnode*(2**(3-colonne_cour)))(r)
939 else:
940 calc.#@.@(r)
941 fi
942 else:
943 Orig_arbre[1][1]:=origin;
944 if tmp_compt=0: % Arbre défini par des points (dec_horiz,dec_vert)
945 comp.#@.@(t)(r)
946 elseif tmp_compt=2: % Arbre régulier, on donne largeur et esp. vert.
947 reg.#@.@(t)(r)
948 else:
949 dec.#@.@(t)(r) % Arbre semi-régulier, largeur fixe, decalages vert.
950 fi
951 fi
952 enddef;
953
954
955 def tree = arbre enddef;
956
957 %%%%% stree (simple tree) Arbre sans les probas
958
959 vardef colonne.stree[]=
960 @
961 enddef;
962
963 vardef stree[][](text t)(text r)=
964 save cc,ll;
965 numeric cc,ll;
966 cc:=colonne.#@;
967 ll:=@;
968 def tmptxt=
969 for i=r:
970 i,"",
971 endfor
972 enddef;
973 tree[cc][ll](t)(tmptxt)
974 enddef;
975
976
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 %%%%%%%% "Calculated" trees
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980
981 vardef ymean(expr i,j)=
982 (0
983 for k=1 upto mptNbBranches[i][j]:
984 + ypart (mptFinBrancheRel[i][j][k] rotated -dirtree)
985 endfor)/mptNbBranches[i][j]
986 enddef;
987
988
989 vardef barycentre(expr A,a,B,b)=
990 (a*A+b*B)/(a+b)
991 enddef;
992
993 numeric mpt_fact_gap;
994 mpt_fact_gap:=1.5;
995
996 vardef exlignes(expr col,lig)=
997 % renvoie (x,y) tel que x<lig<y, x et y ont des child, 0 sinon
998 save tmpcl,tmpx,tmpy,ytrue;
999 pair tmpcl;
1000 numeric tmpx,tmpy;
1001 boolean ytrue;
1002 tmpx:=0;tmpy:=0;ytrue:=false;
1003 for i=1 upto countligne[col+1]:
1004 if known exist.arbre[col+1][i]:
1005 if i<lig:
1006 tmpx:=i
1007 else:
1008 tmpy:=i;ytrue:=true;
1009 fi
1010 fi;
1011 exitif ytrue;
1012 endfor
1013 (tmpx,tmpy)
1014 enddef;
1015
1016
1017 vardef mptCalcIsol(expr a,b,c,l)=
1018 save tmpmin,tmpmax;
1019 numeric tmpmin,tmpmax;
1020 tmpmin:=xpart exlignes(a,l);
1021 tmpmax:=ypart exlignes(a,l);
1022 if tmpmin=0:
1023 mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmax]][bluepart mptparent[a+1][tmpmax]] + (tmpmax-l)*gapnode*mpt_fact_gap*((0,1) rotated dirtree)
1024 elseif tmpmax=0:
1025 mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmin]][bluepart mptparent[a+1][tmpmin]] - (l-tmpmin)*gapnode*mpt_fact_gap*((0,1) rotated dirtree)
1026 else:
1027 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)
1028 fi
1029 enddef;
1030
1031
1032 numeric tmplig,tmpligii;
1033
1034 numeric mpt_decbranche[][][];
1035
1036
1037
1038 def AdjustNodes=
1039 tmplig:=0;
1040 for i=1 upto countligne[colonne_cour]:
1041 for j= 1 upto mptNbBranches[colonne_cour][i]:
1042 mptFinBrancheRel[colonne_cour][i][j]:=mptFinBranche[colonne_cour+1][1] -tmplig*gapnode*((0,1) rotated dirtree);
1043 tmplig:=tmplig+1;
1044 endfor
1045 endfor
1046 for cmpttmp:=1 upto colonne_cour-1: % plusieurs tours pour tout régler...
1047 for col=colonne_cour-1 downto 0:
1048 tmplig:=0;
1049 for i=1 upto countligne[col]:
1050 if known mptNbBranches[col][i]:
1051 for j= 1 upto mptNbBranches[col][i]:
1052 if known exist.arbre[col][i]:
1053 tmplig:=tmplig+1;
1054 if known exist.arbre[col+1][tmplig]:
1055 mptFinBrancheRel[col][i][j]:=
1056 (xpart (mptFinBrancheRel[col][i][j] rotated -dirtree),ymean(col+1,tmplig)) rotated dirtree;
1057 else:
1058 mptFinBrancheRel[col][i][j]:=mptCalcIsol(col,i,j,tmplig);
1059 fi;
1060 Orig_arbre[col+1][tmplig]:=(xpart (Orig_arbre[col+1][tmplig] rotated -dirtree),ypart (mptFinBrancheRel[col][i][j] rotated -dirtree)) rotated dirtree;
1061 fi;
1062 endfor
1063 fi
1064 endfor
1065 endfor
1066 % adjustii;
1067 endfor
1068 for col=1 upto colonne_cour:
1069 for i=1 upto countligne[col]:
1070 if known exist.arbre[col][i]:
1071 for j=1 upto mptNbBranches[col][i]:
1072 mpt_decbranche[col][i][j]:=ypart (mptFinBrancheRel[col][i][j] rotated -dirtree) - ypart (Orig_arbre[col][i] rotated -dirtree);
1073 endfor
1074 fi
1075 endfor
1076 endfor
1077 Orig_arbre[1][1]:=origin;
1078 enddef;
1079
1080
1081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 %%%%%%%% regular trees
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084
1085 numeric scalebranch;
1086 scalebranch := 0.8;
1087
1088 numeric widthbranch,gapnode;
1089 widthbranch:=3.5cm;
1090 gapnode:=0.7cm;
1091
1092 vardef regulartree(expr n)(text t)(text r)=
1093 save tmp,ll,shiftev,tmpstr,count,cc,tmp_compt,h;
1094 numeric ll,count,cc,tmp_compt,h;
1095 string tmpstr[][];
1096 count:=0;cc:=1;
1097 for i = r:
1098 count := count + cc;
1099 cc := 1 - cc;
1100 tmpstr[count][cc] := i;
1101 endfor
1102 shiftev:=-1;
1103 for j=1 upto count:
1104 if shiftev < longdir(labelarbres(tmpstr[j][0]),dirtree):
1105 shiftev := longdir(labelarbres(tmpstr[j][0]),dirtree)
1106 fi;
1107 endfor
1108 shiftev := shiftev + 2*labeloffset;
1109 tmp_compt:=1;
1110 for i=t:
1111 if (not string i) and (tmp_compt=1):
1112 ll := i; tmp_compt:=2
1113 elseif (not string i) and (tmp_compt=2):
1114 h := i
1115 else:
1116 scantokens i
1117 fi;
1118 endfor
1119 picture tmp;
1120 tmp := image(%
1121 for i := 1 upto n:
1122 for j := 1 upto (count**(i-1)):
1123 draw tree[i][j](ll,h*(count**(n-i)))(r);
1124 endfor
1125 ll := ll * scalebranch;
1126 endfor
1127 );
1128 tmp
1129 enddef;
1130
1131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1132 %%%%%%%% Binomial
1133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1134
1135 string bernoullisuccessevent, bernoullifailureevent, bernoullisuccessprob, bernoullifailureprob;
1136 bernoullisuccessevent := "$S$";
1137 bernoullifailureevent := "$\overline{S}$";
1138 bernoullisuccessprob := "$p$";
1139 bernoullifailureprob := "$q$";
1140 numeric bernoulliscalebranch;
1141 bernoulliscalebranch := 0.8;
1142
1143 vardef bernoulliprocess(expr n)(text t)(text r)=
1144 save tmp_ct,tmp_ch;
1145 numeric tmp_ct;
1146 string tmp_ch[];
1147 tmp_ch[1]:=bernoullisuccessevent;
1148 tmp_ch[2]:=bernoullisuccessprob;
1149 tmp_ch[3]:=bernoullifailureevent;
1150 tmp_ch[4]:=bernoullifailureprob;
1151 tmp_ct:=0;
1152 for i=r:
1153 tmp_ct:=tmp_ct+1;
1154 if string i:
1155 tmp_ch[tmp_ct]:=i
1156 fi;
1157 endfor
1158 regulartree(n)(t)(tmp_ch[1],tmp_ch[2],tmp_ch[3],tmp_ch[4])
1159 enddef;
1160
1161
1162
1163 vardef bernoulliprocessL(expr n)(text t)(text r)=
1164 save L,H,l,hh,lpic,hpic;
1165 numeric L,H,l,hh,lpic,hpic;
1166 save tmp_ct,tmp_chi,tmp_b;
1167 numeric tmp_ct;
1168 string tmp_chi[];
1169 boolean tmp_b;
1170 tmp_chi[1]:=bernoullisuccessevent;
1171 tmp_chi[2]:=bernoullisuccessprob;
1172 tmp_chi[3]:=bernoullifailureevent;
1173 tmp_chi[4]:=bernoullifailureprob;
1174 tmp_b:=true;
1175 for i=t:
1176 if numeric i:
1177 if tmp_b: L:=i; tmp_b := false else: H:=i fi
1178 fi;
1179 endfor
1180 tmp_ct:=0;
1181 for i=r:
1182 tmp_ct:=tmp_ct+1;
1183 if string i:
1184 tmp_chi[tmp_ct]:=i
1185 fi;
1186 endfor
1187 hpic:=0.5*longdir(labelarbres(tmp_chi[1]),90+dirtree) + 0.5*longdir(labelarbres(tmp_chi[3]),90+dirtree);
1188 hh := (H-hpic)/((2**n)-1);
1189 lpic:=longdir(mptreestartlabel,dirtree) + n*max(longdir(labelarbres(tmp_chi[1]),dirtree) , longdir(labelarbres(tmp_chi[3]),dirtree)) + (2*n-1)*labeloffset;
1190 if bernoulliscalebranch<>1:
1191 l:= (L-lpic)*(1-bernoulliscalebranch)/(1-bernoulliscalebranch**n)
1192 else:
1193 l:= (L-lpic)/n
1194 fi;
1195 def tmp_text=
1196 l,hh
1197 for i=t:
1198 if string i: ,i fi
1199 endfor
1200 enddef;
1201 regulartree(n)(tmp_text)(tmp_chi[1],tmp_chi[2],tmp_chi[3],tmp_chi[4])
1202 enddef;
1203
1204
1205 vardef binomialtree(expr n)(expr l,h)=
1206 save tmp,ll,posprob,shiftev,bernoulliscalebranch;
1207 numeric ll;
1208 posprob:=0.5;bernoulliscalebranch:=1;
1209 shiftev:=longdir(labelarbres("3"),dirtree)+2*labeloffset;
1210 ll := l;
1211 picture tmp;
1212 tmp := image(%
1213 draw tree[1][1](ll,h)("$1$", bernoullisuccessprob, "$0$", bernoullifailureprob);
1214 if n>1:
1215 for i := 2 upto n:
1216 for j := 1 upto (i-1):
1217 draw tree[i][2*j-1](ll,h)(decimal(i-j+1), bernoullisuccessprob, " ", bernoullifailureprob);
1218 endfor
1219 draw tree[i][2*i-2](ll,h)("$1$", bernoullisuccessprob, "$0$", bernoullifailureprob);
1220 endfor
1221 ll := ll * bernoulliscalebranch;
1222 fi
1223 );
1224 tmp
1225 enddef;
1226
1227 vardef binomialtreeL(expr n)(expr L,H)=
1228 save l,h,lpic,hpic;
1229 numeric l,h,lpic,hpic;
1230 hpic:=longdir(labelarbres("0"),90+dirtree);
1231 h := (H-hpic)/n;
1232 lpic:=longdir(mptreestartlabel,dirtree) + n*max(longdir(labelarbres("0"),dirtree) , longdir(labelarbres("0"),dirtree)) + (2*n-1)*labeloffset;
1233 l:= (L-lpic)/n;
1234 binomialtree(n)(l,h)
1235 enddef;
1236
1237
1238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1240 %
1241 % GRAPHES
1242 %
1243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245
1246
1247 %%%%%%
1248 %% Paramètres
1249 %%%%%
1250
1251
1252 numeric nodewidth,edgelinewidth,edgearrowpos,edgeshift,startegdeshift;
1253 nodewidth:=0.6cm;
1254 edgelinewidth:=1;
1255 edgearrowpos:=1;
1256 edgeshift:=0;
1257 startedgeshift:=0;
1258
1259 boolean naturalwidth;
1260 naturalwidth:=false;
1261
1262 numeric nodelabeloffset, nodeedgeoffset; % pour la position des étiquettes
1263 nodelabeloffset:=2;
1264 nodeedgeoffset:=-101;
1265
1266
1267
1268 numeric edgeangle,loopangle,loopsize,loopstartangle;
1269 edgeangle:=0;
1270 loopangle:=90;
1271 loopsize:=-1;
1272 loopstartangle:=30;
1273
1274
1275 %%% Format du nom des sommets
1276 string defnodename;
1277 defnodename:="A";
1278 string nodename;
1279 nodename:="array";
1280 numeric startnodename;
1281 startnodename=1;
1282
1283
1284 boolean printnodename;
1285 printnodename:=true;
1286
1287
1288
1289 numeric mpt_numtotal; % nombre total de sommets
1290 mpt_numtotal:=0;
1291
1292 pair mpt_nodecoord[]; % coordonnées du noeud i
1293 string mpt_nodename[]; % nom du noeud i
1294 path mpt_nodepath[]; % chemin autour du noeud i
1295 string mpt_nodelpart[],mpt_nodenumpart[]; % parties alphanumérique et numérique du nom du noeud
1296
1297 color gridcolor;
1298 gridcolor:=0.7white;
1299
1300 numeric gridlinewidth;
1301 gridlinewidth:=0.5;
1302
1303
1304
1305 %%%% Variables privées
1306 boolean mpt_dir; %arête orientée ou non
1307 mpt_dir:=false;
1308
1309 boolean mpt_isgraph;
1310 mpt_isgraph:=false;
1311
1312 picture mpt_tmp_fig_cbg;
1313 path mpt_tmp_branche;
1314 string mpt_tmp_prob;
1315
1316 path mpt_tmp_path;
1317
1318 boolean mpt_isprob,mpt_isprobover;
1319 mpt_isprob:=false;
1320 mpt_isprobover:=false;
1321
1322
1323 boolean mpt_bgc;
1324 mpt_bgc:=false;
1325
1326
1327
1328
1329
1330 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Noeuds
1331
1332 vardef mptlpart(expr tt)=
1333 save tmp;
1334 string tmp;
1335 tmp:="";
1336 for i =1 upto length tt:
1337 if not isdigit substring(i-1,i) of tt:
1338 tmp:=tmp & substring(i-1,i) of tt
1339 fi;
1340 endfor
1341 tmp
1342 enddef;
1343
1344 vardef mptnumpart(expr tt)=
1345 save tmp;
1346 string tmp;
1347 tmp:="";
1348 for i =1 upto length tt:
1349 if isdigit substring(i-1,i) of tt:
1350 tmp:=tmp & substring(i-1,i) of tt
1351 fi;
1352 endfor
1353 tmp
1354 enddef;
1355
1356 % définit le noeud et certains attributs
1357 def defnode(suffix ptA)(expr A)=
1358 save tmpchcomp,tmpchlet,tmpchnum;
1359 string tmpchcomp,tmpchlet,tmpchnum;
1360 mpt_isgraph:=true;arbrebool:=false;
1361 tmpchcomp:= str ptA;
1362 tmpchlet:=mptlpart(str ptA);
1363 tmpchnum:=mptnumpart(str ptA);
1364 if tmpchnum="":
1365 pair ptA;
1366 ptA:=A;
1367 else:
1368 scantokens("if not known " & tmpchlet & ": pair " & tmpchlet & "[] fi;" & tmpchlet & tmpchnum & ":=("& decimal(xpart A)&","& decimal(ypart A) & ");")
1369 fi;
1370 mpt_numtotal:=mpt_numtotal+1;
1371 mpt_nodecoord[mpt_numtotal]:=A;
1372 mpt_nodename[mpt_numtotal]:= tmpchcomp;
1373 mpt_nodelpart[mpt_numtotal]:= tmpchlet;
1374 mpt_nodenumpart[mpt_numtotal]:= tmpchnum;
1375 mpt_nodepath[mpt_numtotal]:= formatnodepath(labelarbres(str ptA) scaled scaleev) shifted A;
1376 enddef;
1377
1378
1379 def defnodestr(expr ptA)(expr A)= % Quand le premier paramètre est une chaine simple
1380 save tmpchcomp,tmpchlet,tmpchnum;
1381 string tmpchcomp,tmpchlet,tmpchnum;
1382 mpt_isgraph:=true;arbrebool:=false;
1383 tmpchcomp:= ptA;
1384 tmpchlet:=ptA;
1385 tmpchnum:="";
1386 scantokens("pair " & tmpchcomp & ";" & tmpchcomp & ":=("& decimal(xpart A)&","& decimal(ypart A) & ");")
1387 mpt_numtotal:=mpt_numtotal+1;
1388 mpt_nodecoord[mpt_numtotal]:=A;
1389 mpt_nodename[mpt_numtotal]:= tmpchcomp;
1390 mpt_nodelpart[mpt_numtotal]:= tmpchlet;
1391 mpt_nodenumpart[mpt_numtotal]:= tmpchnum;
1392 mpt_nodepath[mpt_numtotal]:= formatnodepath(labelarbres(tmpchcomp) scaled scaleev) shifted A;
1393 enddef;
1394
1395
1396 def defnodes(text t)= % Pour définir plusieurs noeuds à la fois
1397 mpt_isgraph:=true;arbrebool:=false;
1398 if (nodename="array") or (nodename="arabic"):
1399 for i=t:
1400 mpt_numtotal:=mpt_numtotal+1;
1401 if mpt_numtotal=1:
1402 scantokens("pair " & defnodename & "[];")
1403 fi;
1404 scantokens(defnodename & decimal(mpt_numtotal) & ":=("& decimal(xpart i)&","& decimal(ypart i) & ");");
1405 mpt_nodecoord[mpt_numtotal]:=i;
1406 mpt_nodename[mpt_numtotal]:= defnodename & decimal(mpt_numtotal);
1407 mpt_nodelpart[mpt_numtotal]:= defnodename;
1408 mpt_nodenumpart[mpt_numtotal]:= decimal(mpt_numtotal);
1409 mpt_nodepath[mpt_numtotal]:= formatnodepath(labelarbres(mpt_nodename[mpt_numtotal]) scaled scaleev) shifted mpt_nodecoord[mpt_numtotal];
1410 endfor
1411 elseif (nodename="Alph") or (nodename="alph"):
1412 save tmp_str,tmp_nn;
1413 string tmp_str;
1414 numeric tmp_nn;
1415 tmp_nn:= if nodename="Alph": 64 else: 96 fi;
1416 for i=t:
1417 tmp_str:=char(mpt_numtotal+startnodename+tmp_nn);
1418 defnodestr(tmp_str)(i);
1419 endfor
1420 fi
1421 enddef;
1422
1423
1424 % Renvoie le rang d'un noeud par suffixe
1425 vardef searchnode(suffix ptA)=
1426 save val,st;
1427 numeric val;
1428 string st;
1429 st= str ptA;
1430 for i=1 upto mpt_numtotal:
1431 if mpt_nodename[i]=st:
1432 val=i
1433 fi;
1434 exitif known val;
1435 endfor
1436 val
1437 enddef;
1438
1439 % Renvoie le rang d'un noeud suivi d'une chaine
1440 vardef searchnodech(suffix s)(expr c)=
1441 searchnode(s)
1442 enddef;
1443
1444 % Renvoie le path d'un noeud
1445 vardef nodepath(suffix ptA)=
1446 save nn;
1447 numeric nn;
1448 nn:=searchnode(ptA);
1449 mpt_nodepath[nn]
1450 enddef;
1451
1452
1453
1454 % Dessin du noeud
1455 vardef thenode@#(text t)=
1456 save tmp_fig,tmpstr;
1457 picture tmp_fig;
1458 string tmp_str;
1459 if long_texte(t)=1:
1460 tmp_fig:=thenodenum@#(searchnode(t))
1461 else:
1462 for i=t:
1463 if string i: tmp_fig:=thenodenum@#(searchnodech(t),i) fi;
1464 endfor
1465 fi;
1466 tmp_fig
1467 enddef;
1468
1469 vardef thenodenum@#(text t)=
1470 save tmp_pt,ch,dess,latch,num;
1471 numeric num;
1472 pair tmp_pt;
1473 string ch,latch;
1474 picture dess;
1475 mpt_isgraph:=true;arbrebool:=false;
1476 % interim labeloffset:=nodelabeloffset+nodewidth/2;
1477 interim labeloffset:=nodelabeloffset;
1478 if ((ASCII str @# < 58) or (ASCII str @# = 91)) and (str @#<>""):
1479 def mylabel = labelang enddef
1480 else:
1481 def mylabel = label enddef
1482 fi;
1483 if long_texte(t)=1:
1484 num:=t;
1485 dess:=image(
1486 ch:="$" & _chainec(mpt_nodename[num]) & "$";
1487 if nodename="arabic": ch := "$" & decimal(num) & "$" fi;
1488 mpt_nodepath[num]:=formatnodepath(labelarbres(ch) scaled scaleev) shifted mpt_nodecoord[num];
1489 draw formatnodeev(labelarbres(ch) scaled scaleev) shifted mpt_nodecoord[num];
1490 if printnodename:
1491 mylabel@#(textext(ch) scaled scaleev, ptofpath@#(mpt_nodepath[num],mpt_nodecoord[num])) withcolor nodefgcolor mpt_po_
1492 fi)
1493 else:
1494 dess:=image(%
1495 for PP=t:
1496 if numeric PP:
1497 num:=PP;
1498 tmp_pt:=mpt_nodecoord[num];
1499 else:
1500 mpt_nodepath[num]:=formatnodepath(labelarbres(PP) scaled scaleev) shifted mpt_nodecoord[num];
1501 draw formatnodeev(labelarbres(PP) scaled scaleev) shifted tmp_pt;
1502 if printnodename:
1503 if string PP:
1504 mylabel@#(textext(PP),ptofpath@#(mpt_nodepath[num],mpt_nodecoord[num])) withcolor nodefgcolor mpt_po_;
1505 else:
1506 mylabel@#(PP scaled defaultscale,ptofpath@#(mpt_nodepath[num],mpt_nodecoord[num])) withcolor nodefgcolor mpt_po_;
1507 fi
1508 fi
1509 fi;
1510 endfor)
1511 fi;
1512 dess
1513 enddef;
1514
1515 vardef node@#(text s)=
1516 save mpt_boolnum;
1517 boolean mpt_boolnum;
1518 mpt_boolnum:=false;
1519 for i=s:
1520 if numeric i:
1521 mpt_boolnum:=true
1522 fi;
1523 endfor
1524 if mpt_boolnum:
1525 thenodenum@#(s)
1526 else:
1527 thenode@#(s)
1528 fi
1529 enddef;
1530
1531
1532 vardef firstnode@#(suffix s)(text t) text r=
1533 drawnode@#(s) r;
1534 def tmp_text= t enddef;
1535 enddef;
1536
1537
1538 vardef nodes@#(text t) text s= % Dessin des noeuds à partir de leur rang
1539 save tmp_pt;
1540 pair tmp_pt;
1541 save mpt_boolnum;
1542 boolean mpt_boolnum;
1543 mpt_boolnum:=false;
1544 for i=t:
1545 if numeric i:
1546 mpt_boolnum:=true
1547 fi;
1548 endfor
1549 image(
1550 if long_texte(t)=0:
1551 for i=1 upto mpt_numtotal:
1552 drawnode@#(i) s;
1553 endfor
1554 elseif mpt_boolnum:
1555 for i=t:
1556 drawnode@#(i) s;
1557 endfor
1558 else:
1559 save n;numeric n;
1560 n=long_texte(t);
1561 def tmp_text= t enddef;
1562 for i=1 upto n-1:
1563 firstnode@#(tmp_text) s;
1564 endfor
1565 drawnode@#(tmp_text) s;
1566 fi
1567 )
1568 enddef;
1569
1570
1571
1572 vardef drawnode@#(text p) text t =
1573 mpt_drop(t);
1574 mpt_tmp_fig_cbg:=image(draw (0,0) mpt_po_);
1575 draw node@#(p);
1576 mpt_bgc:=false;
1577 mpt_drop();
1578 enddef;
1579
1580 def drawnodes = draw nodes enddef;
1581
1582
1583
1584 %%%%%%%%%%%%%
1585 %% Arêtes
1586 %%%%%%%%%%%%%
1587 % chemin entre les noeuds de rangs n et m, angles en param
1588 vardef theedgeAB(expr m,n)=
1589 save an,ta,pb,pathab,A,B,C,tmpoffset;
1590 numeric an,tmpoffset;
1591 numeric ta,tb;
1592 pair A,B,C;
1593 if nodeedgeoffset=-101: tmpoffset:=0
1594 else: tmpoffset:=nodeedgeoffset
1595 fi;
1596 tmpoffset:=tmpoffset+nodelinewidth/2+edgelinewidth/2;
1597 A:=mpt_nodecoord[m];
1598 B:=mpt_nodecoord[n];
1599 path pathab;
1600 if m<>n:
1601 pathab:=dessinarete(A,B);
1602 ta=xpart(pathab intersectiontimes mpt_nodepath[m]);
1603 tb=xpart(pathab intersectiontimes mpt_nodepath[n]);
1604 pathab:= subpath(ta,tb) of pathab;
1605 ta:= arctime tmpoffset of pathab;
1606 tb:= arctime ((arclength pathab)-tmpoffset) of pathab;
1607 pathab:= subpath (ta, tb) of pathab;
1608 else:
1609 pathab:=theloopA(n)
1610 fi;
1611 pathab
1612 enddef;
1613
1614
1615
1616 % boucle sur le noeud n
1617 vardef theloopA(expr n)=
1618 save pathaa,aapath,A,B,C,tmpoffset,ta,tb;
1619 pair A,B,C;
1620 numeric tmpoffset,ta,tb;
1621 path pathaa,aapath;
1622 if nodeedgeoffset=-101: tmpoffset:=0
1623 else: tmpoffset:=nodeedgeoffset
1624 fi;
1625 tmpoffset:=tmpoffset+nodelinewidth/2+edgelinewidth/2;
1626 C:=mpt_nodecoord[n];
1627 pathaa:=C{dir (-tmp_loopangle[1]+tmp_loopangle[0])}..
1628 (C+((0.5loopsize,0.77loopsize) rotated (tmp_loopangle[0]-90))){dir (tmp_loopangle[0])}..
1629 (C+((loopsize,0) rotated (tmp_loopangle[0]))){dir (tmp_loopangle[0]+90)}..
1630 (C+((-0.5loopsize,0.7loopsize) rotated (tmp_loopangle[0]-90))){dir (tmp_loopangle[0]+180)}..
1631 {dir (tmp_loopangle[1]+tmp_loopangle[0]+180)}C;
1632 aapath:= reverse pathaa;
1633 ta=xpart(subpath(0,0.5*length pathaa) of pathaa intersectiontimes mpt_nodepath[n]);
1634 tb=xpart(subpath(0,0.5*length aapath) of aapath intersectiontimes mpt_nodepath[n]);
1635 pathaa:= subpath (ta,(length pathaa) - tb) of pathaa;
1636 ta:= arctime tmpoffset of pathaa;
1637 tb:= arctime ((arclength pathaa)-tmpoffset) of pathaa;
1638 pathaa:= subpath (ta, tb) of pathaa;
1639 pathaa
1640 enddef;
1641
1642 % Chemin entre les noeuds A et B, angles dans p
1643 vardef theedge(suffix A,B)(text p)=
1644 save nA,nB,tmp_fig;
1645 numeric nA,nB;
1646 picture tmp_fig;
1647 nA:=searchnode(A);
1648 nB:=searchnode(B);
1649 theedgenum(nA,nB)(p)
1650 enddef;
1651
1652 % Chemin entre les noeuds nA et nB, angles dans p
1653 vardef theedgenum(expr nA,nB)(text p)=
1654 save tmp_fig;
1655 picture tmp_fig;
1656 numeric edgeangleaa[],tmp_loopangle[];
1657 edgeangleaa[0]=edgeangleaa[1]=edgeangle;
1658 tmp_loopangle[0]:=loopangle;tmp_loopangle[1]:=loopstartangle;
1659 mpt_isgraph:=true;arbrebool:=false;
1660 nn:=0;
1661 for i=p:
1662 if numeric i:
1663 if nn=0: edgeangleaa[0]:=i;edgeangleaa[1]:=i;tmp_loopangle[0]:=i
1664 else: edgeangleaa[1]:=-i;tmp_loopangle[1]:=i
1665 fi;
1666 nn:=nn+1;
1667 elseif string i:
1668 mpt_tmp_prob:=i;
1669 fi
1670 endfor
1671 theedgeAB(nA,nB)
1672 enddef;
1673
1674 % Figure représentant le chemin avec (ou non) la flèche
1675 vardef theedgenumfig(expr nA,nB)(text p)=
1676 save tmppath;
1677 path tmppath;
1678 tmppath:=theedgenum(nA,nB)(p);
1679 image(draw tmppath withpen pencircle scaled edgelinewidth withcolor linecolor mpt_po_;
1680 if mpt_dir:
1681 filldraw arrowhead subpath(0, edgearrowpos*length mpt_tmp_path) of mpt_tmp_path mpt_po_
1682 fi;
1683 )
1684 enddef;
1685
1686 vardef edges@#(text s)(text p) text t= % dessine plusieurs arêtes simultanément
1687 save a,b,isp;
1688 numeric a,b;
1689 boolean isp;
1690 isp:=false;
1691 image(
1692 for i=s:
1693 if pair i:
1694 if isp: drawedge@#(a,b)(p) t; fi;
1695 a:= xpart i;b:= ypart i;
1696 isp:=true;
1697 else:
1698 drawedge@#(a,b)(i,p) t;
1699 isp:=false
1700 fi;
1701 endfor
1702 if isp: drawedge(a,b)(p) t; fi
1703 )
1704 enddef;
1705
1706
1707
1708
1709
1710
1711 vardef theedgepond@#(suffix A,B)(text p)=
1712 save nA,nB;
1713 numeric nA,nB;
1714 nA:=searchnode(A);
1715 nB:=searchnode(B);
1716 theedgepondnum@#(nA,nB)(p)
1717 enddef;
1718
1719 vardef theedgepondnum@#(expr nA,nB)(text p)=
1720 save tmpfig,tmppath,tmppt,mpt_isprob,mpt_isprobover;
1721 boolean mpt_isprob,mpt_isprobover;
1722 mpt_isprob:=true;
1723 path tmppath;
1724 picture tmpfig;
1725 pair tmppt;
1726 if str @#="": mpt_isprobover:=true else: mpt_isprobover:=false fi;
1727 if ((ASCII str @# < 58) or (ASCII str @# = 91)) and (str @#<>""):
1728 def mylabel = labelang enddef
1729 else:
1730 def mylabel = labelmp enddef
1731 fi;
1732 tmppath:=theedgenum(nA,nB)(p);
1733 tmppt:=point (arctime posprob*(arclength tmppath) of tmppath) of tmppath;
1734 image(draw theedgenum(nA,nB)(p) withpen pencircle scaled edgelinewidth withcolor linecolor mpt_po_;
1735 if mpt_dir:
1736 filldraw arrowhead subpath(0, edgearrowpos*length mpt_tmp_path) of mpt_tmp_path mpt_po_
1737 fi;
1738 mylabel@#(formatprob(labelarbres(mpt_tmp_prob) scaled scaleprob),tmppt))
1739 enddef;
1740
1741 vardef edge@#(text s)(text p)=
1742 save mpt_boolprob,mpt_boolnum;
1743 boolean mpt_boolprob,mpt_boolnum;
1744 mpt_boolprob:=false;
1745 mpt_boolnum:=false;
1746 if posprob=-1: posprob:=0.5 fi;
1747 if loopsize=-1:
1748 loopsize:=1.5nodewidth;
1749 if loopsize<0.6cm: loopsize:=0.6cm fi;
1750 fi;
1751 for i=p:
1752 if string i:
1753 mpt_boolprob:=true
1754 fi;
1755 endfor
1756 for i=s:
1757 if numeric i:
1758 mpt_boolnum:=true
1759 fi;
1760 endfor
1761 if mpt_boolnum:
1762 mpt_tmp_path:=theedgenum(s)(p);
1763 if mpt_boolprob:
1764 theedgepondnum@#(s)(p)
1765 else:
1766 theedgenumfig(s)(p)
1767 fi
1768 else:
1769 mpt_tmp_path:=theedge(s)(p);
1770 if mpt_boolprob:
1771 theedgepond@#(s)(p)
1772 else:
1773 theedge(s)(p)
1774 fi
1775 fi
1776 enddef;
1777
1778 %%%%%%%%%% Drawing edges
1779
1780 vardef drawedge@#(text s)(text p) text t=
1781 mpt_drop(t);
1782 mydraw edge@#(s)(p) withpen pencircle scaled edgelinewidth;
1783 mpt_drop();
1784 enddef;
1785
1786 vardef drawedges@#(text s)(text p) text t=
1787 mydraw edges@#(s)(p) t
1788 enddef;
1789
1790
1791 vardef drawdiredge@#(text s)(text p) text t=
1792 save mpt_dir;
1793 boolean mpt_dir;
1794 mpt_dir:=true;
1795 drawedge@#(s)(p) t;
1796 enddef;
1797
1798 vardef drawdiredges@#(text s)(text p) text t=
1799 save mpt_dir;
1800 boolean mpt_dir;
1801 mpt_dir:=true;
1802 drawedges@#(s)(p) t;
1803 enddef;
1804
1805
1806 vardef drawgraph@#(text s)(text p) text t= % dessine les noeuds et les arêtes
1807 drawnodes@#() t;
1808 drawedges@#(s)(p) t;
1809 enddef;
1810
1811 vardef drawdirgraph@#(text s)(text p) text t= % dessine les noeuds et les arêtes
1812 drawnodes@#() t;
1813 drawdiredges@#(s)(p) t;
1814 enddef;
1815
1816
1817 %%%%%%%%%%%%%% Managing colors
1818
1819 def mpt_drop(text t) =
1820 def mpt_po_ = t enddef;
1821 enddef;
1822
1823 mpt_drop();
1824 def withbgcolor expr c=
1825 hide(mpt_bgc:=true; def mpt_opbg_ = withcolor c enddef)
1826 enddef;
1827
1828 def mpt_opbg_ = enddef;
1829
1830 %%%%%%%%%%%%%% Grids
1831
1832 vardef thegridcomp(expr xmin,ymin,xmax,ymax,pas,u)=
1833 image(
1834 for i=xmin step pas until xmax:
1835 draw (i*u,ymin*u)--(i*u,ymax*u) withcolor gridcolor withpen pencircle scaled gridlinewidth;
1836 label.bot(textext("$" & decimal(i)& "$") scaled 0.6,(i*u,ymin*u)) withcolor gridcolor;
1837 endfor
1838 for i=ymin step pas until ymax:
1839 draw (xmin*u,i*u)--(xmax*u,i*u) withcolor gridcolor withpen pencircle scaled gridlinewidth;
1840 label.lft(textext("$" & decimal(i)& "$") scaled 0.6,(xmin*u,i*u)) withcolor gridcolor;
1841 endfor
1842 )
1843 enddef;
1844
1845
1846 vardef grid(text t)(text s)=
1847 save minmax,pasun,c;
1848 numeric minmax[],pasun[],c;
1849 c:=0;
1850 pasun[1]:=1cm;
1851 pasun[2]:=1;
1852 for i=s:
1853 c:=c+1;
1854 pasun[c]:=i;
1855 endfor
1856 c:=0;
1857 for i=t:
1858 c:=c+1;
1859 minmax[c]:=i;
1860 endfor
1861 if c=4:
1862 thegridcomp(minmax[1],minmax[2],minmax[3],minmax[4],pasun[2],pasun[1])
1863 else:
1864 thegridcomp(0,0,minmax[1],minmax[2],pasun[2],pasun[1])
1865 fi
1866 enddef;
1867
1868 def drawgrid= draw grid enddef;
1869
1870
1871
1872 %%%%%%%%%%%%%%%
1873 %% Outils
1874 %%%%%%%%%%%%%%%%
1875
1876
1877 vardef isdigit primary d =
1878 ("0"<=d)and(d<="9")
1879 enddef ;
1880
1881 % Convertit un suffixe en chaine avec les chiffres en indice
1882 vardef _chaine(suffix s)=
1883 save c,d;
1884 string c,d;
1885 d:= str s;
1886 if length(d)=1:
1887 c:=d
1888 elseif isdigit (substring (1,2) of d):
1889 c:= (substring (0,1) of d) & "_{" & substring (1,infinity) of d & "}"
1890 else: c:=str s
1891 fi;
1892 c
1893 enddef;
1894
1895 vardef _chainec(expr d)=
1896 save c;
1897 string c;
1898 if length(d)=1:
1899 c:=d
1900 elseif isdigit (substring (1,2) of d):
1901 c:= (substring (0,1) of d) & "_{" & substring (1,infinity) of d & "}"
1902 elseif isdigit (substring (2,3) of d):
1903 c:= (substring (0,2) of d) & "_{" & substring (2,infinity) of d & "}"
1904 else:
1905 c:=d
1906 fi;
1907 c
1908 enddef;
1909
1910 % longueur d'un paramètre text
1911 vardef long_texte(text t)=
1912 save i;
1913 numeric i;
1914 i:=0;
1915 for n=t: i:=i+1; endfor
1916 i
1917 enddef;
1918
1919
1920 % Placement fin des étiquettes
1921 vardef thelabelang@#(expr s,z)=
1922 save tmpang,tmpfig,tmppt,tmppath,tmpstr;
1923 string tmpstr;
1924 numeric tmpang;
1925 pair tmppt;
1926 path tmppath;
1927 save p; picture p;
1928 tmpstr := str @#;
1929 if picture s: p=s
1930 else: p = textext(s)
1931 fi;
1932 tmppath := llcorner p -- lrcorner p -- urcorner p -- ulcorner p -- cycle;
1933 if ASCII tmpstr = 91:
1934 tmpang := scantokens substring(1,length(tmpstr)-1) of tmpstr
1935 else:
1936 tmpang := scantokens tmpstr
1937 fi;
1938 tmppt := tmppath intersectionpoint ((0.5urcorner p+0.5llcorner p)-- (0.5urcorner p +0.5llcorner p+ 100 * (dir (180+tmpang))));
1939 p shifted (-tmppt + z + labeloffset*(cosd(tmpang),sind(tmpang)) )
1940 enddef;
1941
1942
1943 % draw et fill sans _op_
1944 def myfill expr c = addto currentpicture contour c enddef;
1945 def mydraw expr p =
1946 addto currentpicture
1947 if picture p:
1948 also p
1949 else:
1950 doublepath p withpen currentpen
1951 fi
1952 enddef;
1953
1954
1955
1956
1957 def labelang = draw thelabelang enddef;
1958 def labelmp = mydraw thelabel enddef;
1959
1960
1961 vardef ptofpath@#(expr p, c)= % Trouve le point du chemin p à la position @# par rapport à c
1962 save diam; numeric diam; diam:=abs(urcorner bbox p - llcorner bbox p);
1963 save tmpang;numeric tmpang;
1964 if (str @#=""):
1965 c
1966 elseif ((ASCII str @# < 58) or (ASCII str @# = 91)):
1967 if ASCII str @# = 91:
1968 tmpang := scantokens substring(1,length(str @#)-1) of str @#
1969 else:
1970 tmpang := scantokens str @#
1971 fi;
1972 (c--((c+diam*(1,0)) rotatedaround(c,tmpang))) intersectionpoint p
1973 else:
1974 (c--(c+diam*laboff@#)) intersectionpoint p
1975 fi
1976 enddef;
1977